scala 日期处理
https://github.com/nscala-time/nscala-time
For processing dates, the Java Joda Time project is popular and well documented. A project named nscala-time implements a Scala wrapper around Joda Time, and lets you write date expressions in a more Scala-like way, including these examples:
DateTime. now // returns org.joda.time.DateTime
DateTime. now + 2. months
DateTime. nextMonth < DateTime. now + 2. months
(2. hours + 45. minutes + 10. seconds). millis
如何引入依赖
<dependency>
<groupId>com.github.nscala-time</groupId>
<artifactId>nscala-time_2.10</artifactId>
<version>2.0.0</version>
</dependency>
代码示例
import com.github.nscala_time.time.Imports._
object HelloWorld {
def main(args: Array[String]) {
println(DateTime.now + 2.months) // returns org.joda.time.DateTime = 2009-06-27T13:25:59.195-07:00
println(DateTime.nextMonth < DateTime.now + 2.months) // returns Boolean = true
println(DateTime.now to DateTime.tomorrow) // return org.joda.time.Interval = > 2009-04-27T13:47:14.840/2009-04-28T13:47:14.840
println((DateTime.now to DateTime.nextSecond).millis) // returns Long = 1000
println((2.hours + 45.minutes + 10.seconds))
// returns com.github.nscala_time.time.DurationBuilder
// (can be used as a Duration or as a Period)
println((2.hours + 45.minutes + 10.seconds).millis)
// returns Long = 9910000
println(2.months + 3.days)
// returns Period
}
}