簡體   English   中英

計算JodaTime中兩個日期之間的時差

[英]Calculating time difference between 2 dates in JodaTime

我使用JodaTime ,我需要檢查2個日期是否在3個月之間。 所以我寫了簡單的方法來檢查

  private boolean inRangeOf3Months(Pair<BusinessData, BusinessData> pair) {     
        return pair.getKey().getDateTimeValue() != null && pair.getValue().getDateTimeValue() != null ? 
            new Period(pair.getKey().getDateTimeValue(), pair.getValue().getDateTimeValue()).getMonths() <= 3 : false;
    }

現在我正在編寫測試,這個很好

@Test
public void shouldReturnTrueWhenInRangeOf3Months() {          
    BusinessData closingDateFrom = businessData("closingDateFrom");
    closingDateFrom.setDateTimeValue(DateTime.now());

    BusinessData closingDateTo = businessData("closingDateTo");
    closingDateTo.setDateTimeValue(DateTime.now().plusMonths(3));

    ReportingSearchCriteria criteria = criteriaOf(closingDateFrom, closingDateTo);      

    Assert.assertTrue(validator.isSufficient(criteria));        
}

但是那個不是,我將第一個日期設置為now() ,第二個日期設置為now().plusMonths(3).plusDays(1) 因此,在我的范圍之內,不應該允許它。

@Test
public void shouldReturnFalseWhenOverRangeOf3Months() {          
    BusinessData closingDateFrom = businessData("closingDateFrom");
    closingDateFrom.setDateTimeValue(DateTime.now());

    BusinessData closingDateTo = businessData("closingDateTo");
    closingDateTo.setDateTimeValue(DateTime.now().plusMonths(3).plusDays(1));

    ReportingSearchCriteria criteria = criteriaOf(closingDateFrom, closingDateTo);        

    Assert.assertFalse(validator.isSufficient(criteria));        
}

3個月和幾天的時間仍然相差3個月。 因此,如果您希望將其設置為3個月(包括3個月),則可以將最后一天添加一天,並使用少於(而不是少於或等於)的日期:

new Period(pair.getKey().getDateTimeValue(), pair.getValue().getDateTimeValue().plusDays(1)).getMonths() < 3

另外,請注意,第二個日期應晚於第一個日期(否則它將不起作用)。

如果我理解您的比較必須是總天數而不是月數,因為例如讓我們考慮今天的日期為第一天和第二天,加上3個月加25天

DateTime date1 = DateTime.now();
System.out.println(date1);
DateTime date2 = DateTime.now().plusMonths(3).plusDays(25);
System.out.println(date2);
Period p = new Period(date1,date2);
System.out.println(p.getMonths());

輸出量

2019-08-09T04:31:07.400-05:00
2019-12-04T04:31:07.449-06:00
3

不過它返回幾個月不同的是3 ,因為差為3個月零25天分別為4個月還沒有完成。 現在,如果您將3個月加30天添加到第二個日期,它將返回差額為4個月(有時您可能需要添加31天,因為這取決於具有31天的月份)

DateTime date1 = DateTime.now();
System.out.println(date1);
DateTime date2 = DateTime.now().plusMonths(3).plusDays(30);
System.out.println(date2);
Period p = new Period(date1,date2);
System.out.println(p.getMonths());

輸出量

2019-08-09T04:34:29.530-05:00
2019-12-09T04:34:29.579-06:00
4

通過上述方法,您甚至都無法花費兩天的時間來檢查差異是否為3個月,因為在某些情況下,第二個日期可能會返回第三個月(例如,如果日期是一個月中,並且如果您加上兩個天)。

我建議比較總天數,例如兩天之間的總天數> 90或> 91,可以使用Duration

DateTime date1 = DateTime.now();
System.out.println(date1);
DateTime date2 = DateTime.now().plusMonths(3).plusDays(1);
System.out.println(date2);
Duration d = new Duration(date1, date2);
System.out.println(d.getStandardDays());

輸出量

2019-08-09T04:40:47.334-05:00
2019-11-10T04:40:47.381-06:00
93

似乎最簡單的方法是將3個月的離開標記(過去和將來)與另一個日期進行比較。

public boolean areWithin3Months(DateTime dateTime1, DateTime dateTime2) {
    return !dateTime1.minusMonths(3).isAfter(dateTime2)
        && !dateTime1.plusMonths(3).isBefore(dateTime2);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM