6

イベントの終了日(および時間)を計算したいと思います。開始日期間(分単位)を知っています。しかし:

  1. 休日をスキップする必要がある - 再発しない状況
  2. 週末をスキップする必要がある - 再発状況
  3. 勤務時間を数えなくてもよい(例: 午前 8 時から午後 5 時まで) - 繰り返し発生する状況ですが、より細かい粒度で

Joda タイム ライブラリを使用してこれらのケースを実現する簡単な方法はありますか?

4

4 に答える 4

2

休日計算プロジェクトを調べましたか? jodatime の関連プロジェクトで取り上げられており、役に立つ可能性があります。

于 2010-05-01T19:50:28.907 に答える
2

Jodatime はあなたを助けてくれるでしょう - 私が言いたいことはたくさんありますが - ロジックを自分で書く必要があります。それほど単純でもなく、複雑でもないように思えます。

于 2010-05-03T13:48:23.727 に答える
1

まず、「休日」を定義する必要があります。すべてのロケールに同じものがあるわけではないため、これは汎用的でプラグ可能にする必要があります。

「単純」ではないと思います。

于 2010-05-01T19:49:05.230 に答える
0

ここに私が使用するいくつかのコードがあります。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;
}
于 2013-05-22T10:50:28.367 に答える