在Scala/Spark中将纪元转换为日期时间
问题描述:
我正在使用:
def strToTime(x: String):Long = { DateTimeFormat.
forPattern("YYYY-MM-dd HH:mm:ss").parseDateTime(x).getMillis()/1000 }
获得像这样的Long列表:
to get a list of Long like this :
.map( p=> List( strToTime(p(0) ) ) )
我的问题是-向后上交的最简单方法是什么? 像这样:
my question is - what is the easiest way to turn in backwards? something like:
def timeToStr(x: Long):String = { x*1000L.toDateTime}
我可以在上面的列表(长)上使用的
that I could use on the above List(Long)
我已阅读将自纪元以来的秒数转换为Scala ,但无法成功应用
I have read Convert seconds since epoch to joda DateTime in Scala but can't apply it successfully
答
您有一个优先级问题-在应用*
之前将.toDateTime
应用于1000L
.用括号括起来以清楚显示呼叫顺序:
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 }