车家互联 小程序在智能汽车时代的创新与发展潜力
652
2022-09-02
Scala103-日期处理
Intro
介绍常用的日期处理方法。
时间获取
import org.joda.time.format.DateTimeFormatimport org.joda.time.{DateTime, Period,Duration}
Intitializing Scala interpreter ...Spark Web UI available at available as 'sc' (version = 2.4.4, master = local[*], app id = local-1578101041406)SparkSession available as 'spark'import org.joda.time.format.DateTimeFormatimport org.joda.time.{DateTime, Period, Duration}
获取当前时间-时间戳
val nowTimestamp = System.currentTimeMillis()
nowTimestamp: Long = 1578101045890
获取当前时间-日期
val nowDate = new DateTime(nowTimestamp)
nowDate: org.joda.time.DateTime = 2020-01-04T09:24:05.890+08:00
格式化日期
把日期转换成字符串,主要对应三种格式:
yyyy-MM-dd HH:mm:ssyyyy-MM-ddyyyyMMdd
val DateFormat1 = "yyyy-MM-dd HH:mm:ss"val DateFormat2 = "yyyy-MM-dd"val DateFormat3 = "yyyyMMdd"
DateFormat1: String = yyyy-MM-dd HH:mm:ssDateFormat2: String = yyyy-MM-ddDateFormat3: String = yyyyMMdd
Array(DateFormat1,DateFormat2,DateFormat3).foreach(x=>println("["+x+"] =>"+" Convert to=>"+nowDate.toString(x)))
[yyyy-MM-dd HH:mm:ss] => Convert to=>2020-01-04 09:24:05[yyyy-MM-dd] => Convert to=>2020-01-04[yyyyMMdd] => Convert to=>20200104
类型转换
时间戳转换
时间戳转日期
new DateTime(nowTimestamp)
res1: org.joda.time.DateTime = 2020-01-04T09:24:05.890+08:00
nowDate.getClass.getSimpleName
res2: String = DateTime
日期转时间戳
nowDate.getMillis
res3: Long = 1578101045890
字符串转换
日期转字符串
nowDate.toString("yyyy-MM-dd")
res4: String = 2020-01-04
字符串转日期 注意,如果字符串没有时分秒,会默认转成0点0分0秒
val StringDateArray = Array("2020-01-01 10:10:10","2020-01-02","20200103")val FormatArray = Array(DateFormat1,DateFormat2,DateFormat3)
StringDateArray: Array[String] = Array(2020-01-01 10:10:10, 2020-01-02, 20200103)FormatArray: Array[String] = Array(yyyy-MM-dd HH:mm:ss, yyyy-MM-dd, yyyyMMdd)
for(i<-0 until 3){println("Raw:"+StringDateArray(i)+"\n"+"New:"+DateTime.parse(StringDateArray(i), DateTimeFormat.forPattern(FormatArray(i))))}
Raw:2020-01-01 10:10:10New:2020-01-01T10:10:10.000+08:00Raw:2020-01-02New:2020-01-02T00:00:00.000+08:00Raw:20200103New:2020-01-03T00:00:00.000+08:00
运算
转换成时间戳
repeat once more
DateTime.parse("2020-01-01 01:00:01",DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss")).getMillis
res6: Long = 1577811601000
时刻属性
该时刻所拥有的属性,如
属于当天的哪一个小时属于该小时的哪一分钟属于分钟的哪一秒
时分秒
DateTime.parse("2020-01-01 21:00:01",DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss")).getHourOfDay
res7: Int = 21
DateTime.parse("2020-01-01 11:11:11",DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss")).getMinuteOfHour
res8: Int = 11
DateTime.parse("2020-01-01 11:12:31",DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss")).getSecondOfMinute
res9: Int = 31
再如:
该日期属于哪一个月该日期属于当年的第几周该日期属于周几该日期属于当年的第几天
月份信息
DateTime.parse("2019-12-29 02:00:01",DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss")).getMonthOfYear
res10: Int = 12
DateTime.parse("2020-01-01 02:00:01",DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss")).getMonthOfYear
res11: Int = 1
周信息 注意,跨年度的周,从1记起
DateTime.parse("2019-12-29 02:00:01",DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss")).getWeekOfWeekyear
res12: Int = 52
DateTime.parse("2019-12-31 02:00:01",DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss")).getWeekOfWeekyear
res13: Int = 1
DateTime.parse("2020-01-01 02:00:01",DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss")).getWeekOfWeekyear
res14: Int = 1
周几信息
周一即为1,周日为7
DateTime.parse("2019-12-31 02:00:01",DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss")).getDayOfWeek
res15: Int = 2
DateTime.parse("2020-01-01 02:00:01",DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss")).getDayOfWeek
res16: Int = 3
该日期在当年的天数
DateTime.parse("2020-01-01 02:00:01",DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss")).getDayOfYear
res17: Int = 1
DateTime.parse("2019-12-31 02:00:01",DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss")).getDayOfYear
res18: Int = 365
还有其他的方法,暂时用不到,不赘
两日期间隔
val testDate1 = DateTime.parse("2020-01-01 01:00:01",DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss"))val testDate2 = DateTime.parse("2020-01-01 02:00:02",DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss"))val testDate3 = DateTime.parse("2019-12-31 02:00:01",DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss"))
testDate1: org.joda.time.DateTime = 2020-01-01T01:00:01.000+08:00testDate2: org.joda.time.DateTime = 2020-01-01T02:00:02.000+08:00testDate3: org.joda.time.DateTime = 2019-12-31T02:00:01.000+08:00
val diffDuration = new Duration(testDate1,testDate2)
diffDuration: org.joda.time.Duration = PT3601S
毫秒为单位
diffDuration.getMillis
res19: Long = 3601000
//实际相差1小时1秒 (60*60+1)*1000
res20: Int = 3601000
秒为单位
diffDuration.getStandardSeconds
res21: Long = 3601
分钟为单位 直接取整
diffDuration.getStandardMinutes
res22: Long = 60
小时为单位
diffDuration.getStandardHours
res23: Long = 1
天为单位
diffDuration.getStandardDays
res24: Long = 0
某日期间隔N天的日期
间隔为正,向后推算;间隔为负,向前推算
plusDays 天数为单位plusMonths 月份为单位
其他的不多说,用到再补充~
testDate1
res25: org.joda.time.DateTime = 2020-01-01T01:00:01.000+08:00
testDate1.plusDays(1)
res26: org.joda.time.DateTime = 2020-01-02T01:00:01.000+08:00
testDate1.plusDays(-1)
res27: org.joda.time.DateTime = 2019-12-31T01:00:01.000+08:00
testDate1.plusMonths(1)
res28: org.joda.time.DateTime = 2020-02-01T01:00:01.000+08:00
时间比较
时间比较具体如下:
isAfterisBeforisEqual
testDate1.isAfter(testDate2)
res29: Boolean = false
testDate1.isBefore(testDate2)
res30: Boolean = true
testDate1.isEqual(testDate2)
res31: Boolean = false
生成日期list
import scala.collection.mutable.ArrayBufferdef dateRangeList(from: String, to: String,stepdays:Int=1,format:String="yyyy-MM-dd"):Array[String]={ // 先转换成日期 val fromDate = DateTime.parse(from,DateTimeFormat.forPattern("yyyy-MM-dd")) val toDate = DateTime.parse(to,DateTimeFormat.forPattern("yyyy-MM-dd")).plusDays(1) val dateRangeBuffer = ArrayBuffer[String]() var tempDate = fromDate while(tempDate.isBefore(toDate)){ dateRangeBuffer.append(tempDate.toString(format)) tempDate = tempDate.plusDays(stepdays) } dateRangeBuffer.toArray}
import scala.collection.mutable.ArrayBufferdateRangeList: (from: String, to: String, stepdays: Int, format: String)Array[String]
dateRangeList("2019-12-25","2019-12-30").mkString("\n")
res32: String =2019-12-252019-12-262019-12-272019-12-282019-12-292019-12-30
dateRangeList(from="2019-12-25",to="2019-12-30",stepdays=2).mkString("\n")
res33: String =2019-12-252019-12-272019-12-29
Ref
[1]于南京市江宁区九龙湖
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~