public String getRandomHoliday ( String earliest , String latest ) { String dateString = "" ; DateTimeFormatter parser = ISODateTimeFormat . date ( ) ; DateTime earlyDate = parser . parseDateTime ( earliest ) ; DateTime lateDate = parser . parseDateTime ( latest ) ; List < Holiday > holidays = new LinkedList <> ( ) ; int min = Integer . parseInt ( earlyDate . toString ( ) . substring ( 0 , 4 ) ) ; int max = Integer . parseInt ( lateDate . toString ( ) . substring ( 0 , 4 ) ) ; int range = max - min + 1 ; int randomYear = ( int ) ( Math . random ( ) * range ) + min ; for ( Holiday s : EquivalenceClassTransformer . HOLIDAYS ) { holidays . add ( s ) ; } Collections . shuffle ( holidays ) ; for ( Holiday holiday : holidays ) { dateString = convertToReadableDate ( holiday . forYear ( randomYear ) ) ; if ( toDate ( dateString ) . after ( toDate ( earliest ) ) && toDate ( dateString ) . before ( toDate ( latest ) ) ) { break ; } } return dateString ; }
Grab random holiday from the equivalence class that falls between the two dates