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