美文网首页
java中的时间转换(二)(SimpleDateFormat)

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

作者: 及未来 | 来源:发表于2020-04-16 06:42 被阅读0次

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))

运行效果图:


在这里插入图片描述

相关文章

网友评论

      本文标题:java中的时间转换(二)(SimpleDateFormat)

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