在Java中转换1900年之前的时间戳 [英] Convert a timestamp before year 1900 in java
问题描述
我的Android应用程序与API通信,该API给了我以下时间戳:-2209161600
.转换为日期时间,应该是12-30-1899 00:00:00
My Android app communicate with an API which give me the following timestamp : -2209161600
. Converted to a date time, it's supposed to be 12-30-1899 00:00:00
问题是,我尝试同时使用默认库,Threetenbp和jodatime转换此时间戳,但是使用Europe/Paris
时区,我总是得到相同的错误结果:12-30-1899 00:09:21
The problem is, I tried to convert this timestamp using both the default library, threetenbp, and then jodatime, but I always get the same wrong result, using Europe/Paris
timezone : 12-30-1899 00:09:21
为什么会这样?
例如使用jodatime
For example with jodatime
DateTime dt = new DateTime(-2209161600000L, DateTimeZone.forID("Europe/Paris")); // dt: "1899-12-30T00:09:21.000+00:09:21"
推荐答案
我认为我在为什么时区的偏移量与JDK不同?:
...影响引入现代时区系统之前的日期时间.时区数据是从时区数据库获取的.该数据库包含有关本地平均时间"(LMT)的信息,该信息是在太阳移动之后的某个位置应观测到的本地时间.
... affects date-times before the modern time-zone system was introduced. The time-zone data is obtained from the time-zone database. The database contains information on "Local Mean Time" (LMT) which is the local time that would have been observed at the location following the Sun's movements.
在某个位置选择第一个时区偏移量之前,Joda-Time始终使用LMT信息. ...
Joda-Time uses the LMT information for all times prior to the first time-zone offset being chosen in a location. ...
换句话说,该时间段数据库没有条目,因此使用本地平均时间(例如,巴黎为0:09:21,马德里 1 ).
In other words, the database does not have entries for that time so it is uses the Local Mean Time (e.g. 0:09:21 for Paris, or -0:14:44 for Madrid 1).
System.out.println(new DateTime(-2209161600000L, DateTimeZone.forID("Europe/Paris")));
System.out.println(new DateTime(-2209161600000L, DateTimeZone.forID("Europe/Madrid")));
将打印
1899-12-30T00:09:21.000+00:09:21
1899-12-29T23:45:16.000-00:14:44
解决方案:取决于需要什么时间,如果UTC足够,请使用
Solution: depends what tis time is needed for, if UTC is sufficient, use
new DateTime(-2209161600000L, DateTimeZone.forID("UTC")) // 1899-12-30T00:00:00.000Z
或只是像
Instant.ofEpochSecond(-2209161600L)
Instant.ofEpochMilli(-2209161600000L)
1 - http://home. kpn.nl/vanadovv/time/TZworld.html#eur
这篇关于在Java中转换1900年之前的时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!