Lobsterrrr Lobsterrrr - 5 months ago 27
Scala Question

How to retrieve the year from a Unix timestamp?

I am using

for scala 2.11:


The goal is to retrieve the year from a Unix timestamp:

val t = "1320904800"
val dt = new DateTime(timestamp.toLong)

The problem is that it outputs 1970 instead of 2011. What am I doing wrong?


DateTime constructor assumes millisecond value, and it seems like your input is in seconds (i.e. seconds since 1970/01/01 00:00:00.0) - you should get the right result if you multiply by 1000:

val t = "1320904800"
val dt = new DateTime(t.toLong * 1000)
println(dt.getYear.toString) // 2011