scala时间和时间戳互转

2019-07-02 09:18:49 浏览数 (1)

时间转换为时间戳:

代码语言:javascript复制
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
  }
}

时间戳转化为时间:

代码语言:javascript复制
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
  }
}

0 人点赞