本文介绍了在Java中转换1900年之前的时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的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不同?:

在某个位置选择第一个时区偏移量之前,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,马德里).

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)

- http://home. kpn.nl/vanadovv/time/TZworld.html#eur

这篇关于在Java中转换1900年之前的时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-08 09:41