您好,登錄后才能下訂單哦!
傳統的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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。