166

What's the standard way to work with dates and times in Scala? Should I use Java types such as java.util.Date or there are native Scala alternatives?

michael.kebe
  • 10,916
  • 3
  • 44
  • 62
Ivan
  • 63,011
  • 101
  • 250
  • 382

5 Answers5

133

From Java SE 8 onwards, users are asked to migrate to java.time (JSR-310). There are efforts on creating scala libraries wrapping java.time for scala such as scala-time. If targeting lower than SE 8 use one of the below. Also see Why JSR-310 isn't Joda-Time

Awesome scala lists many of the popular Scala DateTime apis


A new Scala wrapper for Joda Time. This project forked from scala-time since it seems that scala-time is no longer maintained.

import com.github.nscala_time.time.Imports._

DateTime.now // returns org.joda.time.DateTime = 2009-04-27T13:25:42.659-07:00

DateTime.now.hour(2).minute(45).second(10) // returns org.joda.time.DateTime = 2009-04-27T02:45:10.313-07:00

DateTime.now + 2.months // returns org.joda.time.DateTime = 2009-06-27T13:25:59.195-07:00

DateTime.nextMonth < DateTime.now + 2.months // returns Boolean = true

DateTime.now to DateTime.tomorrow  // return org.joda.time.Interval = > 2009-04-27T13:47:14.840/2009-04-28T13:47:14.840

(DateTime.now to DateTime.nextSecond).millis // returns Long = 1000

2.hours + 45.minutes + 10.seconds
// returns com.github.nscala_time.time.DurationBuilder
// (can be used as a Duration or as a Period)

(2.hours + 45.minutes + 10.seconds).millis
// returns Long = 9910000

2.months + 3.days
// returns Period

Joda Time is a good Java library, there is a Scala wrapper / implicit conversion library avaliable for Joda Time at scala-time created by Jorge Ortiz. (Note implicits have a performance hit, but it depends on what you do if you will notice. And if you run into a performance problem you can just revert to the Joda interface)

From the README:

USAGE:
  import org.scala_tools.time.Imports._

  DateTime.now
  // returns org.joda.time.DateTime = 2009-04-27T13:25:42.659-07:00

  DateTime.now.hour(2).minute(45).second(10)
  // returns org.joda.time.DateTime = 2009-04-27T02:45:10.313-07:00

  DateTime.now + 2.months
  // returns org.joda.time.DateTime = 2009-06-27T13:25:59.195-07:00

  DateTime.nextMonth < DateTime.now + 2.months
  // returns Boolean = true
  DateTime.now to DateTime.tomorrow
  // return org.joda.time.Interval =
  //   2009-04-27T13:47:14.840/2009-04-28T13:47:14.840

  (DateTime.now to DateTime.nextSecond).millis
  // returns Long = 1000
    
  2.hours + 45.minutes + 10.seconds
  // returns org.scala_tools.time.DurationBuilder
  // (can be used as a Duration or as a Period)
    
  (2.hours + 45.minutes + 10.seconds).millis
  // returns Long = 9910000 
    
  2.months + 3.days
  // returns Period
oluies
  • 17,694
  • 14
  • 74
  • 117
  • 4
    Video scalaj: Idiomatic Scala Wrappers for Java Libraries http://days2010.scala-lang.org/node/138/164 – oluies Sep 01 '10 at 01:48
  • Thanks, I haven't know about this wrapper. – Vadim Shender Sep 01 '10 at 02:36
  • I can't get a formatted output from this. If I use DateTime.formatted("yyyyMMdd") I just get plain "yyyyMMdd" (letters not replaced with corresponding numbers) as response. If I use DateTime.formatted(DateTimeFormat.forPattern("yyyyMMdd")) - I get an error asking for a string argument. – Ivan Sep 02 '10 at 22:59
  • http://stackoverflow.com/questions/3639944/how-to-substract-a-day-hour-minute-from-joda-time-datetime-in-scala – oluies Sep 03 '10 at 23:56
  • 1
    Since it seems scala-time is no longer maintained, it has been superseded by nscala-time (which in contrast to scala-time works with scala 2.10). It can be found at https://github.com/nscala-time/nscala-time. – Sebastian Ganslandt Mar 06 '13 at 20:16
  • 1
    Can't figure out the problem with their `Imports` and `scala.concurrent.duration._`. Used plain JodaTime instead and wrote own bicycles over it. – kisileno Feb 18 '14 at 20:32
13

If you are using Java 8, then there is no need to use nscala anymore. The Joda-Time library has been moved into Java 8 under the java.time package (JSR-310). Just import that package into your Scala project.

8

There is no standard way to work with dates in Scala. The options available are:

  1. Use java.time (if you are using Java 8) since it has the best of JODA time built into it. No implicits.
  2. Use nscala-time.
  3. Lamma date library (relatively new library on the scene)

I would avoid using java.util.Date due to the well-documented issues surrounding it.

5

MOTIVATION:

The Java Date and Calendar libraries are largely inadequate. They are mutable, not thread-safe, and very inconvenient to use.

The Joda Time library is a great replacement for Java's Date and Calendar classes. They're immutable by default, have a much richer and nicer API, and can easily be converted to Java's Date and Calendar classes when necessary.

This project provides a thin layer of convenience around the Joda Time libraries, making them more idiomatic to use within Scala.

(copied from https://github.com/jorgeortiz85/scala-time)

Pavel Sklenak
  • 59
  • 1
  • 1
3

Everyone uses JodaTime, these Scala helper/wrapper libraries may need re-compilation with new versions of Scala. Jodatime is the only time library that's been around for a long time, and is stable and works reliably with every version of Scala.

irishjava
  • 503
  • 4
  • 11