首页 > 代码库 > scala处理时间序列数据

scala处理时间序列数据

传统的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


本文出自 “wtt561111” 博客,请务必保留此出处http://tuntunwang.blog.51cto.com/5596095/1906511

scala处理时间序列数据