scala 時間格式轉換(String、Long、Date)


1)scala 時間格式轉換(String、Long、Date)

1、時間字符類型轉Date類型

[java] view plain copy
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類型轉字符類型

[java] view plain copy
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類型

[java] view plain copy
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

 

2)scala 時間和時間戳互轉

時間轉換為時間戳:

import java.text.SimpleDateFormat

object test {

def main(args: Array[String]): Unit = {

val tm = "2017-08-01 16:44:32"
val a = tranTimeToLong(tm)
println(a)
}

def tranTimeToLong(tm:String) :Long={
val fm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
val dt = fm.parse(tm)
val aa = fm.format(dt)
val tim: Long = dt.getTime()
tim
}
}

時間戳轉化為時間:

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

object test {

def main(args: Array[String]): Unit = {

val tm = "1502036122000"
val a = tranTimeToString(tm)
println(a)

}

def tranTimeToString(tm:String) :String={
val fm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
val tim = fm.format(new Date(tm.toLong))
tim
}
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2024 CODEPRJ.COM