Convert seconds since epoch to joda DateTime in Scala

ID : 274328

viewed : 37

Tags : scaladatetimejodatimescala





Top 2 Answer for Convert seconds since epoch to joda DateTime in Scala

vote vote

94

Check a quick REPL session:

scala> 1378607203 * 1000 res6: Int = -77299016 

Odd, isn't it? :) Can you guess why this is happening?

I will give you a hint extracted from DateTime's constructor you are trying to use.

DateTime(long instant) 

Still don't get it? Let's try a slightly different version:

scala> 1378607203L * 1000 res8: Long = 1378607203000 

Notice the L indicating a literal of type Long. You are asking for 1 trillion! And Int only go as far as 2 billons:

scala> Int.MaxValue res7: Int = 2147483647 

So doing DateTime(1378607203L*1000) will make it work.

vote vote

83

Top 3 video Explaining Convert seconds since epoch to joda DateTime in Scala







Related QUESTION?