Adding hours, minutes, seconds to SQL Date

前端 未结 4 1764
南旧
南旧 2020-12-10 10:00

Good Day!

I have a problem with SQL Dates. I think the main problem is on the perspective of time. In my program, I have a start and end date, say for example

4条回答
  •  粉色の甜心
    2020-12-10 10:42

    The entire reason for java.sql.Date to exist is that it does not allow hours minutes and seconds (As defined by the ANSI SQL Date type, and pretty much universally ignored by RDMBS implementors).

    It is simply impossible to put the time in the database as long as you use java.sql.Date. If you want time, you need to switch to java.sql.Timestamp.

    If you're asking how to calculate the actual value of 'end of day', it's simplest to just go to the next day and subtract 1 millisecond.

    Calendar cal = Calendar.getInstance();
    cal.setTime(endDate);
    cal.add(Calendar.DATE, 1);
    java.sql.Timestamp endTime = new java.sql.Timestamp(cal.getTimeInMillis() -1L);
    

提交回复
热议问题