Starting with JDK 8

Starting with JDK 8, SimpleDateFormat can be replaced with a new class—DateTimeFormatter. This is an immutable (and, therefore, thread-safe) class, and is used for printing and parsing date-time objects. This class supports everything from predefined formatters (represented as constants, as the ISO local date, 2011-12-03, is ISO_LOCAL_DATE) to user-defined formatters (relying on a set of symbols for writing custom format patterns).

Moreover, beside the Date class, JDK 8 comes with several new classes, which are dedicated to working with date and time. Some of these classes are shown in the following list (these are also referenced as temporals because they implement the Temporal interface):

  • LocalDate (date without a time zone in the ISO-8601 calendar system)
  • LocalTime (time without a time zone in the ISO-8601 calendar system)
  • LocalDateTime (date-time without a time zone in the ISO-8601 calendar system)
  • ZonedDateTime (date-time with a time zone in the ISO-8601 calendar system), and so on
  • OffsetDateTime (date-time with an offset from UTC/GMT in the ISO-8601 calendar system)
  • OffsetTime (time with an offset from UTC/GMT in the ISO-8601 calendar system)

In order to convert String to LocalDate via a predefined formatter, it should respect the DateTimeFormatter.ISO_LOCAL_DATE pattern, for example, 2020-06-01. LocalDate provides a parse() method that can be used as follows:

// 06 is the month, 01 is the day
LocalDate localDate = LocalDate.parse("2020-06-01");

Similarly, in the case of LocalTime, the string should respect the DateTimeFormatter.ISO_LOCAL_TIME pattern; for example, 10:15:30, as shown in the following code snippet:

LocalTime localTime = LocalTime.parse("12:23:44");

In the case of LocalDateTime, the string should respect the DateTimeFormatter.ISO_LOCAL_DATE_TIME pattern; for example, 2020-06-01T11:20:15, as shown in the following code snippet:

LocalDateTime localDateTime 
= LocalDateTime.parse("2020-06-01T11:20:15");

And, in case of ZonedDateTime, the string must respect the DateTimeFormatter.ISO_ZONED_DATE_TIME pattern; for example, 2020-06-01T10:15:30+09:00[Asia/Tokyo], as shown in the following code snippet:

ZonedDateTime zonedDateTime 
= ZonedDateTime.parse("2020-06-01T10:15:30+09:00[Asia/Tokyo]");

In the case of OffsetDateTime, the string must respect the DateTimeFormatter.ISO_OFFSET_DATE_TIME pattern; for example, 2007-12-03T10:15:30+01:00, as shown in the following code snippet:

OffsetDateTime offsetDateTime 
= OffsetDateTime.parse("2007-12-03T10:15:30+01:00");

Finally, in the case of OffsetTime, the string must respect the DateTimeFormatter.ISO_OFFSET_TIME pattern; for example, 10:15:30+01:00, as shown in the following code snippet:

OffsetTime offsetTime = OffsetTime.parse("10:15:30+01:00");

If the string doesn't respect any of the predefined formatters, then it is time for a user-defined formatter via a custom format pattern; for example, the string 01.06.2020 represents a date that needs a user-defined formatter, as follows:

DateTimeFormatter dateFormatter 
= DateTimeFormatter.ofPattern("dd.MM.yyyy");
LocalDate localDateFormatted
= LocalDate.parse("01.06.2020", dateFormatter);

However, a string such as 12|23|44 requires a user-defined formatter as follows:

DateTimeFormatter timeFormatter 
= DateTimeFormatter.ofPattern("HH|mm|ss");
LocalTime localTimeFormatted
= LocalTime.parse("12|23|44", timeFormatter);

A string such as 01.06.2020, 11:20:15 requires a user-defined formatter as follows:

DateTimeFormatter dateTimeFormatter 
= DateTimeFormatter.ofPattern("dd.MM.yyyy, HH:mm:ss");
LocalDateTime localDateTimeFormatted
= LocalDateTime.parse("01.06.2020, 11:20:15", dateTimeFormatter);

A string such as 01.06.2020, 11:20:15+09:00 [Asia/Tokyo] requires a user-defined formatter as follows:

DateTimeFormatter zonedDateTimeFormatter 
= DateTimeFormatter.ofPattern("dd.MM.yyyy, HH:mm:ssXXXXX '['VV']'");
ZonedDateTime zonedDateTimeFormatted
= ZonedDateTime.parse("01.06.2020, 11:20:15+09:00 [Asia/Tokyo]",
zonedDateTimeFormatter);

A string such as 2007.12.03, 10:15:30, +01:00 requires a user-defined formatter as follows:

DateTimeFormatter offsetDateTimeFormatter 
= DateTimeFormatter.ofPattern("yyyy.MM.dd, HH:mm:ss, XXXXX");
OffsetDateTime offsetDateTimeFormatted
= OffsetDateTime.parse("2007.12.03, 10:15:30, +01:00",
offsetDateTimeFormatter);

Finally, a string such as 10 15 30 +01:00 requires a user-defined formatter as follows:

DateTimeFormatter offsetTimeFormatter 
= DateTimeFormatter.ofPattern("HH mm ss XXXXX");
OffsetTime offsetTimeFormatted
= OffsetTime.parse("10 15 30 +01:00", offsetTimeFormatter);
Each ofPattern() method from the previous examples also supports Locale.

Converting from LocalDate, LocalDateTime, or ZonedDateTime to String can be accomplished in at least two ways:

  • Rely on the LocalDate, LocalDateTime, or ZonedDateTime.toString() method (automatically or explicitly). Notice that relying on toString() will always print the date via the corresponding predefined formatter:
// 2020-06-01 results in ISO_LOCAL_DATE, 2020-06-01
String localDateAsString = localDate.toString();

// 01.06.2020 results in ISO_LOCAL_DATE, 2020-06-01
String localDateAsString = localDateFormatted.toString();

// 2020-06-01T11:20:15 results
// in ISO_LOCAL_DATE_TIME, 2020-06-01T11:20:15
String localDateTimeAsString = localDateTime.toString();

// 01.06.2020, 11:20:15 results in
// ISO_LOCAL_DATE_TIME, 2020-06-01T11:20:15
String localDateTimeAsString
= localDateTimeFormatted.toString();

// 2020-06-01T10:15:30+09:00[Asia/Tokyo]
// results in ISO_ZONED_DATE_TIME,
// 2020-06-01T11:20:15+09:00[Asia/Tokyo]
String zonedDateTimeAsString = zonedDateTime.toString();

// 01.06.2020, 11:20:15+09:00 [Asia/Tokyo]
// results in ISO_ZONED_DATE_TIME,
// 2020-06-01T11:20:15+09:00[Asia/Tokyo]
String zonedDateTimeAsString
= zonedDateTimeFormatted.toString();
  • Rely on the DateTimeFormatter.format() method. Notice that relying on DateTimeFormatter.format() will always print the date/time using the specified formatter (by default, the time zone will be null), as follows:
// 01.06.2020
String localDateAsFormattedString
= dateFormatter.format(localDateFormatted);

// 01.06.2020, 11:20:15
String localDateTimeAsFormattedString
= dateTimeFormatter.format(localDateTimeFormatted);

// 01.06.2020, 11:20:15+09:00 [Asia/Tokyo]
String zonedDateTimeAsFormattedString
= zonedDateTimeFormatted.format(zonedDateTimeFormatter);

Adding an explicit time zone into the discussion can be done as follows:

DateTimeFormatter zonedDateTimeFormatter 
= DateTimeFormatter.ofPattern("dd.MM.yyyy, HH:mm:ssXXXXX '['VV']'")
.withZone(ZoneId.of("Europe/Paris"));
ZonedDateTime zonedDateTimeFormatted
= ZonedDateTime.parse("01.06.2020, 11:20:15+09:00 [Asia/Tokyo]",
zonedDateTimeFormatter);

This time, the string represents the date/time in the Europe/Paris time zone:

// 01.06.2020, 04:20:15+02:00 [Europe/Paris]
String zonedDateTimeAsFormattedString
= zonedDateTimeFormatted.format(zonedDateTimeFormatter);
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset