Java 8 time zone conversion

In Java 8, I want to convert datetime from UTC to ACST (UTC 9:30)

Input – > 2014-09-14t17:00:00 00:00

Output – > 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

Why not perform offset?

Solution

Try:

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));

Output:

Input:  2014-09-14T17:00Z
Output: 2014-09-15T02:30:00+09:30
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>