Convert a date time in one time zone to another time zone
Description
How to find out the time in Los Angeles when it is June 21 14, 2014 16:30 in Chicago.
We can use the withZoneSameInstant(ZoneId newZoneId) method of the ZonedDateTime class.
Example
import java.time.LocalDateTime;
import java.time.Month;
import java.time.ZoneId;
import java.time.ZonedDateTime;
/*from ww w.ja va2 s. c o m*/
public class Main {
public static void main(String[] args) {
LocalDateTime ldt = LocalDateTime.of(2014, Month.JUNE, 21, 16, 30);
ZoneId usCentral = ZoneId.of("America/Chicago");
ZonedDateTime zdt = ZonedDateTime.of(ldt, usCentral);
System.out.println("In US Central Time Zone:" + zdt);
ZoneId losAngeles = ZoneId.of("America/Los_Angeles");
ZonedDateTime zdt2 = zdt.withZoneSameInstant(losAngeles);
System.out.println("In America/Los_Angeles Time Zone:" + zdt2);
}
}
The code above generates the following result.