Java8 日期时间

绿林寻猫
2021-12-08 / 0 评论 / 65 阅读 / 正在检测是否收录...

    @Test
    public void test(){
        testLocalDateTime();
    }

    public void testLocalDateTime(){

        // 获取当前的日期时间
        LocalDateTime currentTime = LocalDateTime.now();
        System.out.println("当前时间: " + currentTime);//当前时间: 2019-11-01T15:21:42.281

        LocalDate date1 = currentTime.toLocalDate();
        System.out.println("date1: " + date1);//date1: 2019-11-01

        Month month = currentTime.getMonth();
        int day = currentTime.getDayOfMonth();
        int seconds = currentTime.getSecond();

        System.out.println("月: " + month +", 日: " + day +", 秒: " + seconds);//月: NOVEMBER, 日: 1, 秒: 42

        LocalDateTime date2 = currentTime.withDayOfMonth(10).withYear(2012);
        System.out.println("date2: " + date2);//2012-11-10T15:21:42.281

        // 12 december 2014
        LocalDate date3 = LocalDate.of(2014, Month.DECEMBER, 12);
        System.out.println("date3: " + date3);//2014-12-12

        // 22 小时 15 分钟
        LocalTime date4 = LocalTime.of(22, 15);
        System.out.println("date4: " + date4);//22:15

        // 解析字符串
        LocalTime date5 = LocalTime.parse("20:15:30");
        System.out.println("date5: " + date5);//20:15:30
    }

 

0

评论 (0)

取消