यहां कुछ कोड है जिसका मैं उपयोग करता हूं। dtDateTimes
में आपकी पूर्व परिभाषित अवकाश तिथियां हो सकती हैं (उदा। यूके बैंक छुट्टियां) और dtConstants
में पुनरावर्ती चीजें हो सकती हैं, जिन्हें आप DateTimeConstants.SATURDAY
के साथ मेल करना चाहते हैं।
/**
* Returns a tick for each of
* the dates as represented by the <code>dtConstants</code> or the list of <code>dtDateTimes</code>
* occurring in the period as represented by begin -> end.
*
* @param begin
* @param end
* @param dtConstants
* @param dtDateTimes
* @return
*/
public int numberOfOccurrencesInPeriod(final DateTime begin, final DateTime end, List<Integer> dtConstants, List<DateTime> dtDateTimes) {
int counter = 0;
for (DateTime current = begin; current.isBefore(end); current = current.plusDays(1)) {
for (Integer constant : dtConstants) {
if (current.dayOfWeek().get() == constant.intValue()) {
counter++;
}
}
for (DateTime dt : dtDateTimes) {
if (current.getDayOfWeek() == (dt.getDayOfWeek())) {
counter++;
}
}
}
return counter;
}
/**
* Returns true if the period as represented by begin -> end contains any one of
* the dates as represented by the <code>dtConstants</code> or the list of <code>dtDateTimes</code>
*
* @param begin
* @param end
* @param dtConstants
* @param dtDateTimes
*/
public boolean isInPeriod(final DateTime begin, final DateTime end, List<Integer> dtConstants, List<DateTime> dtDateTimes) {
return numberOfOccurrencesInPeriod(begin, end, dtConstants, dtDateTimes) > 0;
}
स्वीकृत उत्तर के रूप में चिह्नित, क्योंकि यह सच है। मेरे शोध के अनुसार, कोई आसान समाधान नहीं है। और जोडाटाइम निश्चित रूप से सहायक है। – picca