[英]How can I check that a two dates are between a period(interval)?
我的輸入是開始日期和結束日期。 我想檢查一下它是在12月1日到3月31日之間。(年份可以更改,並且只有此日期或該日期以外的日期)。
到目前為止,Joda-time還沒有找到任何解決方案。 有人可以給我一個起點怎么做(不是代碼,只是邏輯)? 我尚未檢查代碼,但是非常丑陋,我想找到一種算法解決方案
public static boolean isInWinter(Contract contract) {
logger.info("begin isInWinter:");
DateTime beginDate = contract.getBeginningDate();
DateTime endDate = contract.getEndDate();
/*
* If the year is different (etc 2012 dec,2013 marc) check that the
* beginning month is december, and the end month is jan,feb,marc
*/
if (endDate.getYear() - beginDate.getYear() == 1) {
if ((beginDate.getMonthOfYear() == 12)
&& ((endDate.getMonthOfYear() == 1
|| endDate.getMonthOfYear() == 2 || endDate
.getMonthOfYear() == 3))) {
logger.info("return true different year");
return true;
}
/*
* Same year can be if begin and end date is december or begin and
* and date is jan,febr,marc TODO REMOVE Auto formatter
*/
} else if (endDate.getYear() - beginDate.getYear() == 0) {
if ((beginDate.getMonthOfYear() == 12 && endDate.getMonthOfYear() == 12)
|| ((beginDate.getMonthOfYear() == 1
|| beginDate.getMonthOfYear() == 2 || beginDate
.getMonthOfYear() == 3) && (endDate
.getMonthOfYear() == 1
|| endDate.getMonthOfYear() == 2 || endDate
.getMonthOfYear() == 3))) {
logger.info("return true same year");
return true;
}
}
logger.info("return false");
return false;
}
您需要的是Joda-Time及其Interval類(帶有overlap
方法)。 很容易。
與java.util.Date不同,DateTime真正知道其分配的時區。 通常,指定比依賴默認值更好。 請注意,如果要跟蹤日期和時間(DateTime而不是LocalDate),則時區至關重要。 開始和結束的“天”的定義取決於時區。 跨時區工作的企業可以選擇為此使用UTC。
僅供參考,除了Interval,Joda-Time還提供了Period和Duration類,用於處理一段時間。
當使用日期時間值但專注於“天”時,則將時間部分調整為該特定時區的一天的第一時刻。 在Joda-Time中,只需調用withTimeAtStartOfDay
方法。
避免與“午夜”相關的類和方法。 Joda-Time團隊不再推薦它們。
比較時間跨度時,Joda-Time使用“半開放”方法。 開頭(包括),結尾(包括)。 這意味着,由於您希望從12月1日到3月31日,我們定義12月1日這the first moment of the day of December 1
the first moment of the day of April 1
the first moment of the day of December 1
的間隔。 如果您仔細考慮一下,然后在StackOverflow上搜索“ joda interval”上的其他帖子,您就會發現這種方法的智慧。
Interval類提供了方便的比較方法: contains
, abuts
, overlap
和gap
。 請務必閱讀文檔以了解確切的詳細信息。
Java 8帶來了受JSR 310定義的,受Joda-Time啟發的新java.time軟件包。Joda-Time繼續在Java 8中工作,但是如果您不熟悉Java,則可能要學習java.time而不是Joda-Time。日期時間工作。
DateTimeZone timeZone = DateTimeZone.forID( "Europe/Budapest" ); // Or, perhaps DateTimeZone.UTC
// Inputs
DateTime contractStart = new DateTime( 2014, 1, 2, 0, 0, 0, timeZone );
DateTime contractStop = new DateTime( 2014, 5, 6, 0, 0, 0, timeZone );
Interval contractInterval = new Interval( contractStart, contractStop );
// Target to test
DateTime targetStart = new DateTime( 2013, DateTimeConstants.DECEMBER, 1, 0, 0, 0, timeZone );
DateTime targetStop = targetStart.plusMonths( 4 );
Interval targetInterval = new Interval( targetStart, targetStop );
boolean targetContainsContract = targetInterval.contains( contractInterval );
boolean targetOverlapsContract = ( targetInterval.overlap( contractInterval ) != null );
轉儲到控制台...
System.out.println( "contractInterval: " + contractInterval );
System.out.println( "targetInterval: " + targetInterval );
System.out.println( "targetContainsContract: " + targetContainsContract );
System.out.println( "targetOverlapsContract: " + targetOverlapsContract );
運行時...
contractInterval: 2014-01-02T00:00:00.000+01:00/2014-05-06T00:00:00.000+02:00
targetInterval: 2013-12-01T00:00:00.000+01:00/2014-04-01T00:00:00.000+02:00
targetContainsContract: false
targetOverlapsContract: true
使用獲取的開始日期和結束日期創建間隔。
創建從三月到十二月的間隔。
如果滿足以下任一條件,則您的條件為true:
在代碼中將是:
marchToDecember.contains(interval) || marchToDecember.overlap(interval) == null
由於您提到日期將是該期間內的兩個日期,或者都將是兩個期間,因此您只需要測試其中一個即可。
因此,只需測試beginDate.getMonthOfYear()
是否返回beginDate.getMonthOfYear()
或3。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.