I have two objects: a java.sql.Date and a java.sql.Time.
What is the best way to merge them into single java.util.Date?
In the database those columns are stored separately. I get them by JDBC rs.getDate("Date") and rs.getTime("Time");.
解决方案
You can create two Calendar instances. In the first you initialize the date and in the latter the time. You can the extract the time values from the "time" instance and set them to the "date".
// Construct date and time objects
Calendar dateCal = Calendar.getInstance();
dateCal.setTime(date);
Calendar timeCal = Calendar.getInstance();
timeCal.setTime(time);
// Extract the time of the "time" object to the "date"
dateCal.set(Calendar.HOUR_OF_DAY, timeCal.get(Calendar.HOUR_OF_DAY));
dateCal.set(Calendar.MINUTE, timeCal.get(Calendar.MINUTE));
dateCal.set(Calendar.SECOND, timeCal.get(Calendar.SECOND));
// Get the time value!
date = dateCal.getTime();