イベントの終了日(および時間)を計算したいと思います。開始日と期間(分単位)を知っています。しかし:
- 休日をスキップする必要がある - 再発しない状況
- 週末をスキップする必要がある - 再発状況
- 勤務時間を数えなくてもよい(例: 午前 8 時から午後 5 時まで) - 繰り返し発生する状況ですが、より細かい粒度で
Joda タイム ライブラリを使用してこれらのケースを実現する簡単な方法はありますか?
休日計算プロジェクトを調べましたか? jodatime の関連プロジェクトで取り上げられており、役に立つ可能性があります。
Jodatime はあなたを助けてくれるでしょう - 私が言いたいことはたくさんありますが - ロジックを自分で書く必要があります。それほど単純でもなく、複雑でもないように思えます。
まず、「休日」を定義する必要があります。すべてのロケールに同じものがあるわけではないため、これは汎用的でプラグ可能にする必要があります。
「単純」ではないと思います。
ここに私が使用するいくつかのコードがあります。dtDateTimes
事前に定義された休日の日付 (例: UK Bank Holidays) を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;
}