Java - Convert a datetime in one time zone to a datetime in another time zone.

Introduction

It is like asking the date and time in India when it is May 14, 2012 16:30 in Chicago.

Demo

import java.time.LocalDateTime;
import java.time.Month;
import java.time.ZoneId;
import java.time.ZonedDateTime;

public class Main {
  public static void main(String[] args) {
    LocalDateTime ldt = LocalDateTime.of(2012, Month.MAY, 14, 16, 30);

    ZoneId usCentral = ZoneId.of("America/Chicago");
    ZonedDateTime zdt = ZonedDateTime.of(ldt, usCentral);
    System.out.println("In US Central Time Zone:" + zdt);

    ZoneId asiaKolkata = ZoneId.of("Asia/Kolkata");
    ZonedDateTime zdt2 = zdt.withZoneSameInstant(asiaKolkata);
    System.out.println("In Asia/Kolkata Time Zone:" + zdt2);

    ZonedDateTime zdt3 = zdt.withZoneSameInstant(ZoneId.of("Z"));
    System.out.println("In UTC Time Zone:" + zdt3);
  }//from w  ww  . j  a v a  2 s  .c  o m
}

Result

Related Topic