Time conversion from seconds to date issue

后端 未结 4 1169
慢半拍i
慢半拍i 2021-01-29 09:23

I have the following Long variable holding epoch value in seconds, which I\'m trying to convert into a Date.

val seconds = 1341855763000         


        
4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-29 10:22

    I think your time is actually in milliseconds. If I convert 1341855763000 using this website it gives me your expected time and this as well:

    fun main() {
      val millis = 1341855763000
      val date = Date(millis)
      println(date)
    }
    

    Alternatively, you can also use seconds:

    fun main() {
      val seconds = 1341855763L
      val date = Date(TimeUnit.SECONDS.toMillis(seconds))
      println(date)
    }
    

    Just divide by 1000.

提交回复
热议问题