I tried to search for the same problem but didn’t find what I’m doing wrong.
I need to convert a UTC LocalDateTime to a Europe/Rome LocalDateTime. I’m following this approach that I found on SO but I’m having a wrong result:
ZonedDateTime zonedDateTime = LocalDateTime.of(2020, 01, 01, 15, 0, 0).atZone(ZoneId.of("UTC")); System.out.println(zonedDateTime); ZonedDateTime current = zonedDateTime.withZoneSameInstant(ZoneId.systemDefault()); System.out.println(current); System.out.println(current.toLocalDateTime());
I’m expecting the “converted time” to be 17:00 but I’m having this result.
2020-01-01T15:00Z[UTC] 2020-01-01T16:00+01:00[Europe/Rome] 2020-01-01T16:00
Thanks
Advertisement
Answer
I’m expecting the “converted time” to be 17:00 but I’m having this result.
I’m not sure why you’re expecting that. 16:00 is the correct answer.
Perhaps you missed that this is in januari, when mainland europe is on winter time which is just 1 hour ahead of UTC.
15:00:00 UTC time on jan 1st means it’s +1 hour in Rome. There’s no way to get 17:00:00 here. Try this with a summer date, perhaps. That’ll get you a 2 hour difference.
There’s nothing wrong your code. That is exactly how to do it, which is why it gives you 16:00:00 (as that is the right answer).