码迷,mamicode.com
首页 > 其他好文 > 详细

scala处理日期

时间:2015-08-03 17:05:16      阅读:401      评论:0      收藏:0      [点我收藏+]

标签:

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
  }
}


scala处理日期

标签:

原文地址:http://my.oschina.net/cloudcoder/blog/487146

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!