Java 8 Date - LocalDate, LocalDateTime, Instant With Examples

Java 8 Date Time API is one of the most sought after change for developers. Java has been missing a consistent approach for Date and Time from start and Java 8 Date Time API is a welcome addition to the core Java APIs.

Before we start looking at the Java 8 Date Time API, let’s see why do we need a new API for this. There have been several problems with the existing date and time related classes in java, some of them are:

There are some other issues with the methods defined in Date and Calendar classes but above problems make it clear that a robust Date Time API was needed in Java. That’s why Joda Time played a key role as a quality replacement for Java Date Time requirements.

Java 8 Date Time API is JSR-310 implementation. It is designed to overcome all the flaws in the legacy date time implementations. Some of the design principles of new Date Time API are:

Java 8 Date Time API consists of following packages.

We have looked into most of the important parts of Java Date Time API. It’s time now to look into most important classes of Date Time API with examples.

  • LocalDate

    LocalDate is an immutable class that represents Date with default format of yyyy-MM-dd. We can use now() method to get the current date. We can also provide input arguments for year, month and date to create LocalDate instance. This class provides overloaded method for now() where we can pass ZoneId for getting date in specific time zone. This class provides the same functionality as java.sql.Date. Let’s look at a simple example for it’s usage.

    LocalDate methods explanation is provided in comments, when we run this program, we get following output.

  • LocalTime

    LocalTime is an immutable class whose instance represents a time in the human readable format. It’s default format is hh:mm:ss.zzz. Just like LocalDate, this class provides time zone support and creating instance by passing hour, minute and second as input arguments. Let’s look at it’s usage with a simple program.

    When we run above program for LocalTime examples, we get following output.

  • LocalDateTime

    LocalDateTime is an immutable date-time object that represents a date-time, with default format as yyyy-MM-dd-HH-mm-ss.zzz. It provides a factory method that takes LocalDate and LocalTime input arguments to create LocalDateTime instance. Let’s look it’s usage with a simple example.

    In all the three examples, we have seen that if we provide invalid arguments for creating Date/Time, then it throws java.time.DateTimeException that is a RuntimeException, so we don’t need to explicitly catch it.

    We have also seen that we can get Date/Time data by passing ZoneId, you can get the list of supported ZoneId values from it’s javadoc. When we run above class, we get following output.

  • Instant

    Instant class is used to work with machine readable time format, it stores date time in unix timestamp. Let’s see it’s usage with a simple program.

    Output of above program is:

  • Java 8 Date API Utilities

    As mentioned earlier, most of the Date Time principle classes provide various utility methods such as plus/minus days, weeks, months etc. There are some other utility methods for adjusting the date using TemporalAdjuster and to calculate the period between two dates.

    Output of above program is:

  • Java 8 Date Parsing and Formatting

    It’s very common to format date into different formats and then parse a String to get the Date Time objects. Let’s see it with simple examples.

    When we run above program, we get following output.

  • Java 8 Date API Legacy Date Time Support

    Legacy Date/Time classes are used in almost all the applications, so having backward compatibility is a must. That’s why there are several utility methods through which we can convert Legacy classes to new classes and vice versa. Let’s see this with a simple example.

    When we run above application, we get following output.

    As you can see that legacy TimeZone and GregorianCalendar classes toString() methods are too verbose and not user friendly.

That’s all for Java 8 Date Time API, I like this new API a lot. Some of the most used classes will be LocalDate and LocalDateTime for this new API. It’s very easy to work with and having similar methods that does a particular job makes it easy to find. It will take some time from moving legacy classes to new Date Time classes, but I believe it will be worthy of the time.

By admin

Leave a Reply

%d bloggers like this: