java中的时间转换(二)(SimpleDateFormat)

SimpleDateFormat

  1. 创建SimpleDateFormat时即可指定各种时间格式
//Demo
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd")
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
  1. format() 把时间转化为对应格式的字符串
  2. parse() 把对应格式的字符串转成Date对象
  val sdf: SimpleDateFormat = new SimpleDateFormat("yyyyMMdd")
  val date = new Date()
  val timeLong = date.getTime

  println("String 转 Date:  " + sdf.parse("20080101"))
  println("Long类型 转 String:  " + sdf.format(timeLong))
  println("Date类型 转 String:  " + sdf.format(date))

在这里插入图片描述
SimpleDateFormat 还有其它日期格式,可以随意玩

  val sdf_date: SimpleDateFormat = new SimpleDateFormat("yyyy/MM/dd")
  val sdf_seconds: SimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")

  println(sdf_date.parse("2019/06/28"))
  println(sdf_seconds.parse("2019-06-28 14:20:00"))

  println(sdf_date.format(date))
  println(sdf_seconds.format(date))

运行效果图:
在这里插入图片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章