常用类-LocalDate、LocalTime、LocalDateTime

详情解释请看下方代码区

点击查看代码
 @Test
    public void test1(){
        //实例化:now()获取当前日期、时间、日期 + 时间
        LocalDate localDate = LocalDate.now();
        LocalTime localTime = LocalTime.now();
        LocalDateTime localDateTime =  LocalDateTime.now();

        System.out.println(localDate);
        System.out.println(localTime);
        System.out.println(localDateTime);
        //of():设置指定的年、月、日、时、分、秒没有偏移量
        System.out.println("===================");
        LocalDateTime localDateTime1 = LocalDateTime.of(2020, 10, 23, 6, 23, 43);
        System.out.println(localDateTime1);

        //getXxx()
        System.out.println("===================");
        System.out.println(localDateTime.getDayOfMonth());//获取当前这一天是这一月的第几天
        System.out.println(localDateTime.getDayOfWeek());//获取当前这一天是这一周的星期几
        System.out.println(localDateTime.getMonth());//获取当前月的英文值
        System.out.println(localDateTime.getMonthValue());//获取当前月的数值
        System.out.println(localDateTime.getMinute());//获取当时时间的分钟数
        System.out.println("===================");
        //体现不可变性:修改日期的值不会对日期本身造成影响
        //withXxx():设置相关的属性
        LocalDateTime localDateTime2 = localDateTime.withDayOfMonth(22);
        System.out.println(localDateTime2);//修改当前日期数之后的日期
        System.out.println(localDateTime);//修改之前的日期
        System.out.println("===================");
        LocalDateTime localDateTime3 = localDateTime.withHour(14);
        System.out.println(localDateTime3);//修改当前的小时之后的值
        System.out.println(localDateTime);//修改之前的值
        System.out.println("===================");
        //
        LocalDateTime localDateTime4 = localDateTime.plusMonths(3);
        System.out.println(localDateTime4);//输出在当前月的基础上加上三个月之后的值
        System.out.println(localDateTime);
        LocalDateTime localDateTime5 = localDateTime.minusDays(6);
        System.out.println(localDateTime5);//输出在当前天数的基础上减去6天之后的值
        System.out.println(localDateTime);


    }
运行结果图

posted @ 2022-07-11 19:49  剑断青丝ii  阅读(97)  评论(0编辑  收藏  举报