java 1900 1970,在Java中转换1900年之前的时间戳

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

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

Why does that happen ?

EDIT: For example with jodatime

DateTime dt = new DateTime(-2209161600000L, DateTimeZone.forID("Europe/Paris")); // dt: "1899-12-30T00:09:21.000+00:09:21"

解决方案

... 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 uses the LMT information for all times prior to the first time-zone offset being chosen in a location. ...

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")));

will print

1899-12-30T00:09:21.000+00:09:21

1899-12-29T23:45:16.000-00:14:44

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

or just the standard java.time classes like

Instant.ofEpochSecond(-2209161600L)

Instant.ofEpochMilli(-2209161600000L)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值