Java 时间函数ZonedDateTime LocalDate LocalTime LocalDateTime

 

If you want to convert a timestamp from one timezone to another, use withZoneSameInstant().

 withZoneSameLocal() will change the zone but keep all the other fields the same. The exception is where it would be an invalid date in that timezone.

 参考 https://stackoverflow.com/questions/55032725/what-is-the-difference-between-zoneddatetime-withzonesameinstant-and-zoneddateti

    var localDate = LocalDate.parse("2025-02-20");
    var localTime = LocalTime.parse("14:17:01");
    var zonedDateTime = ZonedDateTime.of(localDate, localTime, ZoneId.of("Asia/Seoul"));
    ZonedDateTime beijingDateTime = zonedDateTime.withZoneSameInstant(ZoneId.of("Asia/Shanghai"));

    System.out.println("zonedDateTime=" + zonedDateTime);
    System.out.println("beijing      =" + zonedDateTime.withZoneSameInstant(ZoneId.of("Asia/Shanghai")));
    System.out.println("b2           =" + zonedDateTime.withZoneSameLocal(ZoneId.of("Asia/Shanghai")););

    //print result is:
    //zonedDateTime=2025-02-20T14:17:01+09:00[Asia/Seoul]
    //beijing      =2025-02-20T13:17:01+08:00[Asia/Shanghai]
    //b2           =2025-02-20T14:17:01+08:00[Asia/Shanghai]

 

DayOfWeek dw = DaysOfWeek.get(day);  
ZonedDateTime.getDayOfMonth  the day-of-month, from 1 to 31
zonedDateTime.getMonth().length(localDate.isLeapYear()) February has 28 days in a standard year and 29 days in a leap year. April, June, September and November have 30 days. All other months have 31 days
ZonedDateTime.toLocalDate   toLocalTime  toLocalDateTime  
   LocalDate.plusMonths  plusYears  
  localDate.with(TemporalAdjusters.lastDayOfMonth()) return the last day of localDate
  localDate.with(TemporalAdjusters.firstDayOfMonth()) return the first day of localDate
  ZonedDateTime now = ZonedDateTime.now();   
  //DayOfWeek dw = DaysOfWeek.get(day);

 

posted @ 2025-02-20 15:28  cyj2024  阅读(26)  评论(0)    收藏  举报