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

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

问题描述

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



db关系的date属性是日期类型(不是时间戳)。一些日期在数据库中保存一天。问题只发生在间隔之间的日期,例如在31.03.1968和27.10.1968之间,这让我想起了一些夏季时间问题。但是,因为在1969年没有发生,例如,我不能很好地分离这个问题。我试图找到问题发生的其他日期间隔。 例如,如果我在应用程序中选择19.05.1968,保存在数据库中后,日期将保存为18.05.1968。



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




  • glassfish配置;

  • java(java.util.Date implementation?);

  • 我缺少某种服务器配置



我试图设置到欧洲/罗马(我的时区)我的服务器可能的每个配置,但没有。任何想法?我如何解决或调查这个问题?



更新:
1968年是一个闰年。问题也发生在1972年,这又是一个闰年。总结:



代码部分中的⽇期保存 - 前一天问题发生在闰年期间。创建日期oject是:

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

其中dateField被声明为:

  transient private DatePicker dateField; 

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



接受答案后的更新:



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

  final long hours12 = 12L * 60L * 60L * 1000L; 
日期d =新日期(d1.getTime()+ hours12);


解决方案

只需将日期设置为12:00 (而不是默认的0:00),你应该没事。问题是,GWT时区库不包括从1990年以前的所有闰年,因此您将在服务器处收到错误的时间(因为该值以时间戳的形式发送并且关闭一小时)。 / p>

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


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).

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 configuration;
  • java (java.util.Date implementation?);
  • some kind of server configuration I'm missing

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?

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

where dateField is declared as:

transient private DatePicker dateField;

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

UPDATE after accepting the answer:

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

解决方案

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).

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