Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
857 views
in Technique[技术] by (71.8m points)

postgresql - Java - date saved as the day before

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);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...