将字符串转换为UTC的ZonedDateTime。

huangapple go评论90阅读模式
英文:

Convert a String to ZonedDateTime in UTC

问题

我有一个包含日期的String,格式为ZonedDateTime - UTC格式。例如:

2020-08-21T02:05:45.231Z

我想要将它转换为一个类型为ZonedDateTime变量。格式化程序的模式应该是什么?

(注意以“Z”结尾的时间表示UTC)

英文:

I have a String which contains date in ZonedDateTime - UTC format. Example :-

2020-08-21T02:05:45.231Z

I want to convert it to a variable of type ZonedDateTime. What should be the pattern on the formatter?

(Note the time ending in "Z" which indicates UTC)

答案1

得分: 3

由于此示例的String采用了带有有效时区缩写的ISO格式,您可以无需传递模式直接解析它

就像这样:

public static void main(String[] args) {
	ZonedDateTime zdt = ZonedDateTime.parse("2020-08-21T02:05:45.231Z");
	System.out.println(zdt);
}

然后输出为(再次是)ISO格式的String

2020-08-21T02:05:45.231Z

请注意,在这里应该优先选择OffsetDateTime,因为UTC不是一个真正的时区,它是协调世界时,即没有偏移的时间。
代码略有不同:

public static void main(String[] args) {
    OffsetDateTime odt = OffsetDateTime.parse("2020-08-21T02:05:45.231Z");
    System.out.println(odt);
}

输出完全相同。

为了完整起见,这是如何将您的示例String解析为Instant

public static void main(String[] args) {
    Instant instant = Instant.parse("2020-08-21T02:05:45.231Z");
    System.out.println(instant.toEpochMilli());
}

这次,输出是与时间纪元毫秒表示的同一时刻(自1970-01-01'T'00:00:00.000...以来的毫秒数):

1597975545231
英文:

Due to this example String being in ISO format with a valid time zone abbreviation, you can directly parse it without passing a pattern.

Just like this:

public static void main(String[] args) {
	ZonedDateTime zdt = ZonedDateTime.parse("2020-08-21T02:05:45.231Z");
	System.out.println(zdt);
}

The output is then (again) the ISO-formatted String

2020-08-21T02:05:45.231Z

Please note that an OffsetDateTime should be preferred here because UTC is not a real time zone, it is the Coordinated Universal Time, the time without an offset.
The code is just slightly different:

public static void main(String[] args) {
    OffsetDateTime odt = OffsetDateTime.parse("2020-08-21T02:05:45.231Z");
    System.out.println(odt);
}

with exactly the same output.

For the sake of completeness, this is how to parse your example String to an Instant:

public static void main(String[] args) {
    Instant instant = Instant.parse("2020-08-21T02:05:45.231Z");
    System.out.println(instant.toEpochMilli());
}

This time, the output is the very same moment in time represented by epoch milliseconds (milliseconds since 1970-01-01'T'00:00:00.000...):

1597975545231

答案2

得分: 1

你还可以使用 DateTimeFormatter.ISO_ZONED_DATE_TIME

jshell> DateTimeFormatter.ISO_ZONED_DATE_TIME.parse("2020-08-21T02:05:45.231Z")
=> {InstantSeconds=1597975545, OffsetSeconds=0},ISO解析为2020-08-21T02:05:45.231
英文:

You can also use DateTimeFormatter.ISO_ZONED_DATE_TIME.

jshell> DateTimeFormatter.ISO_ZONED_DATE_TIME.parse("2020-08-21T02:05:45.231Z")
=> {InstantSeconds=1597975545, OffsetSeconds=0},ISO resolved to 2020-08-21T02:05:45.231

huangapple
  • 本文由 发表于 2020年8月17日 16:28:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/63447235.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定