关于ChronoUnit工具类的几个小用法

jdk1.8给出的解释:

 常见用法:

@Test
public void testChromoUnitsPlus() {
    //Get the current date
    LocalDate today = LocalDate.now();
    System.out.println("Current date: " + today);
    //add 1 week to the current date
    LocalDate nextWeek = today.plus(1, ChronoUnit.WEEKS);
    System.out.println("Next week: " + nextWeek);
    //add 1 month to the current date
    LocalDate nextMonth = today.plus(1, ChronoUnit.MONTHS);
    System.out.println("Next month: " + nextMonth);
    //add 1 year to the current date
    LocalDate nextYear = today.plus(1, ChronoUnit.YEARS);
    System.out.println("Next year: " + nextYear);
    //add 10 years to the current date
    LocalDate nextDecade = today.plus(1, ChronoUnit.DECADES);
    System.out.println("Date after ten year: " + nextDecade);
}

输出:

Current date: 2021-12-09
Next week: 2021-12-16
Next month: 2022-01-09
Next year: 2022-12-09
Date after ten year: 2031-12-09

between用法

int lastDateTime = (int) ChronoUnit.HOURS.between(createTime, localDateTime);
//between取createTime和localDateTime间的差值,返回值为long,这里转化了一下

 

posted @ 2021-12-09 11:37  Tzero  阅读(284)  评论(0)    收藏  举报
Live2D