JDK8 三种时间获取方法 日期加减

        //2018-12-04
        LocalDate localDate = LocalDate.now();

        //16:14:56.892
        LocalTime localTime = LocalTime.now();

        //2018-12-04T16:14:56.892
        LocalDateTime localDateTime = LocalDateTime.now();

        //16:14:5(去除纳秒)
        LocalTime time = LocalTime.now().withNano(0);
        
        //2018-12-04 16:17:55
        String now1 = LocalDateTime.now().withNano(0).toString().replace("T", " ");
        String now2 = LocalDate.now()+" "+LocalTime.now().withNano(0);

日期加减

//三天后
LocalDate afterThreeDays = LocalDate.now().plusDays(3);

//三天前
LocalDate threeDaysAgo = LocalDate.now().minusDays(3);

LocalDate now = LocalDate.now();
LocalDate date = LocalDate.parse("2020-01-30");
long day1 = now.toEpochDay();
long day2 = date.toEpochDay();
//相差多少天
long day = day2-day1;

 

相关推荐
©️2020 CSDN 皮肤主题: Age of Ai 设计师:meimeiellie 返回首页