如何检查间隔列表(Joda-Time)是否完全涵盖Java中的一个月 [英] How to check if a list of intervals (Joda-Time) fully covers a month in Java

查看:120
本文介绍了如何检查间隔列表(Joda-Time)是否完全涵盖Java中的一个月的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Java中的 Joda-Time 库来跟踪时间列表间隔。我想查看 Interval 的列表对象完全覆盖了一个月的每一分钟。从几小时到几天,大约有30个不同长度的间隔。

I'm using Joda-Time library in Java to keep track a list of time intervals. I want to check if a list of Interval objects fully covers every minute of a month. There's about 30 intervals of varying length from a few hours to a few days.

我认为一种便宜的方法是按开始时间对间隔列表进行排序,然后连续检查月份范围内的间隔是否有中断。如果是,则月份未完全覆盖。

I thought a cheap way to do this is to sort the list of intervals by start time, then successively check if there is a break between the intervals within the range of the month. If yes, then the month is not fully covered.

我被困在第一部分,对列表进行排序。我计划使用数组 .sort() ,但它需要元素来实现类似的界面。然而,在查看源代码之后,Joda-Time的Interval类似乎没有我可以覆盖的类,并且我无法扩展它来编写我自己的compareTo方法。

I'm stuck on the first part, sorting the list. I planned to use Arrays.sort(), but it needs the elements to implement the comparable interface. However, after looking through the source, Joda-Time's Interval class doesn't seem to have one I can override, and I can't extend it to write my own compareTo method.

除了编写自己的排序方法外,任何人都知道更简单的方法来完成此任务吗?谢谢

Other than writing my own sorting method, anyone know an easier way to accomplish this? Thanks

推荐答案

你可以使用下一个方法

static boolean covers(Interval month, List<Interval> intervals)
{
  //assumes intervals are sorted already on start times
  final MutableInterval monthInterval = new MutableInterval(month);
  start: for (final Interval interval : intervals)
  {
     if (interval.getStartMillis() <= monthInterval.getStartMillis()
        && interval.getEndMillis() > monthInterval.getStartMillis())
     {
        if (interval.getEndMillis() > monthInterval.getEndMillis())
        {
           return true;
        }
        monthInterval.setStartMillis(interval.getEndMillis());
        // continue start;  // loop continues regardless
     } else {
           if (interval.overlaps(month)) return false;
     }
  }
  return monthInterval.getStartMillis()== monthInterval.getEndMillis();
} 

示例

   static final List<Interval> intervals = new ArrayList<Interval>();
   static
   {
      intervals.add(new Interval(new DateTime(1990, 05, 15, 00, 00, 00, 00), new DateTime(1990, 05, 18, 00, 00, 00, 00)));
      intervals.add(new Interval(new DateTime(1990, 04, 28, 00, 00, 00, 00), new DateTime(1990, 05, 18, 00, 00, 00, 00)));
      intervals.add(new Interval(new DateTime(1990, 05, 17, 00, 00, 00, 00), new DateTime(1990, 05, 21, 00, 00, 00, 00)));
      intervals.add(new Interval(new DateTime(1990, 05, 21, 00, 00, 00, 00), new DateTime(1990, 05, 29, 00, 00, 00, 00)));
      intervals.add(new Interval(new DateTime(1990, 05, 22, 00, 00, 00, 00), new DateTime(1990, 05, 25, 00, 00, 00, 00)));
      intervals.add(new Interval(new DateTime(1990, 05, 27, 00, 00, 00, 00), new DateTime(1990, 06, 02, 00, 00, 00, 00)));
   }

   public static void main(String[] args)
   {
      final DateTime startOfMonth = new DateTime(1990, 05, 01, 00, 00);
      final Interval monthInterval = new Interval(startOfMonth, startOfMonth.plusMonths(1));

      covers(monthInterval, intervals);
   }

   static boolean covers(Interval month, List<Interval> intervals)
   {
      final MutableInterval monthInterval = new MutableInterval(month);
      start: for (final Interval interval : intervals)
      {
         if (interval.getStartMillis() <= monthInterval.getStartMillis()
            && interval.getEndMillis() > monthInterval.getStartMillis())
         {
            if (interval.getEndMillis() > monthInterval.getStartMillis())
            {
               return true;
            }
            monthInterval.setStartMillis(interval.getEndMillis());
            continue start;
         }
      }
      return monthInterval.getStartMillis()== monthInterval.getEndMillis();
   }

这篇关于如何检查间隔列表(Joda-Time)是否完全涵盖Java中的一个月的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆