Java ZonedDateTime calculate arriving time across time zone

Description

Java ZonedDateTime calculate arriving time across time zone

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

public class Main {
  public static void main(String[] args) {
    DateTimeFormatter format = DateTimeFormatter.ofPattern("MMM d yyyy  hh:mm a");

    LocalDateTime leaving = LocalDateTime.of(2020, Month.JULY, 20, 19, 30);
    ZoneId leavingZone = ZoneId.of("America/Los_Angeles");
    ZonedDateTime departure = ZonedDateTime.of(leaving, leavingZone);

    String out1 = departure.format(format);
    System.out.printf("LEAVING:  %s (%s)%n", out1, leavingZone);

    // Flight is 10 hours and 50 minutes, or 650 minutes
    ZoneId arrivingZone = ZoneId.of("Asia/Tokyo");
    ZonedDateTime arrival = departure.withZoneSameInstant(arrivingZone).plusMinutes(650);

    String out2 = arrival.format(format);
    System.out.printf("ARRIVING: %s (%s)%n", out2, arrivingZone);

  }/*from   w w w . jav  a2  s. c o m*/
}



PreviousNext

Related