Java LocalDateTime With Examples

Java LocalDateTime class is part of Java 8 Date API.

Java LocalDateTime

  1. Java LocalDateTime is an immutable class, so it’s thread safe and suitable to use in multithreaded environment.
  2. LocalDateTime provides date and time output in the format of YYYY-MM-DD-hh-mm-ss.zzz, extendable to nanosecond precision. So we can store “2017-11-10 21:30:45.123456789” in LocalDateTime object.
  3. java-localdatetime
  4. Just like the LocalDate class, LocalDateTime has no time zone data. We can use LocalDateTime instance for general purpose date and time information.
  5. LocalDateTime class implements Comparable, ChronoLocalDateTime, Temporal, TemporalAdjuster, TermporalAccessor and Serializable interfaces.
  6. LocalDateTime is a final class, so we can’t extend it.
  7. LocalDateTime is a value based class, so we should use equals() method to check if two instances of LocalDateTime are equal or not.
  8. LocalDateTime class is a synergistic ‘combination’ of LocalDate and LocalTime with the contatenated format as shown in the figure above.

Importing Java LocalDateTime Class

Java LocalDateTime class is part of java.time package. So we can import it like following:

Creating LocalDateTime instance

There are following ways to create LocalDateTime object.

  1. Invoking the static now() method that returns the current date-time from the system clock in the default time-zone.
  2. By passing year, month, day, hour, minute, second and nanosecond values to of() method. This method is overloaded and one of them takes argument at LocalDate and LocalTime.
  3. Using parse() method and passing date time string representation.
  4. Using ofInstant() by passing Instant and ZoneId information.

Retrieving Date Time Information from LocalDateTime instance

Let’s look into some of the LocalDateTime methods to retrieve date time information.

  1. getHour(): returns the int hour contained within the LocalDateTime object, from 0 to 23.
  2. getMinute(): returns the int minute information, from 0 to 59.
  3. getSecond(): returns the int second contained within the LocalDateTime object, from 0 to 59.
  4. getNano(): returns the int nanosecond from the LocalDateTime object, ranging from 0 to 999,999,999.

Apart from above, LocaDate class stock methods such as getYear(), getMonth(), getDayOfMonth(), getDayOfWeek(), getDayOfYear() are also applicable to LocalDateTime class. Let’s look at these methods through an example.

Java LocalDateTime Example

Java LocalDateTime Example, Java 8 LocalDateTime

Java LocalDateTime methods – plus and minus

LocalDateTime class has many methods to plus and minus date time components. Note that it returns a copy of the LocalDateTime object with the change, no change happens in the specified object. Some of these methods are:

  1. plusHours(), plusMinutes(), plusSeconds() and plusNanos() for adding time component in LocalDateTime object.
  2. minusHours(), minusMinutes(), minusSeconds() and minusNanos() for subtracting time component in LocalDateTime object.
  3. plusYears(), plusMonths(), plusWeeks() and plusDays() for adding date component in LocalDateTime object.
  4. minusYears(), minusMonths(), minusWeeks() and minusDays() for subtracting date component in LocalDateTime object.

All the above method take long argument, their names clearly suggests what they are used for.

Here is an example for LocalDateTime plus and minus methods.

Output of above program is:

As you can see that if the second and nano values are 0, then it’s not getting returned when we call LocalDateTime toString() method.

Java LocalDateTime to Date

We should avoid using legacy java.util.Date class, but if required then we can convert LocalDateTime to Date using below code.

Note that above output is because the LocalDateTime doesn’t have timezone information, so we need to provide timezone to offset and convert to local timezone.

That’s all for java LocalDateTime class.

Reference: API Doc

By admin

Leave a Reply

%d bloggers like this: