传统的java对于时间的操作是用Date类和Calender类
但是使用很麻烦
scala有一个包装库叫NScalaTime,他提供了JodaTime的一些语法。
需要在maven库中下载nscala的jar包
然后import com.github.nscala_time.time.Imports._
scala> val dt1=new DateTime(2014,9,4,9,0)
warning: Class org.joda.convert.ToString not found - continuing with a stub.
warning: Class org.joda.convert.ToString not found - continuing with a stub.
dt1: org.joda.time.DateTime = 2014-09-04T09:00:00.000+08:00
scala> dt1.dayOfYear.get
res0: Int = 247
scala> val dt2=new DateTime(2014,10,31,15,0)
dt2: org.joda.time.DateTime = 2014-10-31T15:00:00.000+08:00
scala> dt1<dt2
res1: Boolean = true
scala> val dt3=dt1+60.days
warning: Class org.joda.convert.FromString not found - continuing with a stub.
dt3: org.joda.time.DateTime = 2014-11-03T09:00:00.000+08:00
//字符转为date
scala> import java.text.SimpleDateFormat
import java.text.SimpleDateFormat
scala> val format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
format: java.text.SimpleDateFormat = java.text.SimpleDateFormat@4f76f1a
scala> val date=format.parse("2014-10-12 10:30:44")
date: java.util.Date = Sun Oct 12 10:30:44 CST 2014
scala> val datetime=new DateTime(date)
datetime: org.joda.time.DateTime = 2014-10-12T10:30:44.000+08:00
//做差
scala> val d=new Duration(dt1,dt2)
d: org.joda.time.Duration = PT4946400S
scala> d.getMillis
res5: Long = 4946400000
scala> d.getStandardDays
res6: Long = 57
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。