Java - date saved as the day before

前端 未结 3 1120
遇见更好的自我
遇见更好的自我 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: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.

提交回复
热议问题