Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
896 views
in Technique[技术] by (71.8m points)

datetime - 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 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...