public static Date setTime ( Date date , Date canonicalTime ) { Date result ; if ( canonicalTime == null ) { result = date ; } else { // // The original naive implementation of this method generated // the "start of day" date (midnight) for the required day // then added the milliseconds from the canonical time // to move the time forward to the required point. Unfortunately // if the date we'e trying to do this for is the entry or // exit from DST, the result is wrong, hence I've switched to // the approach below. // Calendar cal = popCalendar ( canonicalTime ) ; int dayOffset = cal . get ( Calendar . DAY_OF_YEAR ) - 1 ; int hourOfDay = cal . get ( Calendar . HOUR_OF_DAY ) ; int minute = cal . get ( Calendar . MINUTE ) ; int second = cal . get ( Calendar . SECOND ) ; int millisecond = cal . get ( Calendar . MILLISECOND ) ; cal . setTime ( date ) ; if ( dayOffset != 0 ) { // The canonical time can be +1 day. // It's to do with the way we've historically // managed time ranges and midnight. cal . add ( Calendar . DAY_OF_YEAR , dayOffset ) ; } cal . set ( Calendar . MILLISECOND , millisecond ) ; cal . set ( Calendar . SECOND , second ) ; cal . set ( Calendar . MINUTE , minute ) ; cal . set ( Calendar . HOUR_OF_DAY , hourOfDay ) ; result = cal . getTime ( ) ; pushCalendar ( cal ) ; } return result ; }
Given a date represented by a Date instance set the time component of the date based on the hours and minutes of the time supplied by the Date instance .