public static int [ ] getElapsedTime ( Date first , Date second ) { if ( first . compareTo ( second ) == 1 ) { return null ; } int difDays = 0 ; int difHours = 0 ; int difMinutes = 0 ; Calendar c = Calendar . getInstance ( ) ; c . setTime ( first ) ; int h1 = c . get ( Calendar . HOUR_OF_DAY ) ; int m1 = c . get ( Calendar . MINUTE ) ; c . setTime ( second ) ; int h2 = c . get ( Calendar . HOUR_OF_DAY ) ; int m2 = c . get ( Calendar . MINUTE ) ; if ( sameDay ( first , second ) ) { difHours = h2 - h1 ; } else { difDays = getNumberOfDays ( first , second ) - 1 ; if ( h1 >= h2 ) { difDays -- ; difHours = ( 24 - h1 ) + h2 ; } else { difHours = h2 - h1 ; } } if ( m1 >= m2 ) { difHours -- ; difMinutes = ( 60 - m1 ) + m2 ; } else { difMinutes = m2 - m1 ; } int [ ] result = new int [ 3 ] ; result [ 0 ] = difDays ; result [ 1 ] = difHours ; result [ 2 ] = difMinutes ; return result ; }
Get elapsedtime between two dates