Java Data Type How to - Count the duration between date time in two timezones








Question

We would like to know how to count the duration between date time in two timezones.

Answer

/*from  w  w w . j  a v a  2s. c o  m*/
import java.time.Duration;
import java.time.ZoneId;
import java.time.ZonedDateTime;

public class Main {
  public static void main(String[] argv) {
    ZonedDateTime here = ZonedDateTime.now(ZoneId.of("America/Los_Angeles"));
    ZonedDateTime gmtNewYear = ZonedDateTime.of(2014, 12, 31, 23, 59, 59, 0, ZoneId.of("Europe/London"));

    Duration d = Duration.between(here, gmtNewYear);
    System.out.println(d);
  }
}

The code above generates the following result.