时间戳显示错误的数据 [英] Timestamp show wrong data

查看:1732
本文介绍了时间戳显示错误的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么时间戳((长)-62135596800000L)返回0001-01-03 00:00:00 ,但必须返回 0001-01-01 00:00:00 ?该服务显示正确的时间戳这里

Why Timestamp((long)-62135596800000L) return 0001-01-03 00:00:00, but must return 0001-01-01 00:00:00? This service show correct Timestamp here

推荐答案

-62135596800000 为0001-01-03 00:00:00.0因为的使用儒略历的日期10月15日之前,1582

-62135596800000 is 0001-01-03 00:00:00.0 Because by default java uses Julian calendar for dates before October 15, 1582.

您正在使用的网站,使用JavaScript它采用外推,或proleptic,公历的所有日期。从 JavaScript规范

The website you are using, uses javascript which uses extrapolated, or proleptic, Gregorian calendar for all dates. From javascript specification

的ECMAScript使用一个外推公历系统来映射的天数时,以一年数,并确定一年内的月份和日期。

ECMAScript uses an extrapolated Gregorian system to map a day number to a year number and to determine the month and date within that year.

事实上,在JavaScript:

Indeed, in javascript:

new Date(-62135596800000).toUTCString()
//"Mon, 01 Jan 1 00:00:00 GMT"

您可以使用这样的事情在Java中获得同样的结果:

You can use something like this in java to gain the same results:

GregorianCalendar date = new GregorianCalendar();
date.clear();
//Use Gregorian calendar for all values
date.setGregorianChange(new Date(Long.MIN_VALUE));

date.setTimeZone( TimeZone.getTimeZone("UTC"));
date.setTime(new Date(-62135596800000L));

System.out.println(
        date.get(GregorianCalendar.YEAR) + "-" +
        (date.get(GregorianCalendar.MONTH) + 1) + "-" + 
        date.get(GregorianCalendar.DAY_OF_YEAR) + " " +
        date.get(GregorianCalendar.HOUR_OF_DAY) + ":" +
        date.get(GregorianCalendar.MINUTE) + ":" + 
        date.get(GregorianCalendar.SECOND) + "." +
        date.get(GregorianCalendar.MILLISECOND)
);
//Prints 1-1-1 0:0:0.0

不幸的是我不知道如何开展从日历格里高利更改日期对象,所以我我直接从日历对象所做的格式。如果我只是做
formatter.format(date.getTime())它将失去公历改变设置,并再次显示第3天。

Unfortunately I don't know how to carry out the Gregorian change from Calendar to Date objects, so I am doing the formatting directly from the calendar object. If I just did formatter.format(date.getTime()) it would lose the Gregorian change setting and show 3rd day again.

Julian日期领先2天因为根据<一个href=\"http://en.wikipedia.org/wiki/Proleptic_Gregorian_calendar#Difference_between_Julian_and_proleptic_Gregorian_calendar_dates\">this,
朱利安领先proleptic阳历的2天从公元前1年至公元100

The Julian date is 2 days ahead because according to this, Julian is ahead of proleptic Gregorian by 2 days from 1 BC to 100 AD.

顺便说一句,我建议使用 JodaTime ,它正确的(我认为,但如果你需要的东西更有说服力)默认为纯阳历:

Btw, I recommend using JodaTime, it correctly (my opinion, though if you need something more convincing) defaults to pure Gregorian:

DateTime dt = new DateTime(-62135596800000L, DateTimeZone.UTC);
System.out.println(dt.toString());
//0001-01-01T00:00:00.000Z

这篇关于时间戳显示错误的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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