本博客转自:https://blog.csdn.net/Neleuska/article/details/78864845
scala中关于时间格式的转换问题,总结为以下三种常用情况:
1、时间字符类型转Date类型
-
import java.text.SimpleDateFormat
-
val time =
"2017-12-18 00:01:56"
-
val newtime :Date =
new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss").parse(time)
-
println(newtime)
-
//output:Mon Dec 18 00:01:56 CST 2017
2、Long类型转字符类型
-
val time:Long=
1513839667
//秒
-
val newtime :String =
new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss").format(time*
1000)
-
println(newtime)
-
//output:2017-12-21 15:01:07
3、时间字符类型转Long类型
-
val time =
"2017-12-18 00:01:56"
-
val newtime :Long=
new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss").parse(time).
getTime
-
println
(newtime)
-
//output:1513526516000
-