Java - date saved as the day before

前端 未结 3 1119
遇见更好的自我
遇见更好的自我 2020-12-14 08:48

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.

相关标签:
3条回答
  • 2020-12-14 09:23

    Are you using java.util.date or java.sql. date (the latter is the correct one)? I had a similar problem with SQL Server, even if it was regular and yes, related with the summertime.

    Basically, you store a Java date as midnight of a particular day. if summertime, the date get moved to the day before at 23:00, and then the time gets truncated! If you send the date with a "random" timestamp you will experience the problem one time out of 24 during the summer.

    I don't remember exactly the solution (which will not help you directly as it refers to a different DBMS) but there was a setting in the dB to say "store the date as you receive it". You can test if this is the case by changing the dbcolumn to timestamp and looking at how the time get stored.

    I did a bit of research - it seems the gwt-datepicker has a lot of issues! http://code.google.com/p/google-apps-script-issues/issues/detail?id=2022 http://code.google.com/p/google-apps-script-issues/issues/detail?id=2001

    I would not be surprised if their calculation had a bug on leap years. Also, it is entirely possible it is just a mismatch between what you are doing and what you think you are doing - working with Dates is a surprisingly difficult matter

    To check, try:

    final java.util.Date ud = dateField.getSelectedDate();;
    final java.sql.Date sd = new java.sql.Date(ud.getTime());
    System.out.println(ud);// this is what you pick from the DatePicker
    System.out.println(sd);// this is what will be stored on the database
    

    And see if they match during summer on leap years. If it is a GWT bug, http://code.google.com/p/google-apps-script-issues/ is the right place to report it

    0 讨论(0)
  • 2020-12-14 09:26

    Faced this issue in real time for date of birth.

    I did the below fix, so that instead of saving a date at 12 am, date will be saved at 6 am which is before business hours of server. Even an hour is deducted, it won’t affect dob.

    Calendar now = Calendar.getInstance();
    now.setTime(YOUR_DATE);
    now.set(Calendar.HOUR_OF_DAY, 6);
    YOUR_DATE = now.getTime();
    

    Mikes answer is fine. But, if we save with 12 pm, there is a possibility for error in date comparison, if user enters date during business hours before 12 pm.

    0 讨论(0)
  • 2020-12-14 09:28

    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

    0 讨论(0)
提交回复
热议问题