首页 > 编程语言 >【Java】其他时间日期API:ZonedDateTime / ZoneId、Duration、Period、TemporalAdjuster

【Java】其他时间日期API:ZonedDateTime / ZoneId、Duration、Period、TemporalAdjuster

时间:2022-12-06 16:22:48浏览次数:43  
标签:12 Java System Period LocalDateTime println ZoneId LocalDate out

1.带时区的日期时间:ZoneId / ZonedDateTime

(1)ZoneId

    public void test1(){
        Set<String> zoneIds = ZoneId.getAvailableZoneIds();  //获取所有时区
        for (String zoneId : zoneIds) {
            System.out.println(zoneId);  //America/Cuiaba…………
        }
        System.out.println();

        LocalDateTime ldt = LocalDateTime.now(ZoneId.of("Asia/Tokyo"));  //指定时区对应时间
        System.out.println(ldt);  //2022-12-06T16:29:43.650
    }

(2)ZonedDateTime

带时区的日期时间

    public void test2(){
        ZonedDateTime zdt = ZonedDateTime.now();  //获取本时区对象
        System.out.println(zdt);  //2022-12-06T15:27:41.957+08:00[Asia/Shanghai]

        ZonedDateTime zdt1 = ZonedDateTime.now(ZoneId.of("Asia/Tokyo"));  //指定时区
        System.out.println(zdt1);  //2022-12-06T16:27:41.958+09:00[Asia/Tokyo]
    }

2.时间间隔Duration

用于计算两个时间间隔,以秒和纳秒为基准。

方法 描述
between(Temporal start,Temporal end) 静态方法,返回Duration对象,表示两个时间的间隔
getNano()、getSeconds 返回时间间隔的纳秒数、返回时间间隔的秒数
toDays()、toHours()、toMinutes()、toMillis()、toNanos() 返回时间间隔期间的天数、小时数、分钟数、毫秒数、纳秒数
    public void test3(){
        LocalTime time1 = LocalTime.now();
        LocalTime time2 = LocalTime.of(15, 23, 32);

        Duration duration = Duration.between(time1, time2);
        System.out.println(duration);  //PT-23M-48.466S
        System.out.println(duration.getSeconds());   //-1429
        System.out.println(duration.getNano());  //534000000

        LocalDateTime dateTime1 = LocalDateTime.of(2016, 6, 12, 15, 23, 32);
        LocalDateTime dateTime2 = LocalDateTime.of(2017, 6, 12, 15, 23, 32);

        Duration duration1 = Duration.between(dateTime1, dateTime2);
        System.out.println(duration1.toDays());  //365
    }

3.日期间隔Period

用于计算两个
日期间隔,以年、月、日衡量。

方法 描述
between(LocalDate start,localDate end) 静态方法,返回Period对象,表示两个本地日期的间隔
getYears()、getMonths()、getDays() 返回此期间的年数、月数、天数
withYears(int years)、withMonths(int months)、withDays(int days) 返回设置间隔指定年、月、日数以后的Period对象
    public void test4(){
        LocalDate date1 = LocalDate.now();   //2022-12-06
        LocalDate date2 = LocalDate.of(2028, 3, 18);

        Period period = Period.between(date1, date2);
        System.out.println(period);    //P5Y3M12D
        System.out.println(period.getYears());   //5
        System.out.println(period.getMonths());   //3,单独看月份
        System.out.println(period.getDays());   //12

        Period period1 = period.withYears(2); //P2Y3M12D
        System.out.println(period1);
    }

4.日期时间校正器:TemporalAdjuster

public void test5(){
        //获取当前日期的下一个周日是哪天?
        TemporalAdjuster adjuster = TemporalAdjusters.next(DayOfWeek.SUNDAY);

        LocalDateTime dateTime = LocalDateTime.now().with(adjuster);
        System.out.println(dateTime);  //2022-12-11T16:08:44.137

        //获取下一个工作日是哪天?
        LocalDate localDate = LocalDate.now().with(new TemporalAdjuster() {
            @Override
            public Temporal adjustInto(Temporal temporal) {
                LocalDate date = (LocalDate) temporal;
                if (date.getDayOfWeek().equals(DayOfWeek.FRIDAY)) {
                    return date.plusDays(3);
                } else if (date.getDayOfWeek().equals(DayOfWeek.SATURDAY)) {
                    return date.plusDays(2);
                } else {
                    return date.plusDays(1);
                }
            }
        });
        System.out.println("下一个工作日是:"+localDate);  //下一个工作日是:2022-12-07
    }

标签:12,Java,System,Period,LocalDateTime,println,ZoneId,LocalDate,out
From: https://www.cnblogs.com/zhishu/p/16955636.html

相关文章