Java8 Date Time API

The main improvements over previous classes are Thread safety and ease of use.


Represents a date in ISO format (yyyy-MM-dd) without time.

// Declaration
final LocalDate ld1 =;
final LocalDate ld2 = LocalDate.of(2015, 02, 20);
final LocalDate ld3 = LocalDate.parse("2015-02-20");
// Add and substract units  
final LocalDate today =;
final LocalDate tomorrow = today.plusDays(1);
final LocalDate future =, ChronoUnits.CENTURIES);  
final LocalDate past = today.minus(1, ChronoUnits.ERAS);

// Utility methods
final LocalDate now =;
final DayOfWeek dow = now.getDayOfWeek();
final boolean isLeapYear = now.isLeapYear();

// compare days
final LocalDate yesterday =;
final boolean isBefore = yesterday.isBefore(now);


Represents a time, without date.

// Declaration
final LocalTime lt1 =;
final LocalTime lt2 = LocalTime.parse("06:30");
final LocalTime lt3 = LocalTime.of(06, 30);

// Add and substract  
final LocalTime future =;

// Utilities
final LocalTime max = LocalTime.MAX;


It’s a combination of date and time. This is the most commonly used class.

// Declaration
final LocalDateTime ldt1 =;
final LocalDateTime ldt2 = LocalDateTime.of(2015, Month.FEBRUARY, 20, 06, 30);
final LocalDateTime ldt3 = LocalDateTime.parse("2015-02-20T06:30:00");

// It has methods from the previous 2.
final LocalDateTime future1 = ldt1.plusDays(1);
final LocalDateTime future2 = ldt1.plusHours(5);

ZonedDateTime & OffsetDateTime

Used when we need to deal with specific time zones. It uses a LocalDateTime + ZoneId for ZonedDateTime or LocalDateTime + ZoneOffset for OffsetDateTime.

// ZonedDateTime.
final LocalDateTime now =;
final ZoneId zone = ZoneId.of("Europe/Berlin");
final ZonedDateTime zdt = ZonedDateTime.of(now, zone);

// OffsetDateTime
final ZoneOffset zo = ZoneOffset.of("+02:00");
final OffsetDateTime odt = OffsetDateTime.of(now, zo);

Period & Duration

Period represents the time in years, months and days, passed between two LocalDates

final LocalDate now =;
final LocalDate future = now.plusDays(5);  

final Period period = Period.between(now, future);
final int daysPassed = period.getDays();

Duration represents the time in seconds and nano-seconds from a LocalTime.

final LocalTime now =;
final LocalTime past = now.minusMinutes(50);

final Duration dur = Duration.between(now, past);
final int nanos = dur.getNano();