【发布时间】:2021-01-13 15:16:24
【问题描述】:
我正在尝试将一个时区的午夜转换为另一个时区的午夜。 Kotlin 几乎可以轻松转换时区,但在将日期和时间转换为毫秒时,它的工作方式并不相同。
问题:
Indian Time: Mon Sep 28 00:00:00 GMT+5:30 2020
Vancouver Time: Sunday Sep 27 11:30:00 GMT-7:00 2020
我需要什么
Indian Time: Mon Sep 28 00:00:00 GMT+5:30 2020
Vancouver Time: Sunday Sep 27 11:30:00 GMT-7:00 2020
这是我尝试过的:
val today = DateTime().withTimeAtStartOfDay().toDate() //current date and time converted to Date format
val dateOutputFormat = SimpleDateFormat("yyyy/MM/dd HH:mm:ss") // formatting the output as SimpleDateFormat
dateOutputFormat.setTimeZone(TimeZone.getTimeZone("America/Vancouver")) //Setting the timezone
Log.d("Datey2", "Before conversion ${today}") // Before conversion Mon Sep 28 00:00:00 GMT+5:30 2020
val Vancouver = Date(dateOutputFormat.format(today)).time //formatting the timezone
Log.d("Datey2", "After conversion $Vancouver") // After conversion Sun Sep 27 11:30:00 GMT+05:30 2020
val VancouverMnight = DateTime(Vancouver).withTimeAtStartOfDay().millis
Log.d("Datey2", "MidNight $VancouverMnight") // MidNight Sun Sep 27 00:00:00 GMT+05:30 2020
输出:
Before conversion Mon Sep 28 00:00:00 GMT+5:30 2020
After conversion Sun Sep 27 11:30:00 GMT+05:30 2020 // Note the GMT+5:30 in vacouver time
MidNight Sun Sep 27 00:00:00 GMT+05:30 2020
然后我将这些转换为毫秒,如下所示
Log.d("Datey2", "After conversion {$VancouverMnight.time}") // using time function gives output in milliseconds
但是当我将温哥华输出转换为毫秒时,我得到以下信息:
1601186400000 // Sep 26 23:00:00 2020 - Goes 2 days before the given time
1601145000000 // Sep 26 11:30:00 2020
我需要什么:
Sep 27 11:30:00 2020
Sep 27 00:00:00 2020 (I need these in milliseconds)
【问题讨论】:
-
我发现很难准确理解您想要什么,这就是我投反对票的原因。
-
嗨@OleV.V。非常感谢您阅读本文。我非常感谢投票,并感谢您对原因发表评论。我尽力提出这个问题。但是现在,有人编辑了它:)
标签: android kotlin timezone date-conversion android-date