Sunday, May 17, 2015

Exploring java.time package

Forget Java.Date and Java.Calendar,
try Java.Time!

Source Code

//生年月日
LocalDate bf=LocalDate.of(1992,12,16);
LocalDate rf=LocalDate.of(2015,12,19);
Clock c=Clock.systemUTC();
Clock d=Clock.systemDefaultZone();
LocalDate n=LocalDate.now();//c or d
dump("Current Instant : "+Instant.now());
dump("Current Instant as per System UTC: "+Instant.now(c));
dump("Current Instant as per System Default Timezone : "+Instant.now(d));
dump("Birthdate -> "+bf);
dump("DoM : " +bf.getDayOfMonth()+" of "+bf.lengthOfMonth()+"-day long "+bf.getMonth());
dump("DoY : "+bf.getDayOfYear()+" of "+bf.lengthOfYear()+"-day long "+bf.getYear());
dump("Today -> "+n);
Period yet=bf.until(n);//Duration equivalent in terms of Days
//年齢
dump("Age : -> "+yet.getYears()+" years "+yet.getMonths()+" months "+yet.getDays()+" days");
dump("Today -> "+n);
dump("Flight -> "+rf);
Period more=n.until(rf);//Try Duration for time to sleep, go home or wake up.
dump("Return : -> "+more.getYears()+" years "+more.getMonths()+" months "+more.getDays()+" days");

Wet Run - Sample Output

Current Instant : 2015-05-18T07:55:55.178Z
Current Instant as per System UTC: 2015-05-18T07:55:55.192Z
Current Instant as per System Default Timezone : 2015-05-18T07:55:55.192Z
Birthdate -> 1992-12-16
DoM : 16 of 31-day long DECEMBER
DoY : 351 of 366-day long 1992
Today -> 2015-05-18
Age : -> 22 years 5 months 2 days
Today -> 2015-05-18
Flight -> 2015-12-19
Return : -> 0 years 7 months 1 days

No comments:

Post a Comment

Going one step further with Kotlin & gRPC

Recently, I tried using Quarkus with Kotlin for grpc. I have worked with grpc for communication between microservices in Java & Golang. ...