Issue
When I try to convert OffsetDateTime
to LocalDateTime
from java.time, I expect the resulting LocalDateTime
to be updated with the local time zone. So, If I have an OffsetDateTime
of 2011-12-03T10:00:00Z
, and my local timezone is UTC+2, I expect the LocalDateTime to be 2011-12-03T12:00:00
, but I get instead 2011-12-03T10:00:00
. I'm converting it with the method toLocalDateTime()
that OffsetDateTime
has. It seems that it only truncates the date, removing the offset part, without adjusting the time.
So I'm trying to figure out a way to get a LocalDateTime
that represents the local date time taking into account the zone offset. Following the example, I would like to get 2011-12-03T12:00:00
Solution
LocalDateTime would give you the time of a wall clock of your OffsetDateTime. That's 10:00
You need to first convert to a ZonedDatedTime in your time zone
Like this
OffsetDateTime off = OffsetDateTime.of(2011,12,3,10,00,0,0, ZoneOffset.UTC);
ZonedDateTime zoned = off.atZoneSameInstant(ZoneId.of("Europe/Athens"));
LocalDateTime athensWallTime = zoned.toLocalDateTime();
System.out.println(athensWallTime);
Answered By - David Lilljegren
Answer Checked By - Clifford M. (JavaFixing Volunteer)