score: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
score: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.
score: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)
score:8
There is no standard way to work with dates in Scala. The options available are:
- Use java.time (if you are using Java 8) since it has the best of JODA time built into it. No implicits.
- Use nscala-time.
- Lamma date library (relatively new library on the scene)
I would avoid using java.util.Date due to the well-documented issues surrounding it.
score: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.
Source: stackoverflow.com
Related Query
- What's the standard way to work with dates and times in Scala? Should I use Java types or there are native Scala alternatives?
- What's the right Scala and Java collection combination to use with nested JAXB?
- What is the right way to work with slick's 3.0.0 streaming results and Postgresql?
- Java or Scala fast way to parse dates with many different formats using java.time
- Is there a way to call a function defined using `val` in Scala with the whole curly brace block as an argument and not the final result of that block?
- Scala 2.12 and Travis.ci - how to exclude the combination with Java 6?
- Does Play Framework with the Scala plugin support MVC and the Java API?
- Is there a straightforward and general way to wrap a scala object's code with code to be executed before and after the object body code?
- scala -> use .net (linq) and java code base in the same program?
- What's the simplest way to use SSE with Redis pub/sub and Akka Streams?
- Is there a way to print the full error message in Scala with the use of try/catch?
- How can I use the Scala REPL to test java code - java and scala REPL giving different outputs
- How to use scala list and java list in the same file in scala code base?
- Is there a way to prohibit delimiting by ', ' and just delimit the csv file with ',' in scala
- How can I use scala sources from a different location in a sbt project and also make it work with IntelliJ IDEA?
- How do I specify the java version to use with the Ammonite Scala shell on Windows?
- How to use the Akka sample cluster kubernetes with Scala and minikube?
- Can I use Java reflection to get the value of a member that has been added with a Scala macro annotation?
- I'm new to spark,trying to generate the decision tree model in scala and use that model in java to predict.How to load that model in java?
- is there a way to write coalesce query to remove null from a file and use in scala code with a filter after extracting from a file?
- What is the formal difference in Scala between braces and parentheses, and when should they be used?
- What are the differences and similarties between Scala traits vs. Java 8 interfaces?
- When and why should one use Applicative Functors in Scala
- Should I use Unit or leave out the return type for my scala method?
- What's the new way to iterate over a Java Map in Scala 2.8.0?
- How to use IntelliJ with Play Framework and Scala
- Configuration data in Scala -- should I use the Reader monad?
- Which functionality/feature in Scala only exists as a concession to the underlying platform and should be removed if targeting something else?
- What does the @elidable annotation do in Scala, and when should I use it?
- What are the differences between a Scala Future and a Java Future
More Query from same tag
- What is the meaning of tilde arrow in this context?
- How to calculate hour to day in NetCDf file using scala
- How to initialize a specific random seed in scala breeze, say for Gaussian distribution?
- Scala Spray Routing - two HTTP method directives executed for single request
- Convert JSON from a URL to dataframe (Pyspark and Scala)
- Scala custom collection returns list of null as default values
- Simple concurrency with an Akka Hello World sample
- Actorpublisher as source in handleMessagesWithSinkSource
- Creating Map values in Spark using Scala
- Scala trait syntax
- Load file with schema information and dynamically apply to data file using Spark
- Scala - using parametrized type inside parametrized type
- How to create a DataFrame from List?
- scala - Passing a function that takes another function as a parameter
- What's the Scala syntax for a function taking any subtype of Ordered[A]?
- Scala Type mismatch when a generic type operates on the same generic type
- How do I "get" a Scala case object from Java?
- Parse Json array response in scala\Play
- Understanding the implementation of Y-Combinator
- What is `alsoTo` analogue of akka-streams in monix ?
- Calling Scala from Groovy: How to handle different collection types?
- Call a REST API of a module controller in a play application
- How to use the SublimeSBT plugin?
- How to split List in Scala?
- Java Garbage Collection Behavior for Many Short-Lived Objects
- Spark - groupByKey map-side aggregation
- how to convert Range.Inclusive to NumericRange.Inclusive[Char] type?
- Aggregation on a Dataset with composite keys
- Parameterized trait mixins
- How to implement a delayed future without java.util.Timer?