Java Reference
In-Depth Information
The Date-Time API provides the proper utilities for working with time zone data via
the Time Zone and Offset classes. In the following scenario, suppose that the ap-
plication is working with reservations for rental vehicles. You could rent a vehicle in
one time zone and return it in another. The following lines of code demonstrate how to
print out an individual's reservation in such a scenario. The following method, named
scheduleReport , accepts LocalDateTime objects representing check-in and
check-out date/time, along with ZoneId s for each. This method could be used by an
airline to print time-zone information for a particular flight.
public static void scheduleReport(LocalDateTime checkOut,
ZoneId checkOutZone,
LocalDateTime checkIn, ZoneId
checkInZone){
ZonedDateTime beginTrip = ZonedDateTime.of(checkOut,
checkOutZone);
System.out.println("Trip Begins: " + beginTrip);
// Get the rules of the check out time zone
ZoneRules checkOutZoneRules = checkOutZone.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(checkIn, checkOutZone);
ZonedDateTime endTrip = ZonedDateTime.of(checkIn,
checkInZone);
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 );
Search WWH ::




Custom Search