Prashanth Babu

Ramblings on Hadoop Ecosytem, Java, etc.

Java8 Date and Time: Parsing and formatting

With Java v8, we have new API for Date and Time, which looks pretty elegant and also is immutable and hence thread-safe.

Please check java.time package summary added in Java v8 for detailed info of all the classes present in this package. Also checking official Java Tutorial Trail for Data Time really helps understand the API better.

This API is based on / inspired by the wonderful Joda-Time, which was the de facto standard date and time library for Java prior to Java SE 8.

In this blogpost, we will discuss how to parse and format dates with this new API. At a new very high-level, there are 4 classes related to handling dates in java.time package.

  • LocalDate holds just the date and does not store or represent either time or time-zone. Example: 2016-05-09

  • LocalTime holds just the time and does not store or represent either date or time-zone. Example: 22:04:43.658

  • LocalDateTime is a combination of both LocalDate and LocalTime which holds the date and time; but does not hold the time-zone information. Example: 2016-05-09T22:06:25.599

  • ZonedDateTime is LocalDateTime and also includes the time-zone information too. Example: 2016-05-09T22:09:34.711+01:00[Europe/London]

And for formatting, we will use DateTimeFormatter of java.time.format package. Please refer the Java API docs for more info on this class. There are a number of predefined formatters which are needed for parsing and formatting.

For brevity, programming language syntax chosen for examples here is Scala.

Parsing a date

If we need to parse a date in the format: Mon May 09 22:20:58 +0100 2016 and make it a Java object, we can do the following:

Parse a date String to a Java Object
1
2
3
4
5
6
7
8
9
10
import java.time.{LocalDateTime, ZonedDateTime}
import java.time.format.DateTimeFormatter

val date = "Mon May 09 22:20:58 +0100 2016"

val formatter = DateTimeFormatter.ofPattern("EE MMM dd HH:mm:ss ZZ yyyy")
val parseDate = LocalDateTime.parse(date, formatter)
  // parseDate: java.time.LocalDateTime = 2016-05-09T22:20:58
val parseDateTZ = ZonedDateTime.parse(date, formatter)
  // parseDateTZ:  java.time.ZonedDateTime = 2016-05-09T22:20:58+01:00

Formatting a date with time-zone info

If the time-zone is present in the format string, we can not use LocalDateTime and should instead use ZonedDateTime. And when we have a Java Date object and would like to format it to a String which includes time-zone specific info also, we can do the following:

Parse a date with TZ to a String
1
2
3
4
5
6
7
8
9
10
import java.time.ZonedDateTime
import java.time.format.DateTimeFormatter

val formatter = DateTimeFormatter.ofPattern("EE MMM dd HH:mm:ss.SSS ZZ yyyy")
val now = ZonedDateTime.now()
  // now: java.time.ZonedDateTime = 2016-05-09T22:38:38.289+01:00[Europe/London]
val formattedDate1 = formatter.format(now)
  // formattedDate1: String = Mon May 09 22:38:38.289 +0100 2016
val formattedDate2 = now.format(formatter)
  // formattedDate2: String = Mon May 09 22:38:38.289 +0100 2016

Formatting a date without time-zone info

If we don’t need time-zone specific information for formatting a date, we can use LocalDateTime class.

Parse a date without TZ to a String
1
2
3
4
5
6
7
8
9
10
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter

val formatter = DateTimeFormatter.ofPattern("EE MMM dd HH:mm:ss.SSS yyyy")
val now = LocalDateTime.now()
  // now: java.time.LocalDateTime = 2016-05-09T22:42:15.125
val formattedDate1 = formatter.format(now)
  // formattedDate1: String = Mon May 09 22:42:15.125 2016
val formattedDate2 = now.format(formatter)
  // formattedDate2: String = Mon May 09 22:42:15.125 2016

Hope this post gave a quick understanding of Java8 Date and Time parsing and formatting. Happy learning!