Ajitav Dutta Ajitav Dutta - 5 months ago 17
Java Question

Not able to convert IST [Asia/Calcutta] to AET [Australia/Sydney] using ZoneDateTime in Java8

I am trying to convert a LocalDateTime which is in IST [Asia/Calcutta] to AET [Australia/Sydney] using

. Below is what I did:

LocalDateTime ldt = LocalDateTime.now();
ZoneDateTime zdt = ldt.atZone(ZoneId.of("Australia/Sydney"));

System.out.println( ldt.toString() + " -- " + ZoneDateTime.now(ZoneId.systemDefault()));
System.out.printlb( zdt + " -- " + ZoneDateTime.now(ZoneId.of("Australia/Sydney")));

The above code gives me the following result:

2016-09-23T22:53:55.790 -- 2016-09-23T22:53:55.790+05:30[Asia/Calcutta]
2016-09-23T22:57:50.855+10:00[Australia/Sydney] -- 2016-09-24T03:27:50.854+10:00[Australia/Sydney]

As you can see from the above result that
gives me different date an Time.

Can anyone help me in resolving this issue.


LocalDateTime doesn't have a time zone, so when you call atZone(), it's interpreted as being in the zone you're applying:

This returns a ZonedDateTime formed from this date-time at the specified time-zone.

To actually convert the time, it needs to know the original zone.

ZonedDateTime zdt1 = ZonedDateTime.now(ZoneId.of("Asia/Calcutta"));
ZonedDateTime zdt2 = zdt1.withZoneSameInstant(ZoneId.of("Australia/Sydney"));
System.out.println(zdt1); // prints: 2016-09-23T23:10:00.678+05:30[Asia/Calcutta]
System.out.println(zdt2); // prints: 2016-09-24T03:40:00.678+10:00[Australia/Sydney]