简体   繁体   中英

Mixing java.util.date and a java.sql.Time in Java

I want to construct a date based in a java.util.date and a java.sql.Time, so I code this:

Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.HOUR_OF_DAY,time.getHours());
cal.set(Calendar.MINUTE,     time.getMinutes());
cal.set(Calendar.SECOND,     time.getSeconds());

cal.getTime();

It works but ime.getHours(), time.getMinutes(), time.getSeconds() appears as deprecated, how can we make it with a no deprecated method ????

By the looks of what you're doing, I think this is what you want:

Calendar mergedCal = Calendar.getInstance();
mergedCal.setTime(date);
Calendar sqlCal = Calendar.getInstance();
sqlCal.setTime(time.getTime());
mergedCal.set(Calendar.HOUR_OF_DAY, sqlCal.get(Calendar.HOUR_OF_DAY));
mergedCal.set(Calendar.MINUTE, sqlCal.get(Calendar.MINUTE));
mergedCal.set(Calendar.SECOND, sqlCal.get(Calendar.SECOND));
mergedCal.getTime();

At the end of this computation, mergedCal will use the date from date and the time-of-day from time .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM