gpt4 book ai didi

java - DateFormatter 未按给定格式打印值

转载 作者:行者123 更新时间:2023-12-01 09:48:46 28 4
gpt4 key购买 nike

我的日期时间格式字符串是: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

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

最佳答案

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

以下是 Basil Bourque 对 wrong answer 的评论:

No, no, no. All you have done is append text, creating a falsity. Ifyour date-time represents a moment in a time zone that is two hoursahead of UTC such as Europe/Helsinki, and you slap a Z on the endwhich says Zulu and means UTC, you are now telling a lie, representingvalue that is off by two hours. This is like replacing the dollar signin a price with a Euro currency symbol but failing to change thenumber.

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

£100 != $100

2016-04-01T23:00:00.000Z 中的 Ztimezone designator零时区偏移。它代表 Zulu,指定 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

关于java - DateFormatter 未按给定格式打印值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37765401/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com