javazoneddatetime

How to convert ZonedDateTime to LocalDateTime?


I want to convert local date and time to UTC and then back to local date and time, I could not find solutions on internet. I used below code:

// get local date and time
LocalDateTime localDateTime = LocalDateTime.now();
// print local date time
System.out.println(localDateTime);
// get system time zone / zone id
ZoneId zoneId = ZoneId.systemDefault();
// get UTC time with zone offset
ZonedDateTime zonedDateTime = LocalDateTime.now(ZoneOffset.UTC).atZone(zoneId);
System.out.println(zonedDateTime);
// now I want to convert the UTC with timezone back to local time, 
// but below code is not working  
System.out.println(zonedDateTime.toLocalDateTime());

result expected

local date and time = 2023-08-01T17:15:10.832796200  
zoned date and time = 2023-08-01T11:45:10.832796200+05:30[Asia/Calcutta]  
local date and time after conversion = 2023-08-01T17:15:10.832796200

Solution

  • ZonedDateTime zonedDateTime = LocalDateTime.now(ZoneOffset.UTC).atZone(zoneId);

    This is suspect code. This code says:

    What time is it right now in the UTC zone. So, here in Amsterdam it's 15:46, but if I were to run that (we're in summer time right now, amsterdam has a +2 offset), LocalDateTime.now(ZoneOffset.UTC) gives me a local DT object with local time 13:46. It doesn't have a timezone, because.. it's local date time.

    You then 'place' that time (of 13:46) in a zone. Let's say zoneId is Europe/Amsterdam, this code gets me a time that happened 2 hours ago. It doesn't make any sense, you would never want to write this code.

    What you presumably want is:

    LocalDateTime nowAtUtc = LocalDateTime.now(ZoneOffset.UTC);
    ZonedDateTime zoned = nowAtUtc.atZone(ZoneOffset.UTC);
    ZonedDateTime inAmsterdam = zoned.atZoneSameInstant(ZoneId.of("Europe/Amsterdam"));
    System.out.println(inAmsterdam.toLocalDateTime());
    

    the above code prints the current time as per my clock here in The Netherlands.

    Of course, that exact snippet is pointless - why convert from A to B and right back again? Ordinarily you'd just go LocalDateTime.now() if you want an LDT, or if you want a ZDT, simply ZonedDateTime.now(zoneYouAreInterestedIn);. However, I assume you asked the question because you do a bunch of stuff in between that you elided from the question.