向SQL Date添加小时,分钟,秒 [英] Adding hours, minutes, seconds to SQL Date

查看:939
本文介绍了向SQL Date添加小时,分钟,秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

美好的一天!

我的SQL日期有问题。我认为主要问题是从时间的角度来看。
在我的计划中,我有一个开始和结束日期,例如 2014-01-08 2014-01-10
在日期库中,我使用了datetime,因此日期存储为 2014-01-08 00:00:00 2014-01 -10 00:00:00 分别。

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 2014-01-08 and 2014-01-10. In the datebase, I used datetime so the dates are stored as 2014-01-08 00:00:00 and 2014-01-10 00:00:00 respectively.

如果您是用户,今天是结束日期,您可以认为截止日期是午夜。但是,情况并非如此,因为小时,分钟和秒都设置为0.

If you are a user and today is the end date, you would assume that the deadline is up to midnight. However, that is not the case because the hours, minutes, and seconds are set to 0.

我想问一下,添加小时的最佳方法是什么,分钟和秒,以便结束日期为午夜。

I would like to ask, what is the best way to add the hours, minutes, and seconds so that the end date is up to midnight.

PS。我正在使用Java Sql Date。

PS. I am using Java Sql Date.

INSERT INTO survey_schedule (start_date, end_date) VALUES (startDate, endDate);

startDate和endDate是 java.sql.date 类型

startDate and endDate are java.sql.date types

推荐答案

如前所述,存储为2013-01-10 00:00:00的日期将转换为2013 -01-10 23:59:59.999然后将其作为结束日期。

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代码

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.

希望这会有所帮助。

这篇关于向SQL Date添加小时,分钟,秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆