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

运行效果图:


在这里插入图片描述
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容