确定间隔中是否包含星期三下午3点?

时间:2012-12-04 18:09:00

标签: jodatime

这个问题非常明显。我想创建一个新的Interval(然后,现在)并查看是否在该间隔内包含任何星期三下午3点(或任何其他星期/时间组合日)。为了清楚起见,我对Interval中是否包含特定的DateTime不感兴趣,而只是周/时组合的通用日期。

在Joda中有优雅/干净的方法吗?

1 个答案:

答案 0 :(得分:0)

   DateTime fromDate = new DateTime(); // begin interval
   DateTime toDate = fromDate.plusMinutes(999999); // end inteval
   Interval interval = new Interval(fromDate, toDate); // interval
   DateTime anchor = fromDate.minusWeeks(1).withDayOfWeek(DateTimeConstants.WEDNESDAY).withMillisOfDay(
      0).withHourOfDay(15); // Wednesday 3pm from previous week

   while (anchor.isBefore(toDate))
   {
      if (interval.contains(anchor))
      {
         return anchor; // if interval contains Wednesday 3pm - > return it
      }
      else
      {
         anchor = anchor.plusWeeks(1); // else get next Wednesday 3pm from next week  
      }
   }
   return null; // Wednesday 3pm isn't in interval