【问题标题】:Make a union of two Joda-Time Interval objects合并两个 Joda-Time Interval 对象
【发布时间】:2013-12-27 18:41:59
【问题描述】:

Joda-Time 中,有没有办法将Interval 创建为其他两个区间的联合。我知道有一种叫做overlap(和overlaps)的交集方法。但我看不到任何联合方法。

【问题讨论】:

  • 什么是两个时间间隔的“并集”?如果它们是连续的(一个在确切的时刻开始,另一个在结束的那一刻开始),您可能可以加入它们,但如果它们不是,它们只是两个时间间隔。你想解决什么问题?
  • 正如@JimGarrison 所说,您不能期望合并每一对间隔。如果存在间隙,那么您需要一个额外的类型来表示一组非连续间隔。这种类型在 JodaTime 中不存在,也没有相应的联合方法来传递数组或集合或类似的。
  • 我只是想获得包含两者的区间

标签: java date datetime jodatime


【解决方案1】:

如果您想要一个“覆盖”其他两个区间,只需创建一个从 min(a.start, b.start)max(a.end, b.end) 的新区间。

如果您还需要表示差距,那么您需要编写自己的类来处理您想要的行为。 Joda-time 没有为此内置任何内容,因为对于“联合”非连续间隔的含义有几种可能的解释。

【讨论】:

    【解决方案2】:

    answer by Jim Garrison 是正确的,但很简短。我决定尝试一个实现。在 Mac 上使用 Joda-Time 2.3 和 Java 7。

    如果你经常使用这种“联合”方法,也许你应该考虑创建一个Interval 的子类来添加一个方法,在该方法中你传递一个(第二个)间隔来与第一个进行比较正在调用谁的方法。

    对于偶尔的使用,在某个实用程序类上使用静态方法就足够了。这就是我在这里写的,一个传递一对间隔的静态方法。返回一个新的间隔。

    我的示例代码没有使用多行if 语句,而是使用?: ternary operator 将拉动第一或第二DateTime 的决定折叠到一行。

    静态方法……

    // © 2013 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for doing so.
    
    static Interval union( Interval firstInterval, Interval secondInterval ) 
    {
        // Purpose: Produce a new Interval instance from the outer limits of any pair of Intervals.
    
        // Take the earliest of both starting date-times.
        DateTime start =  firstInterval.getStart().isBefore( secondInterval.getStart() )  ? firstInterval.getStart() : secondInterval.getStart();
        // Take the latest of both ending date-times.
        DateTime end =  firstInterval.getEnd().isAfter( secondInterval.getEnd() )  ? firstInterval.getEnd() : secondInterval.getEnd();
        // Instantiate a new Interval from the pair of DateTime instances.
        Interval unionInterval = new Interval( start, end );
    
        return unionInterval;
    }
    

    示例用法……

    // © 2013 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for doing so.
    // import org.joda.time.*;
    // import org.joda.time.format.*;
    
    // Note the various time zones.
    Interval i1 = new Interval( new DateTime( 2013, 1, 1, 0, 0, 0, DateTimeZone.forID( "America/Montreal" ) ), new DateTime( 2013, 1, 5, 0, 0, 0, DateTimeZone.forID( "America/Montreal" ) ) );
    Interval i2 = new Interval( new DateTime( 2013, 1, 10, 0, 0, 0, DateTimeZone.forID( "Europe/Paris" ) ), new DateTime( 2013, 1, 15, 0, 0, 0, DateTimeZone.forID( "Europe/Paris" ) ) );
    
    Interval i3 = TimeExample.union( i1, i2 );
    

    转储到控制台...

    System.out.println("i1: " + i1 );
    System.out.println("i2: " + i2 );
    // Note that Joda-Time adjusts the ending DateTime's time zone to match the starting one.
    System.out.println("i3: " + i3 );
    

    运行时……

    i1: 2013-01-01T00:00:00.000-05:00/2013-01-04T18:00:00.000-05:00
    i2: 2013-01-10T00:00:00.000+01:00/2013-01-15T00:00:00.000+01:00
    i3: 2013-01-01T00:00:00.000-05:00/2013-01-14T18:00:00.000-05:00
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-04-04
      • 2015-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多