With the Java 8 date and time API, developers have easy access to dates, times, time zones, instants, durations, and periods. These features are available through an extensive list of classes in the java.time package.

In this tutorial, you’ll learn five useful features of the Java 8 date and time API.

1. Getting Your Local Time

The Java 8 date and time API comes with a class called LocalTime. This class provides a time in the hour-minute-second format.

To get your current time using this class, you’ll first have to create a new object of the LocalTime class. Then pass the object retrieved from the now() method to it. The newly created object will then store the current time of the program’s execution. This is because the now() method retrieves the current time from the system clock that is on your computer.

Using the LocalTime Class Example

The code above accomplishes several simple tasks using different LocalTime methods. Executing the code above produces the following output in the console.

2. Get Your Local Date

You can get your local date through the Java 8 date and time API, in much the same way as you retrieve your local time. The only difference is that you use the LocalDate class instead of the LocalTime class.

The LocalDate class uses the year-month-day structure to display the system date on your computer, with the help of the now() method. The LocalDate class provides access to over 50 different methods, but the ones that you might use more often are as follows.

now() (takes no arguments and returns the current date) getYear() (takes no arguments and returns the current year) getMonth() (takes no arguments and returns the current month) getDayOfWeek() (takes no arguments and returns the current day of the week) getDayOfMonth() (takes no arguments and returns the current day of the month) getDayOfYear() (takes no arguments and returns the current day of the year)

Using the LocalDate Class Example

The code above produces the following output in the console.

3. Check if the Current Year Is a Leap Year

Among the many methods in the LocalDate class is the isLeapYear() method. You can use this method to check if a given year is a leap year. To check the current year, you can create a new LocalDate object with the now() method, and use the isLeapYear() method on the newly created object.

Using the isLeapYear() method on a LocalDate object will return a boolean value, which will be true if the year is a leap year and false otherwise.

Using the isLeapYear() Method Example

The code above will produce the following output in the console.

4. Get Your Local Date and Time

There’s a class in the Java 8 date and time API called LocalDateTime. As its name suggests, you can use this class to get the date and time at your current location.

The class has several important methods, but the now() method might be the one that you want to get familiar with first. The now() method of the LocalDateTime class returns the date and time of your current location. It uses the year-month-day T hour:minutes:seconds structure; where the date appears first and a capital t separates it from the time.

Using the LocalDateTime Class Example

The code above produces the following output in the console.

As you can see from the output above, the LocalDateTime class can generate a date and time combination, as well as the individual values (such as the current month), that you can get from the LocalDate and LocalTime classes.

5. Get the Current Date and Time of a Location Using Its Time Zone

As you’ve seen so far, the Java 8 date and time API has many useful features. Another useful one is its ability to get the time zone of your current location, or the date and time at a location using its time zone.

Using the ZonedDateTime Class Example

A lot is happening in the code above. Like the previous classes, the ZoneDateTime class uses the now() method, which in this case returns an object with the current date, time, and time zone.

However, unlike the previous examples, the ZoneDateTime class takes a time zone from the user and returns the appropriate date and time.

The code above will produce the following output in the console.

As you can see from the output above, the GMT-03:00 time zone is two hours ahead of the GMT-05:00 time zone.

Learn More About APIs

In this tutorial, you’ve learned about five top features that come with the Java 8 date and time API. This list is by no means complete, and there are other features of the API that you can explore.

However, if you’re curious about how APIs work in general, there are resources to help you with that too.