Scala 日期 时间戳 转换


 

 

object App {

  def main(args: Array[String]): Unit = {
    val dtString = "2020-01-10 10:20:30";
    val sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    val timestamp = dateToTimestamp(dtString, sdf);
    println(timestamp); //1578622830000
    val dt = timestampToDate(timestamp, sdf);
    println(dt); //2020-01-10 10:20:30


    val dtString2 = "2019-12-12";
    val sdf2 = new java.text.SimpleDateFormat("yyyy-MM-dd");

    val timestamp2 = dateToTimestamp(dtString2, sdf2);
    println(timestamp2); //1576080000000
    val dt2 = timestampToDate(timestamp2, sdf2);
    println(dt2); //2019-12-12
  }

  /**
    * 日期转时间戳
    **/
  def dateToTimestamp(dtString: String, sdf: java.text.SimpleDateFormat): Long = {
    val dt = sdf.parse(dtString);
    val timestamp = dt.getTime();
    return timestamp;
  }

  /**
    * 时间戳转日期
    **/
  def timestampToDate(timestamp: Long, sdf: java.text.SimpleDateFormat): String = {
    val dtString = sdf.format(timestamp);
    return dtString;
  }

}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM