我想計算一個事件的結束日期(和時間)。我知道開始日期和持續時間(以分鐘計)。但是:跳過節假日計算結束日期+ Joda時間
- 我必須跳過假期 - 非經常性形勢
- 我必須跳過週末 - 復發情況
- 我必須不計工作時間(例如:上午8:00至5 :00pm) - 經常出現,但粒度更精細
有沒有簡單的方法來實現這些使用Joda時間庫的情況?
我想計算一個事件的結束日期(和時間)。我知道開始日期和持續時間(以分鐘計)。但是:跳過節假日計算結束日期+ Joda時間
有沒有簡單的方法來實現這些使用Joda時間庫的情況?
Jodatime將幫助您 - 很多我會說 - 但您需要自己編寫邏輯,一個循環跳過一整天和幾次的一天。不是很簡單,也不是很複雜,在我看來。
首先你必須定義「假期」。並非每個區域都具有相同的區域,因此必須將其設置爲通用和可插入的。
我不認爲這很「簡單」。
在我的情況下,假期存儲在數據庫中。但我想指出,我不能使用:if(dayofweek()== 7){skip(); } – picca 2010-05-02 07:35:48
你看過Holiday calculation項目嗎?它在jodatime的相關項目中有所體現,並且可能很有用
看起來非常好。但我仍然不知道如何處理工作時間。我基本上需要以N分鐘快速前進給定日期。跳過工作時間,假期等... – picca 2010-05-02 08:37:56
下面是我使用的一些代碼。 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;
}
標記爲已接受的答案,因爲這是真的。根據我的研究,沒有簡單的解決方案。 Jodatime非常有幫助。 – picca 2010-05-04 18:15:38