Java ZonedDateTime calculate start and end date time across time zone

Description

Java ZonedDateTime calculate start and end date time across time zone

import java.time.LocalDateTime;
import java.time.Month;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.zone.ZoneRules;

public class Main {
  public static void main(String[] args) {
    LocalDateTime startRental = LocalDateTime.of(2015, Month.DECEMBER, 13, 13, 00);
    ZoneId startZone = ZoneId.of("US/Eastern");

    LocalDateTime endRental = LocalDateTime.of(2015, Month.DECEMBER, 18, 10, 00);
    ZoneId endZone = ZoneId.of("US/Mountain");

    ZonedDateTime beginTrip = ZonedDateTime.of(startRental, startZone);
    System.out.println("Trip Begins: " + beginTrip);

    // Get the rules of the check out time zone
    ZoneRules checkOutZoneRules = startZone.getRules();
    System.out.println("Checkout Time Zone Rules: " + checkOutZoneRules);

    // If the trip took 4 days
    ZonedDateTime beginPlus = beginTrip.plusDays(4);
    System.out.println("Four Days Later: " + beginPlus);

    // End of trip in starting time zone
    ZonedDateTime endTripOriginalZone = ZonedDateTime.of(endRental, endZone);
    ZonedDateTime endTrip = ZonedDateTime.of(endRental, endZone);
    int diff = endTripOriginalZone.compareTo(endTrip);
    String diffStr = (diff >= 0) ? "NO" : "YES";

    System.out.println("End trip date/time in original zone: " + endTripOriginalZone);
    System.out.println("End trip date/time in check-in zone: " + endTrip);
    System.out.println("Original Zone Time is less than new zone time? " + diffStr);

    ZoneId checkOutZoneId = beginTrip.getZone();
    ZoneOffset checkOutOffset = beginTrip.getOffset();
    ZoneId checkInZoneId = endTrip.getZone();
    ZoneOffset checkInOffset = endTrip.getOffset();

    System.out.println("Check out zone and offset: " + checkOutZoneId + checkOutOffset);
    System.out.println("Check in zone and offset: " + checkInZoneId + checkInOffset);

  }//from   w  w w. jav  a 2  s  . co m
}



PreviousNext

Related