Java 8 understanding LocalDate and LocalTime

In this blog let’s explore the LocalDate and LocalTime.


LocalDate class is immutable and it doesn’t contain the time of day and also the time zone. It has a static factory method of() which we can use to create the instance:

LocalDate localDate = LocalDate.of(2017, 3, 18);
int year = localDate.getYear(); // 2017
Month month = localDate.getMonth(); // MARCH
int day = localDate.getDayOfMonth(); // 18
DayOfWeek dayOfWeek = localDate.getDayOfWeek();  // TUESDAY
int length = localDate.lengthOfMonth(); // 31 days in March
boolean leap = localDate.isLeapYear(); // false (not a leap year)

We can also get the current date with the now() method.

LocalDate now =;

We can also access LocalDate values using TemporalField interface. The TemporalField is an interface defining how to access the value of a specific field of a temporal object. For example the ChronoField is the implementation of this class.

int year = localDate.get(ChronoField.YEAR);
int month = localDate.get(ChronoField.MONTH_OF_YEAR);
int day = localDate.get(ChronoField.DAY_OF_MONTH);
int secondOfMinute = localDate.get(ChronoField.SECOND_OF_MINUTE);


For the time of day we have LocalTime class which has three overloaded static methods of(). So for example:

LocalTime localTime1 = LocalTime.of(13, 12); // hour, minute
LocalTime localTime2 = LocalTime.of(13, 12, 10); // hour, minute, second
LocalTime localTime3 = LocalTime.of(13, 12, 10, 3); // hour, minute, second, nanosecond

So the LocalTime class provides some getter methods to access the values, for example:

LocalTime time = LocalTime.of(11, 20, 12);
int hour = time.getHour();
int minute = time.getMinute();
int second = time.getSecond();
int nano = time.getNano();

The LocalDate and the LocalTime we can create by passing the string representation, for example:

LocalDate date = LocalDate.parse("2017-03-12");
LocalTime time = LocalTime.parse("14:23:12");