Because I am constantly busy working on something, I have never had time to actually put everything in words and pictures. But, since you got here, then you must have already seen some part of my work - and this is the way I’m talking.I'm 23, born in Romania, student at UPG Romania in software development field. I started from 0, mostly with basic stuff, and I’m evolving every day to an expert. I'm focused on freelancing projects, from small websites, to really heavy stuff. I know that I look and act differently from most developers, but this is why you will love to work with me! Constantin has posted 42 posts at DZone. You can read more from them at their website. View Full User Profile

How to control time with JSR 310

04.01.2011
| 6637 views |
  • submit to reddit

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:

http://jcp.org/en/jsr/summary?id=310
http://sourceforge.net/apps/mediawiki/threeten/index.php?title=ThreeTen
http://groovycookbook.org/basic_types/dates_times_jsr310/

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!

 

From http://e-blog-java.blogspot.com/2011/03/how-to-control-time-with-jsr-310.html

Published at DZone with permission of its author, Constantin Alin.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags:

Comments

Mark Thornton replied on Fri, 2011/04/01 - 2:23am

The bad news is that JSR-310 didn't make the cut for Java 7 and is now scheduled for Java 8.

Aries McRae replied on Fri, 2011/04/01 - 3:32am

Definitely not april fools.

http://www.jcp.org/en/jsr/detail?id=336

JSR 336: JavaTM SE 7 Release Contents
=================================
Consideration of the following JSRs, while of interest to many in the Java community, will most likely be deferred to future Java SE releases:

  • JSR 260: Javadoc Tag Technology Update
  • JSR 295: Beans Binding
  • JSR 296: Swing Application Framework
  • JSR 305: Annotations for Software Defect Detection
  • JSR 308: Annotations on Java Types
  • JSR 310: Date and Time API

matt inger replied on Sun, 2011/04/03 - 2:45pm

It's basically derived from Joda Time anyway, so delaying to Java 8 isn't a big deal. Most people who need proper date/time management aren't using the built in classes I wouldn't think.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.