Java中的日期间隔求和和减法

时间:2015-01-25 21:31:44

标签: java date sum intervals subtraction

我正在寻找Java中的库或助手类,它允许我执行日期间隔求和和减法。

例如,假设我有以下日期间隔:

A = ["2015-01-01 00:00", "2015-01-20 00:00"]
B = ["2015-01-05 00:00", "2015-01-10 00:00"]
C = ["2015-01-11 00:00", "2015-01-14 00:00"]
D = ["2015-01-19 00:00", "2015-01-25 00:00"]

1                  A               20
|----------------------------------|
    |---------|   |----------|   |------------|
    5    B    10  11    C    14  19    D      25

让我们说我想计算以下内容:

A - B - C + D = { ["2015-01-01 00:00", "2015-01-05 00:00"[,
                  ]"2015-01-10 00:00", "2015-01-11 00:00"[,
                  ]"2015-01-14 00:00", "2015-01-25 00:00"] }

1   5         10  11         14               25
|---|         |---|          |----------------|

我知道我可以使用纯Java构建自己的逻辑,但我宁愿不重新发明轮子......

我正在研究Joda-Time,但我无法弄清楚如何使用它来执行此类操作。

非常感谢!

2 个答案:

答案 0 :(得分:6)

我从Ranges找到了我需要的guava-libraries

像这样工作:

Range<Date> a = Range.closed(
    new GregorianCalendar(2015, 0, 1).getTime(),
    new GregorianCalendar(2015, 0, 20).getTime());
Range<Date> b = Range.closed(
    new GregorianCalendar(2015, 0, 5).getTime(),
    new GregorianCalendar(2015, 0, 10).getTime());
Range<Date> c = Range.closed(
    new GregorianCalendar(2015, 0, 11).getTime(),
    new GregorianCalendar(2015, 0, 14).getTime());
Range<Date> d = Range.closed(
    new GregorianCalendar(2015, 0, 19).getTime(),
    new GregorianCalendar(2015, 0, 25).getTime());

RangeSet<Date> result = TreeRangeSet.create();
result.add(a);
result.remove(b);
result.remove(c);
result.add(d);

System.out.println(result);

上面的代码打印:

[
    [Thu Jan 01 00:00:00 BRST 2015‥Mon Jan 05 00:00:00 BRST 2015),
    (Sat Jan 10 00:00:00 BRST 2015‥Sun Jan 11 00:00:00 BRST 2015),
    (Wed Jan 14 00:00:00 BRST 2015‥Sun Jan 25 00:00:00 BRST 2015]
]

答案 1 :(得分:0)

我认为基本上可以使用Joda-Time和一些自定义代码来完成。假设A是所有其他间隔应该与之相关的间隔。

虽然这段代码应该给出预期的结果(并且应该相应地适用于不同的值),但我强烈建议用非常不同的数据对其进行测试,特别是对于三种情况a)一个不与A完全交叉的区间,b )在开头交叉A,c)与BCD相交的间隔。

尽管如此,它可能有助于进一步测试。

Interval a = new Interval(Instant.parse("2015-01-01T00:00Z"), Instant.parse("2015-01-20T00:00Z"));
List<Interval> l = Arrays.asList(
        /* b */ new Interval(Instant.parse("2015-01-05T00:00Z"), Instant.parse("2015-01-10T00:00Z")),
        /* c */ new Interval(Instant.parse("2015-01-11T00:00Z"), Instant.parse("2015-01-14T00:00Z")),
        /* d */ new Interval(Instant.parse("2015-01-19T00:00Z"), Instant.parse("2015-01-25T00:00Z"))
);
List<Interval> results = new ArrayList<Interval>();

for (Interval i : l) {
    if (a.contains(i)) {
        // if i is completely inside a, then calculate the first part and the remaining part
        //   whereas the first part will be added to the result
        Interval firstPart = new Interval(a.getStart(), i.getStart());
        results.add(firstPart);
        // followed by i itself (skipped)
        // part after i, inside a
        Interval remainingPart = new Interval(i.getEnd(), a.getEnd());
        a = remainingPart;
    } else if (i.overlaps(a)) {
        // if the intervals only overlap, then we take the earliest beginning and the latest ending as a result part
        DateTime overlapMin = (a.getStart().isBefore(i.getStart())) ? a.getStart() : i.getStart();
        DateTime overlapMax = (a.getEnd().isAfter(i.getEnd())) ? a.getEnd() : i.getEnd();
        Interval overlapAndBothParts = new Interval(overlapMin, overlapMax);
        results.add(overlapAndBothParts);
        // if the checked interval i is at the beginning, then a will become the part after this "overlap"
        if (i.getStartMillis() < a.getStartMillis()) {
            Interval whatsLeft = new Interval(i.getEndMillis(), a.getEndMillis());
            a = whatsLeft;
        }
    }
}

// print result
for (Interval i : results) {
    System.out.println("result part: " + i);
}