Java 17 Recipes
-13. Converting Dates and Times Based on he
Download 3.2 Mb. Pdf ko'rish
|
Java 17 Recipes
4-13. Converting Dates and Times Based on he
Time Zone Problem The application you are developing has the potential to be utilized throughout the world. In some areas of the application, static dates and times need to be displayed, rather than the system date and time. In such cases, those static dates and times need to be converted to suit the particular time zone in which the application user is currently residing. Solution 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 application works 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 ZoneIds for each. An airline could use this method to print time zone information for a particular flight. public static void scheduleReport(LocalDateTime checkOut, ZoneId checkOutZone, LocalDateTime checkIn, ZoneId checkInZone){ Chapter 4 Numbers aNd dates 134 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 ); 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); } Here is the result. Trip Begins: 2021-12-13T13:00-05:00[US/Eastern] Checkout Time Zone Rules: ZoneRules[currentStandardOffset=-05:00] Four Days Later: 2021-12-17T13:00-05:00[US/Eastern] Chapter 4 Numbers aNd dates 135 End trip date/time in original zone: 2021-12-18T10:00-05:00[US/Eastern] End trip date/time in check-in zone: 2021-12-18T10:00-07:00[US/Mountain] Original Zone Time is less than new zone time? YES Check out zone and offset: US/Eastern-05:00 Check in zone and offset: US/Mountain-07:00 Download 3.2 Mb. Do'stlaringiz bilan baham: |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling