0

I have one column its has epoch time( String type) in the table.Want to minus one hour from epoch time .I'm trying to convert a string into epoch time and do the minus. But I am unable to convert the string to epoch time. Any help, please?

for Eg:

val epochTime="1499013460"

I want ti subtract one hour from the above epoch time.

Thanks!

senthil kumar p
  • 526
  • 2
  • 7
  • 25
  • post an example of your String date format if possible – bottaio Jul 02 '17 at 16:41
  • Have you tried looking here? https://stackoverflow.com/questions/31134969/how-to-convert-unix-timestamp-to-date-in-spark – Evaldas Buinauskas Jul 02 '17 at 16:45
  • yes i tried .Hitting with Caused by: java.lang.IllegalArgumentException: Invalid format: "1499015689" is malformed at "9". val table=Seq(("1499015689",1),("1499015689",2)).toDF("col1","col2") val time_col = table.select(col("col1")) .map(line => new DateTime(line(0)).toString("yyyy-MM-dd")).toDF() Please let me know did i made any mistake.Thanks! – senthil kumar p Jul 02 '17 at 17:18

2 Answers2

3

Here is one approach:

scala> import java.time.Instant
import java.time.Instant

scala> val myString = Instant.now().toString
myString: String = 2017-07-02T17:04:39.911Z

scala> Instant.parse(myString).getEpochSecond
res8: Long = 1499015079
Akavall
  • 82,592
  • 51
  • 207
  • 251
1

If you have an epoch time values and all you want to do is to subtract an hour from it you can go with the simplest solution possible which is:

val secondsPerHour = 60 * 60
val withHourSubtracted = (epochTime.toLong - secondsPerHour).toString
bottaio
  • 4,963
  • 3
  • 19
  • 43