ZonedDateTime to UTC with offset applied?

daydreamer picture daydreamer · Feb 28, 2016 · Viewed 37.4k times · Source

I am using Java 8
This is what my ZonedDateTime looks like

2013-07-10T02:52:49+12:00

I get this value as

z1.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME)

where z1 is a ZonedDateTime.

I wanted to convert this value as 2013-07-10T14:52:49

How can I do that?

Answer

SimMac picture SimMac · Feb 29, 2016

Is this what you want? This converts your ZonedDateTime to a LocalDateTime with a given ZoneId by converting your ZonedDateTime to an Instant before.

LocalDateTime localDateTime = LocalDateTime.ofInstant(z1.toInstant(), ZoneOffset.UTC);

Or maybe you want the users system-timezone instead of hardcoded UTC:

LocalDateTime localDateTime = LocalDateTime.ofInstant(z1.toInstant(), ZoneId.systemDefault());