java.sql.Date如何处理负日期? [英] How does java.sql.Date work with negative dates?

查看:297
本文介绍了java.sql.Date如何处理负日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到的情况是,当从数据库中读取日期时,Java运行时返回了一些反转的毫秒值(在 java.sql.Date 中)。毫秒值与天数大致相同,但从第0年开始向后计数。

I had a situation where the Java runtime returned sort of "inverted" millisecond values when reading dates from the database (in java.sql.Date). The millisecond value was approximately the same amount of days, but counted backwards from year 0.

只需重新启动Java运行时即可解决问题。

The problem was solved by just restarting the Java runtime.

但是:我发现除了工作日之外,Java几乎没有正确处理这些反转值。

But: I found out that Java handles these "inverted" values almost correctly except of the week day.

当你运行以下内容时代码:

When you run the following code:

System.out.println(new java.util.Date(253402214400000l));
System.out.println(new java.util.Date(-377648784000000l));

您将获得以下输出:

Fri Dec 31 01:00:00 CET 9999
Tue Dec 31 01:00:00 CET 9999

另一个例子:

System.out.println(new java.util.Date(-294192000000l));
System.out.println(new java.util.Date(-123967324800000l));

结果:

Mon Sep 05 01:00:00 CET 1960
Mon Sep 05 01:00:00 CET 1960

使用在线转换器时,特定第二行的结果会有所不同。这将导致接近实际正日期的负日期(年份为负):

When using online converters, the result will be different for the particular second line. It will result in a negative date (the year is negative) close to the real, positive date:

Example1:
253402214400000 = Fri Dec 31 9999 01:00:00
-377648784000000 = Tue Oct 15 -9998 02:00:00

Example 2:
-294192000000 = Mon Sep 05 1960 02:00:00
-123967324800000 = Mon Aug 19 -1959 02:00:00

我还没有找到关于这个话题的任何信息。

I have not found any information about this "topic".

那么,倒置日期背后的神话是什么?为什么Java几乎正确处理它们?什么是JDBC ResultSet 在调用 resultSet.getDate(1).getTime()

So, what's the myth behind "inverted" dates? Why does Java handle them almost correctly? And what is the sense of a JDBC ResultSet returning "inverted" millisecond values when calling resultSet.getDate(1).getTime()?

推荐答案


我发现Java几乎正确处理这些倒置值,除了星期一。

I found out that Java handles these "inverted" values almost correctly except of the week day.

在你的第一个例子中,两个日期不一样 - 一个是BC而另一个是AD(这解释了为什么星期几不同):

In your first example, the two dates are not the same - one is BC and the other one AD (which explains why the day of the week is different):

Date d1 = new Date(253402214400000l);
Date d2 = new Date(-377648784000000l);
DateFormat fmt = new SimpleDateFormat("yyyy G");
System.out.println(fmt.format(d1)); //9999 AD
System.out.println(fmt.format(d2)); //9999 BC

所以你的观察只是一个巧合(但是某个地方可能有一个日期格式化程序已经疯狂,否定了数年或数年内数据可能实际为负数。)

So your observation is just a coincidence (however there may be a date formatter somewhere that has gone wild and negates the years or the years may actually be negative in your database).

与在线转换器的差异可能是由于第0年如何被采用帐户和/或用于计算的日历中的变体。

The difference with online converters is probably due to how the year 0 is taken into account and/or variations in the calendar used for the calculations.

这篇关于java.sql.Date如何处理负日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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