android - 在 Unix 中显示时间戳,在 Android Kotlin 中显示 UTC

标签 android date kotlin

我正在开发一个简单的天气应用程序,并尝试以“K:mm a”格式显示时间(例如 6:30 AM)。我正在为用户搜索的指定位置(例如纽约市)获取 Unix、UTC 中的时间戳。时间戳类似于 1624836905,时区偏移量类似于 -14400。我有一个函数可以将两者相加,将其转换为毫秒,并且应该以指定的格式返回时间。函数如下:

fun dateTime(time: Int, zone: Int, format: String = "EEE, MMMM d K:mm a"): String {
        return try {
            val sdf = SimpleDateFormat(format)
            val netDate = Date((time.plus(zone)).toLong() * 1000)
            sdf.timeZone = TimeZone.getTimeZone("UTC")
            sdf.format(netDate)
        } catch (e: Exception) {
            e.toString()
        }
    }

我这样调用它:

sunriseTextView.text = dateTime(result2.lookup<Int>("daily.sunrise")[0], timeZone, "K:mm a")
sunsetTextView.text = dateTime(result2.lookup<Int>("current.sunset")[0], timeZone, "K:mm a")

预期输出是日出/日落时间,例如早上 6:01 和晚上 9:05。我还在指定地点渲染当前时间,也是从 API 获得的。如下:

dateView.text = dateTime(result2.lookup<Int>("current.dt")[0], timeZone)

它以“EEE, MMMM d K:mm a”格式输出该地点的当前日期和时间(例如,Mon June 28 8:23 AM)。

当前时间总是正确的,但是日出和日落时间有问题。例如,如果我输入 NYC,日出时间为晚上 7:35,日落时间为上午 10:39。另一方面,东京的日出和日落在凌晨 4:27 和晚上 7:00 显示正确。

显然我遗漏了一些东西,因为我知道 API 数据是正确的。我正在寻找任何建议,但是,我将不胜感激没有 API 限制的建议,例如需要 API 26 的 kotlinx-datetime。

最佳答案

因为有 API Desugaring ,您可以将 java.time 与低于 26 的 API 版本一起使用。

这意味着您不必依赖那些过时的日期时间类,例如 java.util.Datejava.text.SimpleDateFormat

你的 fun 可以这样重写:

fun dateTime(time: Int, zone: String, format: String = "EEE, MMMM d K:mm a"): String {
    // parse the time zone
    val zoneId = ZoneId.of(zone)
    // create a moment in time from the given timestamp (in seconds!)
    val instant = Instant.ofEpochSecond(time.toLong())
    // define a formatter using the given pattern and a Locale
    val formatter = DateTimeFormatter.ofPattern(format, Locale.ENGLISH)
    // then make the moment in time consider the zone and return the formatted String
    return instant.atZone(zoneId).format(formatter)
}

下面是一些在简单的 fun main() 中使用的示例:

fun main() {
    val timestamp: Int = 1624836905 // your example epoch seconds
    // try two different zones
    val newYorkTime = dateTime(timestamp, "America/New_York")
    val tokyoTime = dateTime(timestamp, "Asia/Tokyo")
    // and print the results
    println(newYorkTime)
    println(tokyoTime)
}

这个例子的输出:

Sun, June 27 7:35 PM
Mon, June 28 8:35 AM

请注意,如果您只想提供与 UTC 的 小时偏移量,您也可以使用 offset: Int 而不是 zone: String 。您需要调整此 fun 的两行,然后:

fun dateTime(time: Int, offset: Int, format: String = "EEE, MMMM d K:mm a"): String {
    // parse the time zone
    val zoneOffset = ZoneOffset.ofHours(offset)
    // create a moment in time from the given timestamp (in seconds!)
    val instant = Instant.ofEpochSecond(time.toLong())
    // define a formatter using the given pattern and a Locale
    val formatter = DateTimeFormatter.ofPattern(format, Locale.ENGLISH)
    // then make the moment in time consider the zone and return the formatted String
    return instant.atOffset(zoneOffset).format(formatter)
}

像这样在 main 中使用它

fun main() {
    val timestamp: Int = 1624836905
    
    val newYorkTime = dateTime(timestamp, -4)
    val tokyoTime = dateTime(timestamp, 9)
    println(newYorkTime)
    println(tokyoTime)
}

将产生完全相同的输出。

此外,DateTimeFormatter 中使用的Locale 也可以是一个函数参数,以防您想支持不同的语言(这会影响月份和日期的名称)周)。

关于android - 在 Unix 中显示时间戳,在 Android Kotlin 中显示 UTC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68163464/

相关文章:

android - Nexus 5 (4.4.2) 手电筒 LED 未打开

java - 为什么只读一次循环读取logcat

date - 如何在 SPARQL 中转换日期格式?

android - 安装kotlin后无法打开Android studio项目

android - 获取 Flow 发出的最后一项并且不接收更新

java - 不能在 Fragment 中使用 Toothpick.inject

PHP 日期差异有差异

php - 查找日期范围与指定日期范围相交的行

android - 如何在 Kotlin 中使用 MPAndroidChart 为条形图添加带有一些详细信息的标记?

android - 如何在Kotlin中将list <String>转换为带有前缀和后缀的字符串