By Pierre Templier – @ptemplier
Java 8 includes a brand new time management API named java.time that was specified by the JSR 310 and implemented by the threeten project.
There are three time management APIs in Java 8:
This new API intends to resolve issues that plagued the two first ones:
java.time was inspired by Joda-Time and was mostly written by its author, Stephen Colebourne.
The new API was built from scratch within Java 8 to avoid carrying over Joda-time issues:
java.time aims to have a simpler and more robust design than Joda-Time. This API is also available in Java 7 through a backport available at the central maven. The fluid API allows the manipulation of immutable and thread-safe objects in order to avoid errors. The time management classes are organized according to two views of time: machine time and human time. To make the transition less painful, it is easy to convert java.util.Date and java.util.GregorianCalendar objects to and from the new API.
A precise point in time.
The Instant class represents the number of nanoseconds since Epoch (January 1st 1970). It is very similar to java.util.Date.
Instant.ofEpochSecond(1395100800);
Instant.parse("2014-03-18T00:00:00.000Z");
Duration between two instants or duration in days/hours/minutes/seconds/milliseconds/nanoseconds.
Duration.ofDays(5);
Duration.of(5, ChronoUnit.DAYS);
Duration.between(Instant.parse("2011-07-28T00:00:00.000Z"),
Instant.parse("2014-03-18T00:00:00.000Z")).getSeconds();
Use Timezone to create ZonedDateTime and OffsetDateTime from an instant through the ZoneId and ZoneOffset classes.
ZonedDateTime and OffsetDateTime are the equivalent of GregorianCalendar.
ZonedDateTime.ofInstant(Instant.now(), ZoneId.of("Europe/Paris"));
OffsetDateTime.ofInstant(Instant.now(), ZoneId.of("GMT+1"));
OffsetDateTime.ofInstant(Instant.now(), ZoneOffset.of("+01:00"));
Date and time (without the timezone concept).
LocalDateTime date = LocalDateTime.of(LocalDate.of(2014, Month.MARCH, 18), LocalTime.MIDNIGHT);
// formatage
date.format(DateTimeFormatter.ISO_DATE);
date.format(DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm"));
//parsing
LocalDateTime.parse("18/03/2014 00:00", DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm"));
// manipulation
date.withHour(16).plusWeeks(3); // Set time at 16:00 and we add 3 weeks
date.with(TemporalAdjusters.firstDayOfNextMonth()); // the first day of the next month
date.with(Temporals.nextWorkingDay()); // TemporalAdjuster in threeten-extra (not included in the jdk)
An incomplete LocalDateTime: 03/18 (March 18th), 12:00 (noon).
LocalDate.of(2014, Month.MARCH, 18);
LocalTime.of(12, 0); // same as LocalTime.NOON
DayOfWeek.of(2);// DayOfWeek.TUESDAY
Month.of(3); // same as Month.MARCH
MonthDay.of(Month.MARCH, 18);
YearMonth.of(2014, Month.MARCH);
Year.of(2014);
“Human” representation of a duration, for instance 1 month (whether it has 28 or 31 days).
Period.ofMonths(3);
// period between Java 7 and Java 8
Period.between(LocalDate.of(2011, Month.JULY, 28), LocalDate.of(2014, Month.MARCH, 18)); // 2 years, 7 months and 18 days
It is easy to convert an object to and from the new API.
Date date = new Date();
date.toInstant();
Date.from(Instant.now());
GregorianCalendar gregorianCalendar = new GregorianCalendar(TimeZone.getTimeZone("Europe/Paris"));
gregorianCalendar.toZonedDateTime();
gregorianCalendar.toInstant();
GregorianCalendar.from(ZonedDateTime.ofInstant(Instant.now(), ZoneId.of("Europe/Paris")));
As the code samples demonstrate, this new API corrects issues with previous APIs. It is easy to use thanks to its fluid syntax and intuitive concepts.
Some of the Joda-time features that have not made it into java.time are now in the threeten-extra project (available at maven central), and can be included in your projects.
One can hope this new API will be quickly integrated in the java frameworks developers use every day.