将 java.util.date 与 java.sql.Time 合并
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5950806/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Merge java.util.date with java.sql.Time
提问by marcolopes
I have an extensive DATE-TIME conversion class, but i came across a scenario that i cannot resolve:
我有一个广泛的 DATE-TIME 转换类,但我遇到了一个我无法解决的场景:
I have a java.util.date: Tue May 10 00:00:00 BST 2011
I have a java.sql.time: 03:58:44
我有一个 java.util.date: Tue May 10 00:00:00 BST 2011
我有一个 java.sql.time: 03:58:44
I need to create a java.util.date: Tue May 10 03:58:44 BST 2011
我需要创建一个 java.util.date: Tue May 10 03:58:44 BST 2011
The only approach i came up with is:
我想出的唯一方法是:
public static Date getDate(Date date, Time time) {
Calendar calendar=Calendar.getInstance();
calendar.set(date.getYear(), date.getMonth(), date.getDay(), time.getHours(), time.getMinutes(), time.getSeconds());
return calendar.getTime();
}
Totally deprecated code, and does not work: java.lang.IllegalArgumentException at java.sql.Time.getYear(Unknown Source)
完全弃用的代码,并且不起作用:java.sql.Time.getYear(Unknown Source)处的java.lang.IllegalArgumentException
Any ideas?
有任何想法吗?
回答by acalypso
java.sql.Time is just a wrapper over the java.util.Date. You can use it as if you would add two java.util.Date objects.
java.sql.Time 只是 java.util.Date 的一个包装器。您可以像添加两个 java.util.Date 对象一样使用它。
For example, set Calendar to java.sql.Time:
例如,将 Calendar 设置为 java.sql.Time:
calendar.setTime(time);
Now extract the hour/minute/seconds fields, i.e.:
现在提取小时/分钟/秒字段,即:
calendar.get(Calendar.HOUR);
Next, set the Calendar to java.util.Date object and add these three fields to its time, i.e.:
接下来,将 Calendar 设置为 java.util.Date 对象并将这三个字段添加到其时间,即:
calendar.add(Calendar.HOUR, hour);
And get the Date back:
并取回日期:
calendar.getTime();
回答by vickirk
Easiest way would be to just add the milli secs together to create a new date, ala
最简单的方法是将毫秒加在一起来创建一个新的日期,ala
public static Date getDate(Date date, Time time) {
return new Date(date.getTime() + time.getTime())
}
回答by T-Bull
vickirk's solution wasn't so bad, but has timezone issues, which results in the one hour less you observed.
vickirk 的解决方案还不错,但有时区问题,这会导致您观察到的时间减少一小时。
I suppose, BST means British Summer Time, which is GMT +0100. Now, java.util.Date and its descendants internally work with numbers of milliseconds since midnight Jan 01, 1970 GMT. The timezone is not taken into account until you stringfy the date/time with toString()
. And they use your local timezone for that, which is BST, apparently. That means, what is really stored in these objects, is
我想,BST 的意思是英国夏令时,即 GMT +0100。现在, java.util.Date 及其后代在内部使用自格林威治标准时间1970 年 1 月 1 日午夜以来的毫秒数。在您使用 .stringfed 字符串化日期/时间之前,不会考虑时区toString()
。他们使用您当地的时区,显然是 BST。这意味着,真正存储在这些对象中的是
java.util.date: Mon May 09 23:00:00 GMT2011
java.sql.time: 02:58:44 GMT
java.util.date:周一5月09 23:00:00 GMT2011
的java.sql.Time:02:58:44 GMT
When you add the internal values (which are retrieved by getTime()
) like vickirk suggested, you obtain a date which contains
Tue May 10 01:58:44 GMT2011, which then results in
Tue May 10 02:58:44 BST2011 on stringification.
当您getTime()
像 vickirk 建议的那样添加内部值(由 检索)时,您将获得一个包含
Tue May 10 01:58:44 GMT2011 的日期,然后在字符串化时导致
Tue May 10 02:58:44 BST2011。
So the explanation for the one hour less is that the timezone offset applies twice, when you stringified the values separately, whereas it applies only once after the addition, because you stringfy only once now. Or, from another point of view, adding the internal value of the point in time03:58:44 BST is equivalent to adding a time spanof 2h 58m 44s.
因此,对少一小时的解释是,当您分别对值进行字符串化时,时区偏移适用两次,而在添加后仅适用一次,因为您现在只对值进行一次字符串化。或者,换个角度,加上BST时间点03:58:44的内部值,相当于加上了2h 58m 44s的时间跨度。
So to get a time spanof 3h 58m 44s encoded in a java.sql.Time, you have to make up for the time zone offset manually. You do that by parsing the time string "00:00:00" with java.sql.Time, which will result in an internal value of -3600000
which is equivalent to 31 Dec 1969 23:00:00 GMT, i.e. one hour before the epoch. This is the negative of the time zone offset.
所以要得到一个java.sql.Time编码的3h 58m 44s的时间跨度,你必须手动弥补时区偏移。您可以通过使用 java.sql.Time 解析时间字符串“00:00:00”来实现,这将导致其内部值-3600000
等于 1969 年 12 月 31 日 23:00:00 GMT,即纪元前一小时. 这是时区偏移的负数。
public static Date mergeDate(Date date, Time time) {
long tzoffset = -(Time.valueOf("00:00:00").getTime());
return new Date(date.getTime() + time.getTime() + tzoffset);
}
Of course, all this is a dirty hack, which is necessary because you insist on interpreting the Time's value as a time span, while it really is a point in time.
当然,所有这些都是一个肮脏的黑客,这是必要的,因为您坚持将 Time 的值解释为时间跨度,而它确实是一个时间点。
回答by Manoj
Instead, you can use this.
相反,您可以使用它。
public static Date getDate(Date date, Time time) {
Calendar calendar=Calendar.getInstance();
calendar.setTimeZone(TimeZone.getTimeZone("UTC"));
calendar.setTime(date);
calendar.add(Calendar.MILLISECOND, (int) time.getTime());
return calendar.getTime();
}
回答by John Vint
Can you do
你可以做
java.util.Date newDate = new java.util.Date(sqlDate.getTime());
回答by sunil
try these
试试这些
public static Date getDate(Date date, Time time) {
Calendar calendar=Calendar.getInstance();
calendar.setTime(date);
Calendar calendar1=Calendar.getInstance();
calendar1.setTime(time);
calendar.set(Calendar.MINUTE, calendar1.get(Calendar.MINUTE));
calendar.set(Calendar.SECOND, calendar1.get(Calendar.SECOND));
calendar.set(Calendar.HOUR_OF_DAY, calendar1.get(Calendar.HOUR_OF_DAY));
return calendar.getTime();
}