Combine a date and a time into a single date-time using java.util.Date


/ Published in: Java
Save to your folder(s)



Copy this code and paste it in your HTML
  1. private Date combineDateTime(Date date, Date time)
  2. {
  3. Calendar calendarA = Calendar.getInstance();
  4. calendarA.setTime(date);
  5. Calendar calendarB = Calendar.getInstance();
  6. calendarB.setTime(time);
  7.  
  8. calendarA.set(Calendar.HOUR_OF_DAY, calendarB.get(Calendar.HOUR_OF_DAY));
  9. calendarA.set(Calendar.MINUTE, calendarB.get(Calendar.MINUTE));
  10. calendarA.set(Calendar.SECOND, calendarB.get(Calendar.SECOND));
  11. calendarA.set(Calendar.MILLISECOND, calendarB.get(Calendar.MILLISECOND));
  12.  
  13. Date result = calendarA.getTime();
  14. return result;
  15. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.