1. Overview

In this tutorial, we’ll find out the options of working with date and time in Scala. Then, we’ll see the advantages and disadvantages of each approach using the help of relevant examples.

2. Date and Time in Scala

Scala is a JVM-based language, therefore, it brings along all the goodness and the issues of the JDK Java libraries. We’ll go through some of the most important ones based on their popularity and functionality.

Keep in mind that native date-time libraries are not available in Scala. However, there are some helper libraries in Scala for our convenience.

3. Scala Libraries

There are plenty of Java libraries available for us. But, unfortunately, none of those options gives an idiomatic way of writing date-time computations in Scala. This motivated the origins of certain wrapper libraries around the popular Java libraries such as Joda-Time.

These libraries combine the power of the underlying Java library with the elegance of Scala’s expressive coding style.

3.1. Scala-Time and Nscala-Time

Scala-Time and its newer version, Nscala-Time, are wrapper libraries around Joda-Time. These wrappers provide several implants to improve the expressiveness of code. It also provides operators for date-time arithmetic.

Let’s see a quick example of calculating the elapsed duration of a long-running process:

val processStart:DateTime = DateTime.now()
val processEnd:DateTime = processStart + (1.hours + 10.minutes + 5.seconds)
val elapsed:Interval = processStart to processEnd
println(elapsed.toPeriod.toString(PeriodFormat.getDefault))   // Prints "1 hour, 10 minutes and 5 seconds"

4. Java Libraries

4.1. Java-Time

As part of the Java 8 release, a new comprehensive date-time library called Java-Time was introduced in the JDK. This was part of Project ThreeTen. All classes are designed to be immutable and thread-safe by default.

The design of the APIs is inspired by Joda-Time. As a result, all the functionalities of Joda-Time are also available in Java-Time. This helps developers to transition from Joda-Time to Java-Time smoothly.

Let’s see how we can parse a date string to get the day of the week:

val dateStr = "2021-06-13"
val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd")
val localDate = LocalDate.parse(dateStr, formatter)
println(localDate.getDayOfWeek) // prints "SUNDAY"

4.2. Joda-Time

The issues with standard date-time classes in JDK resulted in the creation of many other third-party libraries. Joda-Time is the most popular among them. Immutability is built into the library by default. It offers a lot of other functionalities, too, including multiple calendar systems, support for timezones, and parsing.

Joda-Time has a highly stable API with very few issues reported. Its high reliability and dependability made it the library of choice for date-time calculations.

Let’s use Joda-Time to parse a date string and get the day-of-week value:

val dateStr = "2021-06-13"
val formatter = DateTimeFormat.forPattern("yyyy-MM-dd")
val dateTime:DateTime = formatter.parseDateTime(dateStr)
println(dateTime.dayOfWeek().getAsText(Locale.getDefault()))  // Prints "Sunday"

java.util.Date is the default date-time library available in JDK until version 1.8. However, it has drawn a lot of criticism from the developers due to several issues.

Out of those, the major ones are that the objects are mutable and not thread-safe. Regardless of its disadvantages, we can see its use in many legacy applications. There are many ways to get around these issues and safely use this library.

5. Conclusion

In this short tutorial, we’ve evaluated the different options for working with date-time values in Scala. First, we’ve seen the limitations of using java.util.Date. After that, we saw the advantages of using the new java.time library.

Finally, we looked at the Joda-Time library and its Scala wrapper, nscala-time. In conclusion, for new projects, we can choose nscala-time if we prefer the idiomatic Scala style of coding. Otherwise, we’ll choose java.time if we prefer less overhead and require high performance.

As usual, the full source code can be found over on GitHub.

Comments are closed on this article!