public Duration getWork ( Date startDate , Date endDate , TimeUnit format ) { DateRange range = new DateRange ( startDate , endDate ) ; Long cachedResult = m_workingDateCache . get ( range ) ; long totalTime = 0 ; if ( cachedResult == null ) { // // We want the start date to be the earliest date, and the end date // to be the latest date. Set a flag here to indicate if we have swapped // the order of the supplied date. // boolean invert = false ; if ( startDate . getTime ( ) > endDate . getTime ( ) ) { invert = true ; Date temp = startDate ; startDate = endDate ; endDate = temp ; } Date canonicalStartDate = DateHelper . getDayStartDate ( startDate ) ; Date canonicalEndDate = DateHelper . getDayStartDate ( endDate ) ; if ( canonicalStartDate . getTime ( ) == canonicalEndDate . getTime ( ) ) { ProjectCalendarDateRanges ranges = getRanges ( startDate , null , null ) ; if ( ranges . getRangeCount ( ) != 0 ) { totalTime = getTotalTime ( ranges , startDate , endDate ) ; } } else { // // Find the first working day in the range // Date currentDate = startDate ; Calendar cal = Calendar . getInstance ( ) ; cal . setTime ( startDate ) ; Day day = Day . getInstance ( cal . get ( Calendar . DAY_OF_WEEK ) ) ; while ( isWorkingDate ( currentDate , day ) == false && currentDate . getTime ( ) < canonicalEndDate . getTime ( ) ) { cal . add ( Calendar . DAY_OF_YEAR , 1 ) ; currentDate = cal . getTime ( ) ; day = day . getNextDay ( ) ; } if ( currentDate . getTime ( ) < canonicalEndDate . getTime ( ) ) { // // Calculate the amount of working time for this day // totalTime += getTotalTime ( getRanges ( currentDate , null , day ) , currentDate , true ) ; // // Process each working day until we reach the last day // while ( true ) { cal . add ( Calendar . DAY_OF_YEAR , 1 ) ; currentDate = cal . getTime ( ) ; day = day . getNextDay ( ) ; // // We have reached the last day // if ( currentDate . getTime ( ) >= canonicalEndDate . getTime ( ) ) { break ; } // // Skip this day if it has no working time // ProjectCalendarDateRanges ranges = getRanges ( currentDate , null , day ) ; if ( ranges . getRangeCount ( ) == 0 ) { continue ; } // // Add the working time for the whole day // totalTime += getTotalTime ( ranges ) ; } } // // We are now at the last day // ProjectCalendarDateRanges ranges = getRanges ( endDate , null , day ) ; if ( ranges . getRangeCount ( ) != 0 ) { totalTime += getTotalTime ( ranges , DateHelper . getDayStartDate ( endDate ) , endDate ) ; } } if ( invert ) { totalTime = - totalTime ; } m_workingDateCache . put ( range , Long . valueOf ( totalTime ) ) ; } else { totalTime = cachedResult . longValue ( ) ; } return convertFormat ( totalTime , format ) ; }
This method retrieves a Duration instance representing the amount of work between two dates based on this calendar .