Saturday, April 28, 2012

How to control time with JSR 310


Are you enough with java.util.Date and Calendar? Is relatively hard to use and does not satisfy your “hunger” for time control? If the answer is “yes”, then it is time to take a closer look to JSR 310 which is a set of classes for Java 7 which are a revamp of Java's built-in Date and Calendar classes. In the series of beta releases of Java 7, this API is not bundled yet, therefore if you want to play with it you need to have in your classpath the JAR from here: http://sourceforge.net/projects/threeten/files/threeten/.

Some other resources of JSR 310 are:


One of my favorite things is the Clock class that acts as a facade for accessing the current time. You can access current time like this:

Clock clock = Clock.system(TimeZone.UTC);

And now, you can “play” with many methods of the Clock class, as you can see below (get time, day, yesterday, tomorrow, year and so on):

Year year = clock.year();
YearMonth yearMonth = clock.yearMonth();
LocalDate today = clock.today();
LocalDate yesterday = clock.yesterday();
LocalDate tomorrow = clock.tomorrow();
LocalTime time = clock.time();
LocalTime timeToMinute = clock.timeToMinute();
LocalTime timeToSecond = clock.timeToSecond();

If we send this to System.out,

System.out.println("Year: " + year);
System.out.println("Year Month: " + yearMonth);
System.out.println("Today: " + today);
System.out.println("Yesterday: " + yesterday);
System.out.println("Tomorrow: " + tomorrow);
System.out.println("Time: " + time);
System.out.println("Time to minute: " + timeToMinute);
System.out.println("Time to second: " + timeToSecond);

We get:

Year: Year=2011
Year Month: 2011-03
Today: 2011-03-31
Yesterday: 2011-03-30
Tomorrow: 2011-04-01
Time: 08:08:04.812
Time to minute: 08:08
Time to second: 08:08:04

From NetBeans, here it is the set of Clock methods (they are pretty intuitive):

Class name Description
Clock - A facade class for accessing the current time (overview above).
LocalDate - Local date with no time or time zone.
LocalTime - Time with no date or time zone.
ZonedDateTime - Calendar information, often viewed as
year-month-day-hour-minute-second-zone.
LocalDateTime - Local date and time with no time zone.
Instant - Instantaneous point in time with nanosecond precision.
Period - Period consisting of the standard year, month, day, hour, minute,
second and nanosecond units.

Well, here are same methods of LocalDate class (so many and so gooooood):

And, some of LocalTime:

These great methods can be use to accomplished many delicate tasks, like:

• Define time periods – one year period:

Period one_year = Period.ofYears(1);
System.out.println(one_year.getYears());

Output: 1

• Instant time:

Instant instant = clock.instant();
System.out.println(instant);

Output: 2011-03-31T16:25:34.752Z

• Format time:

LocalDate today = clock.today();
DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder().appendValue(CopticChronology.dayOfMonthRule(), 2).appendLiteral('-').appendText(CopticChronology.monthOfYearRule(), DateTimeFormatterBuilder.TextStyle.SHORT).appendLiteral('-').appendText(CopticChronology.yearRule());
System.out.println(builder.toFormatter().print(today));

Output: 22-7-1727

And, there are so many to explore!

No comments:

Post a Comment