时间戳转换 [英] Timestamp convert
问题描述
我下面的函数没有将日期转换为定义的格式.
My below function doesn't cast the date to the defined format.
val oldFormat= new SimpleDateFormat("yyyy-MM-dd-HH.mm.ss.SSSSSS")
val newFormat= new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSSSSS",Locale.ENGLISH)
newFormat.format(oldFormat.parse(s).getTime))
输入日期格式为yyyy-MM-dd-HH.mm.ss.SSSSSS,需要将其转换为yyyy-MM-dd hh:mm:ss.SSSSSS.
Input date is in format yyyy-MM-dd-HH.mm.ss.SSSSSS, need to convert that into yyyy-MM-dd hh:mm:ss.SSSSSS.
我的输入是 2019-10-08-03.57.14.694695 但上面的代码输出到 2019-10-08 04:15:04.000695我在这里做错了什么
my input is 2019-10-08-03.57.14.694695 but the above code outputs to 2019-10-08 04:15:04.000695 what am I doing wrong here
推荐答案
使用 Java 8 中引入的较新的 DatetimeFormatter
和 LocalDateTime
API:
Using the newer DatetimeFormatter
and LocalDateTime
API introduced in Java 8:
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import java.util.*
fun main(args: Array<String>){
val oldFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd-HH.mm.ss.SSSSSS")
val localDateTime = LocalDateTime.parse("2019-10-08-03.57.14.694695", oldFormatter)
val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSS", Locale.ENGLISH)
val output = formatter.format(localDateTime)
println(output)
}
我创建了一个自定义格式化程序并获得了日期对象,我使用不同的自定义格式化程序再次重新格式化了该对象.
I created a custom formatter and obtained date object which I reformatted again using a different custom formatter.
这篇关于时间戳转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!