【问题标题】:Calculating time difference between 2 dates in JodaTime计算JodaTime中2个日期之间的时间差
【发布时间】:2019-08-09 09:00:07
【问题描述】:

我使用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));        
}

【问题讨论】:

    标签: java jodatime


    【解决方案1】:

    3个月的时间和几天还是有3个月的区别。 因此,如果您想将其设为 3 个月(含 3 个月),您可以在最后一天添加一天并使用小于(而不是小于或等于):

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

    另外,请注意第二个日期应该晚于第一个日期(否则它将不起作用)。

    【讨论】:

      【解决方案2】:

      如果我理解您的比较必须是总天数而不是月份,因为例如让我们将今天的日期视为第一个日期和第二个日期,再加上 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】:

        似乎最简单的方法是将 3 个月后的成绩(包括过去和未来)与另一个日期进行比较。

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2010-11-08
          • 1970-01-01
          • 2014-10-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-11-25
          相关资源
          最近更新 更多