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-2025 CODEPRJ.COM