RaAm
RaAm

Reputation: 1092

Scala Operation on TimeStamp values

I have the input in timestamp , based on some condition i need to minus 1 sec or minus 3 months using scala programming

Input:

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

Output:

For Minus 1 sec

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

For Minus 3 Months

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

How to convert a string value to Timestamp and do the operations like minus in scala programming ?

Upvotes: 0

Views: 2104

Answers (3)

Souvik
Souvik

Reputation: 377

Try this below code

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))

Output

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

Upvotes: 2

philantrovert
philantrovert

Reputation: 10092

I assume you are working with Dataframes, since you have the spark-dataframe tag.

You can use the SQL INTERVAL to reduce the time, but your column should be in timestamp format for that:

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|
+-----------------------+-----------------------+-----------------------+

Upvotes: 5

Knows Not Much
Knows Not Much

Reputation: 31576

Look at the jodatime library. it has all the APIs you need to minus seconds or months from a timestamp

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

sbt dependency

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

Upvotes: 1

Related Questions