美文网首页
Scala 日期操作(String,Date,Long互转)

Scala 日期操作(String,Date,Long互转)

作者: 风清云流 | 来源:发表于2018-08-23 23:50 被阅读0次
     /**
        * Author: ouyangxiaoyuan
        */
      def main(args: Array[String]): Unit = {
    
        // String, Long, Date
        // String --> Date
        val timeString_1: String = "2018-08-23 23:14:01"
        val timeDate_1: Date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(timeString_1)
        println("timeDate_1: " + timeDate_1)
    
        // convert Long --> Date
        val longTime_2: Long = 1613839669 //unit: second
        val timeDate_2: String = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(longTime_2 * 1000)
        println("timeDate_2: " + timeDate_2)
    
        // String --> Date --> Long
        val timeString_3: String = "2018-07-09 14:30:00"
        val timeDate_3: Date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(timeString_3)
        val timeLong_3: Long = timeDate_3.getTime
        println("timeLong_3: " + timeLong_3)
    
        // Date --> String
        val timeDate_4: Date = new SimpleDateFormat("yyyy-MM-dd").parse("2018-02-03")
        val timeString_4: String = new SimpleDateFormat("yyyy-MM-dd").format(timeDate_4)
        println("timeString_4: " + timeString_4)
    
        // display current time (unit: millisecond) Long, Long --> String
        println("currentMilliseconds: " + System.currentTimeMillis())
        val time_5: String = new SimpleDateFormat("yyyy-MM-dd").format(System.currentTimeMillis())
        println("time_5: " + time_5)
      }
    

    相关文章

      网友评论

          本文标题:Scala 日期操作(String,Date,Long互转)

          本文链接:https://www.haomeiwen.com/subject/oxuviftx.html