在一组日期中查找时间空间 [英] Find a space of time in a set of dates

查看:72
本文介绍了在一组日期中查找时间空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我来这里有一个我想分享的问题,希望有人能帮助我解决这个问题。我将尽力描述问题。问题如下。

I come here with a problem I would like to share, I hope anyone can help me to solve this. I'll try to describe the problem as clear as possible. The problem is as follows.

我在Java中有一个程序,带有一个接收一组日期的方法(java.util.Date)。

I have a program in java, with a method that receives a set of dates (java.util.Date).

| start    end  |
| date1    date1|
<--------------->
|       | start        end  |     |                   |
|       | date2        date2|     |                   |
|       <------------------->     |                   |
|                                 | start        end  |
|                                 | date3        date3|
|                                 <------------------->

在上面的示例中,我们有三个日期,其中前两个相交,但是start-date3是结束日期之后2。对于我的业务规则,这是一个时间间隔。

In the example above, we have three dates, where the first two intersect, but start-date3 is after end-date2. For my business rule, here's an space of time.

现在考虑下一种情况。

| start    end  |
| date1    date1|
<--------------->
|       | start        end  |     |                   |
|       | date2        date2|     |                   |
|       <------------------->     |                   |
|                                 | start        end  |
|                                 | date3        date3|
|                                 <------------------->   
|  |                                                      |
|  | start                                           end  |
|  | date4                                           date4|
|  <------------------------------------------------------>

在这种情况下,即使end-date2和start-date3之间有一定的时间间隔,认为它不存在时间空间,因为start-date4和end-date4之间的时间涵盖了这样的空间。

In this case, even when there's an space of time between end-date2 and start-date3, it's considered that it doesn't exist a space of time, because the time between start-date4 and end-date4 covers such space.

我想检索true,如果有一个或多个时间间隔,否则我将返回false。

I would like to retrieve true, if there's one or more spaces of time, otherwise I will return false.

我尝试过的唯一方法是循环每个开始/结束关系,比较end-date1与start -date2 vs start-date3等等……这不是我想要应用的。

The only way I've tried is loop every start/end relationship, comparing end-date1 vs start-date2 vs start-date3 and so on ... that's not what I would like to apply.

如果还有其他想法,欢迎大家提出。如果您需要更多信息,请添加。谢谢。

If there's some other ideas, they are all welcome. If you need more information, I'll add it. Thank you.

推荐答案

好的,这确实是一个怪异的想法,但是看看这个概念是否更好。

Okay, this is a really "weird" idea, but see if this "concept" is any better.

基本上,这个想法是将所有重叠的日期尽可能合并为几个范围,这意味着,在第一个示例中,您将得到两个不同的范围(从开始日期1到结束日期2和(开始日期3到结束日期3)),而在第二秒内,您将得到一个范围(从开始日期1到结束日期4)

Basically, the idea is to merge all the overlapping dates into a few "ranges" as possible, this means, that in your first example, you would end up with two distinct ranges (start date 1 to end date 2 and (start date 3 to end date 3) and in your second you would end up with one (start date 1 to end date 4)

因此,如果该集合仅具有一个不同的范围,那么您就没有间隙,否则,您就没有。

So, if the set only has one distinct range, then you have no gaps, other wise, you do.

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

public class TestDateRanges {

    public static void main(String[] args) {
        try {
            List<Date[]> dates = new ArrayList<>(
                    Arrays.asList(
                            new Date[][]{
                                {makeDate("1.1.2015"), makeDate("5.1.2015")},
                                {makeDate("3.1.2015"), makeDate("10.1.2015")},
                                {makeDate("15.1.2015"), makeDate("20.1.2015")},}
                    )
            );

            Collections.sort(dates, new Comparator<Date[]>() {
                @Override
                public int compare(Date[] o1, Date[] o2) {
                    return o1[0].compareTo(o2[0]);
                }
            });

            List<Date[]> ranges = new ArrayList<>(dates.size());
            Date[] baseRange = null;
            for (Date[] range : dates) {
                if (baseRange == null) {
                    baseRange = range;
                    ranges.add(baseRange);
                } else if ((baseRange[0].before(range[0]) || baseRange[0].equals(range[0])) && (baseRange[1].after(range[0]) || baseRange[1].equals(range[0])) {
                    System.out.println("> Overlap " + format(baseRange) + " <-> " + format(range));
                    if (range[1].after(baseRange[1])) {
                        baseRange[1] = range[1];
                    }
                } else {
                    System.out.println("> Out of range " + format(baseRange) + " >-< " + format(range));
                    baseRange = range;
                    ranges.add(baseRange);
                }
            }

            System.out.println("Has " + ranges.size() + " distinct ranges");

            for (Date[] range : ranges) {
                System.out.println(format(range));
            }
        } catch (ParseException exp) {
            exp.printStackTrace();
        }
    }

    public static final DateFormat FORMAT = new SimpleDateFormat("d.M.yyyy");

    protected static final Date makeDate(String value) throws ParseException {
        return FORMAT.parse(value);
    }

    private static String format(Date[] baseRange) {
        return FORMAT.format(baseRange[0]) + "->" + FORMAT.format(baseRange[1]);
    }

    private static Date[] makeDateRange(String from, String to) throws ParseException {
        return new Date[]{makeDate(from), makeDate(to)};
    }

}

哪个输出...

> Overlap 1.1.2015->5.1.2015 <-> 3.1.2015->10.1.2015
> Out of range 1.1.2015->10.1.2015 >-< 15.1.2015->20.1.2015
Has 2 distinct ranges
1.1.2015->10.1.2015
15.1.2015->20.1.2015

现在,如果我将集合更改为...

Now, if I change the set to...

List<Date[]> dates = new ArrayList<>(
        Arrays.asList(
                new Date[][]{
                    {makeDate("1.1.2015"), makeDate("5.1.2015")},
                    {makeDate("3.1.2015"), makeDate("10.1.2015")},
                    {makeDate("15.1.2015"), makeDate("20.1.2015")},
                    makeDateRange("2.1.2015", "25.1.2015")
                }
        )
);

它会输出...

> Overlap 1.1.2015->5.1.2015 <-> 2.1.2015->25.1.2015
> Overlap 1.1.2015->25.1.2015 <-> 3.1.2015->10.1.2015
> Overlap 1.1.2015->25.1.2015 <-> 15.1.2015->20.1.2015
Has 1 distinct ranges
1.1.2015->25.1.2015

这只是一个概念性的想法,您应该注意,该示例将更改 dates 列表中的数据,因此您需要确保首先拥有数据副本

This is just a conceptual idea and you should beware, that this example will change the data in the dates list, so you will want to be sure that you have a copy of the data first

这篇关于在一组日期中查找时间空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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