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

 /**
    * 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互转))