SimpleDateFormat
- 创建SimpleDateFormat时即可指定各种时间格式
//Demo
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd")
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
- format() 把时间转化为对应格式的字符串
- 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))
运行效果图:

网友评论