简体   繁体   English

DateTimeFormatter 问题

[英]DateTimeFormatter issue

See the following test code (java 11):请参阅以下测试代码(java 11):

public static final String DATE_FORMAT_TIMESTAMP = "YYYY-MM-dd'T'HH:mm:ss'Z'";
...
var timestamp = OffsetDateTime.now();
System.out.println(timestamp);

var formatter = DateTimeFormatter.ofPattern(DATE_FORMAT_TIMESTAMP);
var zt = timestamp.format(formatter);
System.out.println(zt);
...

The output: enter code here output: enter code here

2020-12-27T23:34:34.886272600+02:00
2021-12-27T23:34:34Z

Note formatted time year is 2021 .注意格式化的时间年份是2021 And it happens only from 27/12, probably till 31/12.它只发生在 27/12,可能到 31/12。

Can someone explain this to me?谁可以给我解释一下这个? And how do I fix the code to get the correct formatted string?以及如何修复代码以获得正确的格式化字符串?

There are two problems with your pattern:你的模式有两个问题:

  1. Use of Y instead of y : The letter Y specifies week-based-year whereas y specifies year-of-era .使用Y代替y :字母Y指定week-based-year ,而y指定year-of-era However, I recommend you use u instead of y for the reasons mentioned at `uuuu` versus `yyyy` in `DateTimeFormatter` formatting pattern codes in Java?但是,我建议您使用u而不是y ,因为在Java 中的 `DateTimeFormatter` 格式化模式代码中的 `uuuu` 与 `yyyy` 中提到的原因? . . You would also like to check this nice answer about week-based-year .您还想查看有关week-based-year这个不错的答案
  2. Enclosing Z by single quotes: This is a blunder .用单引号将Z括起来:这是一个错误 The letter Z specifies zone-offset and if you enclose it by single quotes, it will simply mean the literal, Z .字母Z指定zone-offset ,如果用单引号将其括起来,它将仅表示文字Z

Check the documentation page of DateTimeFormatter to learn more about these things.查看DateTimeFormatter 的文档页面以了解有关这些内容的更多信息。

A quick demo:快速演示:

import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;

public class Main {
    public static void main(String[] args) {
        final String DATE_FORMAT_TIMESTAMP = "uuuu-MM-dd'T'HH:mm:ssZ";
        // OffsetDateTime now with the default timezone of the JVM
        var timestamp = OffsetDateTime.now();
        System.out.println(timestamp);

        var formatter = DateTimeFormatter.ofPattern(DATE_FORMAT_TIMESTAMP);
        var zt = timestamp.format(formatter);
        System.out.println(zt);

        // OffsetDateTime now with the timezone offset of +02:00 hours
        timestamp = OffsetDateTime.now(ZoneOffset.of("+02:00"));
        System.out.println(timestamp);
        zt = timestamp.format(formatter);
        System.out.println(zt);

        // Parsing a user-provided date-time
        String strDateTime = "2020-12-27T23:34:34.886272600+02:00";
        timestamp = OffsetDateTime.parse(strDateTime);
        System.out.println(timestamp);
        zt = timestamp.format(formatter);
        System.out.println(zt);
    }
}

Output: Output:

2020-12-27T23:44:35.531145Z
2020-12-27T23:44:35+0000
2020-12-28T01:44:35.541700+02:00
2020-12-28T01:44:35+0200
2020-12-27T23:34:34.886272600+02:00
2020-12-27T23:34:34+0200

That's because of the uppercase YYYY .那是因为大写的YYYY You need yyyy here.你需要yyyy这里。

Y means week year . Y表示周年 That is the year to which the week number belongs.那是周数所属的年份。 For example, 27 Dec 2020 is week 1 of 2021.例如,2020 年 12 月 27 日是 2021 年的第 1 周。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM