Kotlin-如何从字符串日期获取时间(“HH:mm”)
我如何从json中获取时间,其中我有如下值:Kotlin-如何从字符串日期获取时间(“HH:mm”),kotlin,Kotlin,我如何从json中获取时间,其中我有如下值: "time": ["1507457400000", "1507458600000"] //Strings 在Javascript中,我可以执行以下操作 new Date(1507457400000) // return Sun Oct 08 2017 12:10:00 GMT+0200 new Date(1507457400000).getHours() // return 12 new Date(15074574000
"time": ["1507457400000", "1507458600000"] //Strings
在Javascript中,我可以执行以下操作
new Date(1507457400000) // return Sun Oct 08 2017 12:10:00 GMT+0200
new Date(1507457400000).getHours() // return 12
new Date(1507457400000).getMinutes() // return 10
但我不知道如何利用kotlin获得时间。知道从我掌握的数据中获取时间的最佳方法是什么吗?使用以下方法:
val calendar = Calendar.getInstance()
calendar.timeInMillis = 1507457400000
val date = calendar.time
如果字符串中有毫秒,如str
:
val calendar = Calendar.getInstance()
calendar.timeInMillis = str.toLong()
val date = calendar.time
小时和分钟:
val h = calendar.get(Calendar.HOUR_OF_DAY)
val m = calendar.get(Calendar.MINUTE)
val result = h.toString() + ":" + m.toString()
好的,我只需要时间(hh:mm),现在您的代码返回给我:Sat-Oct-07 10:00:00 GMT+02:00 2017有什么想法来获取时间吗,谢谢@mtak@gon250您的Javascript示例也没有得到时间。请更新您的问题,提供更多关于您的价值类型的详细信息expect@gon250这不是你想要的吗?如果你的目标是JVM8,你可以使用
java.time
API。为了更准确地回答,我们需要确切地知道你所说的“争取时间”是什么意思。你也要日期吗?您想要对象还是格式化字符串?
val date = SimpleDateFormat("HH:mm").format(Date((1507457400000 / 1000)))