将Instant转换为LocalDateTime时的不同值 [英] Different values when converting Instant to LocalDateTime

查看:70
本文介绍了将Instant转换为LocalDateTime时的不同值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从服务器收到UTC格式的时间.我解析为一个 Instant 对象,然后将其转换为 LocalDateTime .像这样:

I receive a time in UTC format from the server. I parse to an Instant object and then convert it to LocalDateTime. Like so:

fun String.toLocaleDateTime(): LocalDateTime {
    return LocalDateTime.ofInstant(
        Instant.parse(this), 
        ZoneOffset.systemDefault()
    )
}

问题在于, LocalDateTime 对象在不同的​​年份也不同.

The issue is that for different years the LocalDateTime object is different.

例如,当将UTC时间转换为IST时间时:

For example when converting UTC time to IST time:

1800-01-01T03:36:32Z->1800-01-01T09:30
1870-01-01T03:36:32Z->1870-01-01T08:57:42
1906-01-01T03:36:32Z->1906-01-01T09:06:32

1800-01-01T03:36:32Z -> 1800-01-01T09:30
1870-01-01T03:36:32Z -> 1870-01-01T08:57:42
1906-01-01T03:36:32Z -> 1906-01-01T09:06:32

游乐场

推荐答案

让我们获取印度从1700年到2000年的所有偏移量变化:

Let's get all the changes in offset for India from 1700 to 2000:

ZoneId.of("Asia/Kolkata").rules.transitions.filter {
    (1700..2000).contains(it.dateTimeBefore.year)
}.forEach {
    println("On ${it.dateTimeBefore}, jumped from ${it.offsetBefore} to ${it.offsetAfter}")
}

这将获取您计算机上tzdb版本的时区数据.在我的机器上,将打印:

This will get the timezone data on the version of tzdb on your machine. On my machine, this prints:

On 1854-06-28T00:00, jumped from +05:53:28 to +05:53:20
On 1870-01-01T00:00, jumped from +05:53:20 to +05:21:10
On 1906-01-01T00:00, jumped from +05:21:10 to +05:30
On 1941-10-01T00:00, jumped from +05:30 to +06:30
On 1942-05-15T00:00, jumped from +06:30 to +05:30
On 1942-09-01T00:00, jumped from +05:30 to +06:30
On 1945-10-15T00:00, jumped from +06:30 to +05:30

直到1906年,印度似乎一直使用本地平均时间.

It seems like until 1906, India was using local mean time.

您的每个结果都反映了这些偏移量:

Each of your results reflect these offsets:

  • 在1800年,偏移量是+05:53:28,加到03:36:32的恰好是09:30
  • 在1870年,偏移量是+05:21:10偏移量,加上03:36:32恰好是08:57:42
  • 1906年,偏移量被标准化为+05:30,为您提供了您可能期望的答案.

这篇关于将Instant转换为LocalDateTime时的不同值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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