DateFormatter 未以给定格式打印值

DateFormatter is not printing value in the given format

我的日期时间格式字符串是:yyyy-MM-dd'T'HH:mm:ss.SSSZ
我正在使用 Joda Time 的 DateTimeFormatter 以上述格式打印我的日期。

现在,将日期视为

2016/04/01 23:00:00

那么它应该打印

2016-04-01T23:00:00.000Z

但是,它打印

2016-04-01T23:00:00.000+0200

请帮助我以字符串格式中指定的相同格式打印日期。

根据https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html Z有特殊含义:

Z       zone-offset

如果你想用 ':

转义 Z 引号 Z
yyyy-MM-dd'T'HH:mm:ss.SSS'Z'

例如:

java.time.LocalDateTime date = java.time.LocalDateTime.now();
java.time.format.DateTimeFormatter formatter = java.time.format.DateTimeFormatter
                .ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
java.lang.String text = date.format(formatter);
System.out.println(text);

打印

2016-06-11T18:39:41.962Z

2016-04-01T23:00:00.000Z != 2016-04-01T23:00:00.000+0200

以下是 Basil Bourque 对 的评论:

No, no, no. All you have done is append text, creating a falsity. If your date-time represents a moment in a time zone that is two hours ahead of UTC such as Europe/Helsinki, and you slap a Z on the end which says Zulu and means UTC, you are now telling a lie, representing value that is off by two hours. This is like replacing the dollar sign in a price with a Euro currency symbol but failing to change the number.

只是为了说明他提到的内容:

£100 != 0

2016-04-01T23:00:00.000Z中的Z是零时区偏移的timezone designator。它代表祖鲁语并指定 Etc/UTC 时区(时区偏移量为 +00:00 小时)。同一时刻将在不同的时区以不同的值呈现,例如

import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;

public class Main {
    public static void main(String[] args) {
        Instant instant = Instant.parse("2016-04-01T23:00:00.000Z");

        ZonedDateTime zdtNewYork = instant.atZone(ZoneId.of("America/New_York"));
        ZonedDateTime zdtIndia = instant.atZone(ZoneId.of("Asia/Kolkata"));
        ZonedDateTime zdtNepal = instant.atZone(ZoneId.of("Asia/Kathmandu"));

        System.out.println(zdtNewYork);
        System.out.println(zdtIndia);
        System.out.println(zdtNepal);

        // Or at a fixed timezone offset of +02:00 hours
        OffsetDateTime odtWithTwoHoursOffset = instant.atOffset(ZoneOffset.of("+02:00"));
        System.out.println(odtWithTwoHoursOffset);
    }
}

输出:

2016-04-01T19:00-04:00[America/New_York]
2016-04-02T04:30+05:30[Asia/Kolkata]
2016-04-02T04:45+05:45[Asia/Kathmandu]
2016-04-02T01:00+02:00

要进一步理解这个概念,请尝试将日期时间从一个时区转换为另一个时区,例如我已经展示了纽约日期时间到 UTC 的转换。我已经展示了另一种将时区偏移量 +02:00 小时的日期时间转换为 UTC 的方法。

import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;

public class Main {
    public static void main(String[] args) {
        // #######Example of converting a date-time from one timezone to another#####
        ZonedDateTime zdtNewYork = ZonedDateTime.parse("2016-04-01T19:00-04:00[America/New_York]");

        Instant instant = zdtNewYork.toInstant();
        System.out.println(instant);

        // Or as ZonedDateTime
        ZonedDateTime zdtUtc = zdtNewYork.withZoneSameInstant(ZoneId.of("Etc/UTC"));
        System.out.println(zdtUtc);

        // Alternatively, this can be obtained from instant
        zdtUtc = instant.atZone(ZoneId.of("Etc/UTC"));
        System.out.println(zdtUtc);
        // ###########################################################################

        System.out.println();

        // #####Example of converting a date-time at a fixed timezone offset to UTC###
        OffsetDateTime odtNewYork = OffsetDateTime.parse("2016-04-02T01:00+02:00");

        instant = odtNewYork.toInstant();
        System.out.println(instant);

        // Alternatively
        OffsetDateTime odtUtc = odtNewYork.withOffsetSameInstant(ZoneOffset.UTC);
        System.out.println(odtUtc);

        // Alternatively,
        odtUtc = instant.atOffset(ZoneOffset.UTC);
        System.out.println(odtUtc);
        // ###########################################################################
    }
}

输出:

2016-04-01T23:00:00Z
2016-04-01T23:00Z[Etc/UTC]
2016-04-01T23:00Z[Etc/UTC]

2016-04-01T23:00:00Z
2016-04-01T23:00Z
2016-04-01T23:00Z