如何在Kotlin中将日期字符串转换为时间戳? [英] How to convert date string to timestamp in Kotlin?

查看:724
本文介绍了如何在Kotlin中将日期字符串转换为时间戳?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将日期字符串从日期字符串转换为unix时间戳,例如14-02-2018

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

有人可以帮忙吗?

推荐答案

自JDK 8开始,您可以执行以下操作:

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.

这篇关于如何在Kotlin中将日期字符串转换为时间戳?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆