JoBins採用企業サイト JoBinsエージェントサイト

アカウントをお持ちの方はこちら 採用企業ログイン
エージェントログイン


background

Date APIs in Java 8 Onward

Jun 26, 2019

Why the need of new Date APIs?

Date APIs prior to Java 8 were really frustrating, especially while one needed to deal with different timezones, and those APIs were not really readable to the developers, most of the time developers ended up looking inside the methods and the definitions to figure out its purpose. Therefore, the need of new stuff to rescue the new developers and the existing ones (they may need to change whole lot of codes) to some extent.

The Basics

There are three classes which can be used to cover date and time representations.

  • LocalDate: Can be used to represent date only.
  • LocalTime: Can be used to represent time only.
  • LocalDateTime: Can be used to represent both date and time.

Example


LocalDate localDate = LocalDate.now(); //For the current date
LocalTime localTime = LocalTime.now(); //For the current time
LocalDateTime localDateTime = LocalDateTime.now(); //For the current date and time

All these three classes have a static method called now() that can be used to get the current date or time or both according to one’s need. And, the code is much more readable.
To create a date, there is another static method, look the example below.


public static LocalDate of(int year, Month month, int dayOfMonth)
LocalDate localDate = LocalDate.of(2019, Month.JUNE, 21);

To create a specific time, one can use the of() method of LocalTime class. There are three versions of of() method which supports hours, minute, second and nanosecond. Look at the following example and method definition.


public static LocalTime of(int hour, int minute, int second, int nanosecond)
public static LocalTime of(int hour, int minute, int second)
public static LocalTime of(int hour, int minute)
LocalTime localTime = LocalTime.of(10, 15);

Now, date and time can be combined to create DateTime. There are many versions of constructors for DateTime, some example are given below.


public static LocalDateTime of(LocalDate localDate, LocalTime localTime)
LocalDateTime localDateTime = new LocalDateTime(localDate, localTime)//From above
public static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute)
LocalDateTime localDateTime = new (2019, Month.JUNE, 24, 7, 30);

Date and Time Manipulation

Manipulation of dates and times have always been difficult prior to Java 8, now with these new APIs look at the following examples that demonstrate how easy it is to perform different operations with date and time.


LocalDate localDate = new LocalDate(2019, Month.JUNE, 24);
localDate = localDate.plusDays(2);//To add 2 days
localDate = localDate.plusMonths(1);//To add 1 month
localDate = localDate.plusYears(2);//To add 2 years

Similar methods for Time are also available. The methods do what they say, and that what readability means which was not the case with Date APIs prior to Java 8.

Parsing Dates and Times

There is a method call parse() which can be used to convert string to date.


DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(“MM dd yyyy”);
LocalDate localDate = LocalDate.parse(“06 24 2019”, dateTimeFormatter);