【Date Localdate LocalDateTime】


LocalDate

获取当前日期

 @Test
    public void test01(){
        LocalDate now = LocalDate.now();
        System.out.println(now);
        
    }

输出结果

2022-04-17

根据参数设置日期,参数分别为年、月、日

 @Test
    public void test02(){
    LocalDate localDate = LocalDate.of(1983,11,22);
        System.out.println(localDate);
    }

输出结果

1983-11-22

获取相应时间:get

@Test
    public void test03(){
        LocalDate now = LocalDate.now();
        System.out.println(now.getDayOfYear());
        System.out.println(now.getDayOfMonth());
        System.out.println(now.getDayOfWeek());

        //获取当前日期所在月份的数值
        System.out.println(now.getMonth());
        System.out.println(now.getMonthValue());

        //获取当前日期所在月份有多少天
        System.out.println(now.lengthOfMonth());

        //获取当前日期所在年份有多少天
        System.out.println(now.lengthOfYear());

        //获取当天日期所在年是否是闰年
        System.out.println(now.isLeapYear());
    }

输出结果

107
17
SUNDAY
APRIL
4
30
365
false

更改时间:with

   @Test
    public void test04(){
    //with开头的方法,我的理解是将参数替换localDate中的对应属性,重新计算得到的新日期。
        LocalDate now = LocalDate.now();
        System.out.println(now);
        //将参数中的“日”替换成localDate中的“日”
        System.out.println(now.withDayOfMonth(7));
        //将参数中的天数替换成localDate中的天数(一年中的第几天)
        System.out.println(now.withDayOfYear(45));
        System.out.println(now.withYear(1983));
        System.out.println(now.withYear(1983).withMonth(11).withDayOfMonth(22));
    }

输出结果

2022-04-17
2022-04-07
2022-02-14
1983-04-17
1983-11-22

将当前时间往前推,到过去时间:minus

    @Test
    public void test05() {
        LocalDate now = LocalDate.now();
        System.out.println(now);
        //将当前日期减一天
        System.out.println(now.minusDays(1));
        //将当前日期减一周
        System.out.println(now.minusWeeks(1));
        //将当前日期减一月
        System.out.println(now.minusMonths(1));
        //将当前日期减一年
        System.out.println(now.minusYears(1));
        System.out.println(now.minusYears(1).minusMonths(1).minusWeeks(1).minusDays(1));
    }

输出结果

2022-04-17
2022-04-16
2022-04-10
2022-03-17
2021-04-17
2021-03-09

将当前时间往后推,到未来时间:plus

  @Test
        public void test06(){
            LocalDate now = LocalDate.now();
            System.out.println(now);
            //将当前日期加一天
            System.out.println(now.plusDays(1));
            //将当前日期加一周
            System.out.println(now.plusWeeks(1));
            //将当前日期加一月
            System.out.println(now.plusMonths(1));
            //将当前日期加一年
            System.out.println(now.plusYears(1));
            System.out.println(now.plusYears(1).plusMonths(1).plusWeeks(1).plusDays(1));
        }
    }

输出结果

2022-04-17
2022-04-18
2022-04-24
2022-05-17
2023-04-17
2023-05-25

时间比较:isBefore、isAfter

   @Test
        public void test07(){
            LocalDate date01 = LocalDate.of(1983,11,22);
            LocalDate date02 = LocalDate.of(1988,8,11);
            System.out.println(date01.isBefore(date02));

        }

输出结果

true


版权声明:本文为m0_67250699原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。