Adding hours, minutes, seconds to SQL Date

前端 未结 4 1775
南旧
南旧 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:39

    As mentioned that the date stored as 2013-01-10 00:00:00 is to be converted to 2013-01-10 23:59:59.999 and then take that as end date.

    MySql

    DATE_ADD()

    When you query your date time field, you can advance your endtime by one day as follows

    DATE_ADD('your_datetime_field', INTERVAL 1 DAY)
    

    OR

    Java code

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    // rs is ResultSet reference
    long dt = rs.getTimestamp("your_datetime_field").getTime();
    // pick calendar instance and set time
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(dt);
    // this will print `2013-01-10 00:00:00`
    System.out.println(sdf.format(calendar.getTime()));
    // advance the date by 1 day
    calendar.add(Calendar.Date, 1);
    // this is print `2013-01-11 00:00:00`
    System.out.println(sdf.format(calendar.getTime()));
    

    Now, you can compare with this Calendar object as well.

    Hope this helps.

提交回复
热议问题