以 JSON 格式接收Microsoft日期格式



我正在使用 Kotlin,不知何故,我收到了这种我以前从未见过的奇怪日期格式:

/Date(1224043200000)/

这种格式是什么,我们如何使用它显示日期? 我一直在使用 Json ISO 标准 8601。而且我知道如何转换它迄今为止的格式,但这对我来说是新的。

>1224043200000是指从1970-01-01T00:00:00Z开始的毫秒数,相当于在UTC时2008-10-15T04:00:00Z

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class Main {
public static void main(final String[] args) {
Instant instant = Instant.ofEpochMilli(1224043200000L);
System.out.println(instant);
// Get ZonedDateTime from Instant
// I have used time-zone of Europe/London. Use a time-zone as per your
// requirement. You can also use ZoneId.systemDefault()
ZonedDateTime zdt = ZonedDateTime.ofInstant(instant, ZoneId.of("Europe/London"));
// Get OffsetDateTime from ZonedDateTime
OffsetDateTime odt = zdt.toOffsetDateTime();
// Get LocalDateTime from ZonedDateTime if required. However, note that
// LocalDateTime drops the valuable information, time-zone from ZonedDateTime
// and zone-offset from OffsetDateTime
LocalDateTime ldt = zdt.toLocalDateTime();
System.out.println(ldt);
}
}

输出:

2008-10-15T04:00:00Z
2008-10-15T05:00

将 Java SE8 日期时间类向后移植到 Java SE 6 和 7:

检查 ThreeTen-Backport 以及如何使用 ThreeTenABP

最新更新