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

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

问题描述

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



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


ECMAScript使用外推的公历系统将日期数字映射到年数,并确定该年份内的月份和日期。


的确,在javascript中:

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

你可以在java中使用这样的东西获得相同的结果:

  GregorianCalendar date = new GregorianCalendar(); 
date.clear();
//对所有值使用公历日历
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)
);
//打印1-1-1 0:0:0.0

不幸的是,不知道如何将公历从 Calendar Date 对象执行,所以我直接从日历对象。如果我只是做了
formatter.format(date.getTime()),则会丢失公历更改设置,再次显示第3天。



根据这个 $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,我建议使用 JodaTime 正确(我的意见,虽然如果你需要更有说服力的话)默认为纯格里高利: / p>

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


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 is 0001-01-03 00:00:00.0 Because by default java uses Julian calendar for dates before October 15, 1582.

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

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.

Indeed, in javascript:

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

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

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.

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.


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天全站免登陆