Java - 日期保存为前一天 [英] Java - date saved as the day before

查看:17
本文介绍了Java - 日期保存为前一天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在数据库中保存日期时遇到了一个非常奇怪的行为.在我的 (Linux centOS 6.2) 服务器上,我使用 glassfish 应用程序服务器 (3.1.1 - build 12) 和 Java (1.7.0_09),该应用程序是用 Java + GWT 开发的,它使用 PostgreSQL 服务器 (9.2.1).在应用程序内部,有几个日期字段保存在数据库中.日期字段使用 datepicker (http://code.google.com/p/gwt-datepicker, r30).

I'm experiencing a very weird behaviour while saving dates on database. On my (Linux centOS 6.2) server I use glassfish application server (3.1.1 - build 12) and Java (1.7.0_09), the application is developed in Java + GWT, and it uses PostgreSQL server (9.2.1). Inside the application there are several date fields that are saved on the db. The date fields use datepicker (http://code.google.com/p/gwt-datepicker, r30).

db 关系的日期属性是日期类型(不是时间戳).一些日期在数据库中保存一天前.问题仅发生在间隔之间的日期,例如在 31.03.1968 和 27.10.1968 之间,这让我想到了某种夏令时问题.但由于它不会发生在 1969 年,例如,我不能很好地隔离问题.我试图找到问题发生的其他日期间隔.例如,如果我在应用程序中选择 19.05.1968,则在保存到数据库中后,日期将保存为 18.05.1968.

The date attribute of the db relation is date type (not timestamp). Some dates are saved a day before in the database. The problem happens only for dates between intervals, e.g. between 31.03.1968 and 27.10.1968, which makes me think some kind of summer time issue. But since it doesn't happen for 1969, for example, I cannot isolate the problem very well. I'm trying to find some other date interval inside which the problem happens. For example, if I select 19.05.1968 in the application, after saving in the database the date is saved as 18.05.1968.

奇怪的是,我在另一台服务器上有同一个应用程序的另一个实例,并且对于相同的日期,它们被正确保存.这让我认为问题可能依赖于:

The weird thing is that I have another istance of the same application on another server, and for the same dates they are saved correctly. This makes me think that the problem could rely either on:

  • glassfish 配置;
  • java(java.util.Date 实现?);
  • 我缺少某种服务器配置

我尝试将服务器的所有可能配置都设置为欧洲/罗马(我的时区),但没有.任何的想法?我该如何解决或调查此问题?

I tried to set to Europe/Rome (my timezone) every configuration possible of my server, but nothing. Any idea? How could I solve or investigate this problem?

更新:1968年是闰年.这个问题也发生在 1972 年,这又是一个闰年.总结:日期保存一天前"问题发生在夏令时日期间隔的闰年.

UPDATE: 1968 was a leap year. The problem also happens in 1972, which is again a leap year. Summarizing: the "date-saved-one-day-before" problem happens in leap years during summer time date interval.

创建日期对象的代码部分是:

The code portion where the date oject is created is:

Date d = dateField.getSelectedDate();
if (d != null) {
    txtVal = DateTimeFormat.getFormat("dd/MM/yyyy").format(d);
}

其中 dateField 声明为:

where dateField is declared as:

transient private DatePicker dateField;

包是org.zenika.widget.client.datePicker.DatePicker(前面提到的gwt-datepicker-r30),DateTimeFormat指的是com.google.gwt.i18n.shared.DateTimeFormat

The package is org.zenika.widget.client.datePicker.DatePicker (gwt-datepicker-r30 mentioned before), and DateTimeFormat refers to com.google.gwt.i18n.shared.DateTimeFormat

接受答案后更新:

我使用了这个解决方法:创建日期时,我使用以下代码:

I used this workaround: when I create a date, I use the following code:

final long hours12 = 12L * 60L * 60L * 1000L;
Date d = new Date(d1.getTime() + hours12);

推荐答案

只需将日期时间设置为 12:00(而不是默认的 0:00)就可以了.问题是 GWT 时区库不包括 1990 年之前的所有闰年,因此您将在服务器上得到错误的时间(因为该值以时间戳的形式发送并且是一个小时).

Just set the time of the date to 12:00 (instead of the default 0:00) and you should be fine. The issue is that the GWT time zone library does not include all leap years from before 1990, and thus you will get a wrong time at the server (since the value is sent in the form of a timestamp and is one hour off).

顺便说一句:GWT 有一个内置的日期选择器,请在 http://gwt.google.com/samples/Showcase/Showcase.html#!CwDatePicker

By the way: GWT has a built-in date picker, see its demo at http://gwt.google.com/samples/Showcase/Showcase.html#!CwDatePicker

这篇关于Java - 日期保存为前一天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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