将Unix时间戳转换为DateTime显示雅典的错误结果

问题描述 投票:0回答:1

我从DarkSkyApi获取UNIX时间戳,我想将其转换为DateTime格式并显示给用户。

我已经根据堆栈帖子尝试了多种方法,但到目前为止还没有运气。这就是我现在所拥有的:

val dt = Instant.ofEpochMilli(sunsetTime*1000L).atZone(
    ZoneId.of(tmz)
)
val formatted = dt.format(DateTimeFormatter.ofPattern("HH:mm"))
holder.weekDaySunsetTime.text = formatted

tmz的值为"Europe/Athens",因为我正在获取雅典的数据,并且我居住在同一时区。从逻辑上讲,我原本希望看到6:30(日出),但是我看到22:43&12:34日落。

我还尝试获取东京的数据。这将返回tmz"Asia/Tokyo",并且可以正确检索日出和日落时间。有什么想法吗?

android kotlin datetime-format
1个回答
0
投票

听起来API会根据您请求数据的城市返回不同的时区

因此,将时间戳转换为日期时间对象时,需要执行以下操作:

import java.time.*

val dt = Instant.ofEpochSecond(/*put time value here*/)
                .atZone(/*put time zone here*/)
                .toLocalDateTime() //this will convert it to your system's date time
© www.soinside.com 2019 - 2024. All rights reserved.