Java 8时区转换

前端之家收集整理的这篇文章主要介绍了Java 8时区转换前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
Java 8中,我想将datetime从UTC转换为ACST(UTC 9:30).

输入 – > 2014-09-14T17:00:00 00:00

输出 – > 2014-09-15 02:30:00

String isoDateTime = "2014-09-14T17:00:00+00:00";
LocalDateTime fromIsoDate = LocalDateTime.parse(isoDateTime,DateTimeFormatter.ISO_OFFSET_DATE_TIME);

ZoneOffset offset = ZoneOffset.of("+09:30");
OffsetDateTime acst = OffsetDateTime.of(fromIsoDate,offset);
System.out.println(acst.toString()); // 2014-09-14T17:00+09:30
System.out.println(acst.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME)); // 2014-09-14T17:00:00+09:30

为什么不执行偏移?

解决方法

尝试:
String isoDateTime = "2014-09-14T17:00:00+00:00";
ZonedDateTime fromIsoDate = ZonedDateTime.parse(isoDateTime);
ZoneOffset offset = ZoneOffset.of("+09:30");
ZonedDateTime acst = fromIsoDate.withZoneSameInstant(offset);

System.out.println("Input:  " + fromIsoDate);
System.out.println("Output: " + acst.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME));

输出

Input:  2014-09-14T17:00Z
Output: 2014-09-15T02:30:00+09:30
原文链接:https://www.f2er.com/java/120544.html

猜你在找的Java相关文章