原文:http://www.yiibai.com/java8/java8_chronounits.html
java.time.temporal.ChronoUnit 枚举在 Java8 中添加,以取代旧的API用来代表日,月等整数值
让我们来看看他们的操作。
使用所选择的任何编辑器创建下面的java程序在 C:/> JAVA
Java8Tester.java
importjava.time.LocalDate;importjava.time.temporal.ChronoUnit;publicclassJava8Tester{publicstaticvoidmain(Stringargs[]){Java8Testerjava8tester =newJava8Tester();java8tester.testChromoUnits();}publicvoidtestChromoUnits(){//Get the current dateLocalDatetoday =LocalDate.now();System.out.println("Current date: "+today);//add 1 week to the current dateLocalDatenextWeek =today.plus(1,ChronoUnit.WEEKS);System.out.println("Next week: "+nextWeek);//add 1 month to the current dateLocalDatenextMonth =today.plus(1,ChronoUnit.MONTHS);System.out.println("Next month: "+nextMonth);//add 1 year to the current dateLocalDatenextYear =today.plus(1,ChronoUnit.YEARS);System.out.println("Next year: "+nextYear);//add 10 years to the current dateLocalDatenextDecade =today.plus(1,ChronoUnit.DECADES);System.out.println("Date after ten year: "+nextDecade);}}
验证结果
使用javac编译器编译如下类
C:\JAVA>javac Java8Tester.java
现在运行Java8Tester看到的结果
C:\JAVA>java Java8Tester
看到结果
Current date: 2014-12-10
Next week: 2014-12-17
Next month: 2015-01-10
Next year: 2015-12-10
Date after ten year: 2024-12-10