将java.util.date与java.sql.Time合并 [英] Merge java.util.date with java.sql.Time
问题描述
我有一个广泛的DATE-TIME转换类,但是我遇到了一个我无法解决的场景:
I have an extensive DATE-TIME conversion class, but i came across a scenario that i cannot resolve:
我有一个java.util.date:Tue May 10 00:00:00 BST 2011
我有一个java.sql.time:03:58:44
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 03:58:44 BST 2011
我想到的唯一方法是:
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();
}
完全弃用的代码,不起作用:
java.lang 。java.sql.Time.getYearIllegalArgumentException(未知来源)
Totally deprecated code, and does not work: java.lang.IllegalArgumentException at java.sql.Time.getYear(Unknown Source)
任何想法?
推荐答案
java.sql.Time只是java.util.Date中的一个包装。你可以使用它,就像添加两个java.util.Date对象一样。
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.
例如,将Calendar设置为java.sql.Time:
For example, set Calendar to java.sql.Time:
calendar.setTime(time);
现在提取小时/分/秒/字段,即:
Now extract the hour/minute/seconds fields, i.e.:
calendar.get(Calendar.HOUR);
接下来,将Calendar设置为java.util.Date对象,并将这三个字段添加到其中,即:
Next, set the Calendar to java.util.Date object and add these three fields to its time, i.e.:
calendar.add(Calendar.HOUR, hour);
并获取日期:
calendar.getTime();
这篇关于将java.util.date与java.sql.Time合并的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!