Java Data Type How to - Convert date time in a timezone to another timezone


We would like to know how to convert date time in a timezone to another timezone.


import java.time.LocalDateTime;
import java.time.Month;
import java.time.ZoneId;
import java.time.ZonedDateTime;
//w ww. j ava 2  s  . c o  m
public class Main {

  public static void main(String[] args) {
    LocalDateTime localDateTime = LocalDateTime.of(1982, Month.APRIL, 17, 14, 11);
    ZonedDateTime jakartaTime = ZonedDateTime.of(localDateTime, ZoneId.of("Asia/Jakarta"));
    System.out.println(jakartaTime); //1982-04-17T14:11+07:00[Asia/Jakarta]

    System.out.println(jakartaTime.withZoneSameInstant(ZoneId.of("America/Los_Angeles"))); //1982-04-16T23:11-08:00[America/Los_Angeles]
    System.out.println(jakartaTime.withZoneSameLocal(ZoneId.of("America/New_York"))); //1982-04-17T14:11-05:00[America/New_York]


The code above generates the following result.