集册 Java实例教程 根据时区转换日期和时间

根据时区转换日期和时间

欢马劈雪     最近更新时间:2020-01-02 10:19:05

676
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
下表列出了时区类。

import java.time.LocalDateTime;

import java.time.Month;/** from 时 代 J a v a**/

import java.time.ZoneId;

import java.time.ZoneOffset;

import java.time.ZonedDateTime;

import java.time.zone.ZoneRules;


public class Main {


  public static void report(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);
/**N o w  J a v a  . c o m**/

    // 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);


  }


  public static 
展开阅读全文