java chrono_Java8 ChronoUnits枚举

全屏

Java8 ChronoUnits枚举

java.time.temporal.ChronoUnit 枚举在 Java8 中添加,以取代旧的API用来代表日,月等整数值

让我们来看看他们的操作。

使用所选择的任何编辑器创建下面的java程序在 C:/> JAVA

Java8Tester.javaimport java.time.LocalDate;import java.time.temporal.ChronoUnit;public class Java8Tester {

public static void main(String args[]){

Java8Tester java8tester = new Java8Tester();

java8tester.testChromoUnits();

}

public void testChromoUnits(){

//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);

}}

验证结果

使用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

分享到:

0评论

14487a65ea137d8f9ac97cdce44a0324.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值