convert epoch to datetime in Scala / Spark

与世无争的帅哥 提交于 2019-12-05 18:52:13

You have a precedence problem - .toDateTime is being applied to 1000L before * is applied. Bracket the operations to make the call order clear:

def timeToStr(x: Long): String = { (x*1000L).toDateTime }

The opposite of parseDateTime is print :)

def timeToStr(epochMillis: Long): String =
  DateTimeFormat.forPattern("YYYY-MM-dd HH:mm:ss").print(epochMillis)

Follows my approach!

import java.util.Date
import java.text.SimpleDateFormat

def epochToDate(epochMillis: Long): String = {
    val df:SimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd")
    df.format(epochMillis)
}   

Follows a test run.

scala> epochToDate(1515027919000L)
res0: String = 2018-01-03
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!