How to convert date string to timestamp in Kotlin?

二次信任 提交于 2021-02-18 09:43:10

问题


I want to convert a date string to unix timestamp from a date string e.g. 14-02-2018

Can someone help?


回答1:


Since JDK 8 you can do:

val l = LocalDate.parse("14-02-2018", DateTimeFormatter.ofPattern("dd-MM-yyyy"))

val unix = l.atStartOfDay(ZoneId.systemDefault()).toInstant().epochSecond

Note that the example uses your system's default timezone.




回答2:


use this to convert the date string to UNIX timestamp

val date = SimpleDateFormat("dd-MM-yyyy").parse("14-02-2018")
println(date.time)


来源:https://stackoverflow.com/questions/48838992/how-to-convert-date-string-to-timestamp-in-kotlin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!