欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java8中的时间处理5 - 日期工具

程序员文章站 2022-03-01 12:36:42
...

接下来看一下工具类的操作:

        LocalDate today = LocalDate.now();
        System.out.println(today.getYear()+"年是闰年吗? "+today.isLeapYear());
        System.out.println(today.isBefore(LocalDate.of(2015,1,1)));
        System.out.println(today.atTime(LocalTime.now()));

        System.out.println("10天后 "+today.plusDays(10));
        System.out.println("3周后 "+today.plusWeeks(3));
        System.out.println("20个月后 "+today.plusMonths(20));

        System.out.println("10天前 "+today.minusDays(10));
        System.out.println("3周前 "+today.minusWeeks(3));
        System.out.println("20月前 "+today.minusMonths(20));

        System.out.println("月初是"+today.with(TemporalAdjusters.firstDayOfMonth()));
        LocalDate lastDayOfYear = today.with(TemporalAdjusters.lastDayOfYear());
        System.out.println("年末是"+lastDayOfYear);

        Period period = today.until(lastDayOfYear);
        System.out.println("到年底是 "+period);
        System.out.println("还剩几个月 "+period.getMonths());