Scala对TimeStamp值的操作

时间:2018-03-02 03:34:42

标签: scala apache-spark spark-dataframe

我有时间戳输入,基于某些条件我需要使用scala编程减1秒或减3个月

输入:

val date :String = "2017-10-31T23:59:59.000"

输出:

减1秒

val lessOneSec = "2017-10-31T23:59:58.000"

减去3个月

val less3Mon   = "2017-07-31T23:59:58.000"

如何将字符串值转换为Timestamp并在scala编程中执行诸如减号之类的操作?

3 个答案:

答案 0 :(得分:4)

我假设你正在使用Dataframes,因为你有spark-dataframe标签。

您可以使用SQL INTERVAL来缩短时间,但您的列应该采用timestamp格式:

df.show(false)
+-----------------------+
|ts                     |
+-----------------------+
|2017-10-31T23:59:59.000|
+-----------------------+

import org.apache.spark.sql.functions._

df.withColumn("minus1Sec" , date_format($"ts".cast("timestamp") - expr("interval 1 second") , "yyyy-MM-dd'T'HH:mm:ss.SSS") )
  .withColumn("minus3Mon" , date_format($"ts".cast("timestamp") - expr("interval 3 month ") , "yyyy-MM-dd'T'HH:mm:ss.SSS") )
  .show(false)

+-----------------------+-----------------------+-----------------------+
|ts                     |minus1Sec              |minus3Mon              |
+-----------------------+-----------------------+-----------------------+
|2017-10-31T23:59:59.000|2017-10-31T23:59:58.000|2017-07-31T23:59:59.000|
+-----------------------+-----------------------+-----------------------+

答案 1 :(得分:2)

请尝试以下代码

val yourDate = "2017-10-31T23:59:59.000"
val formater = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSS")
val date = LocalDateTime.parse(yourDate, formater)
println(date.minusSeconds(1).toString(formater))
println(date.minusMonths(3).toString(formater))
  

输出

2017-10-31T23:59:58.000
2017-07-31T23:59:59.000

答案 2 :(得分:1)

查看jodatime库。它具有您需要的所有API,从时间戳减去秒或数月

http://www.joda.org/joda-time/

sbt依赖

"joda-time" % "joda-time" % "2.9.9"