Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java:日期比较_Java_Date - Fatal编程技术网

Java:日期比较

Java:日期比较,java,date,Java,Date,我有两张表,前一张和新一张。两个表都有3列: 目标ID、开始日期和结束日期 我需要创建一个NOT_PRESENT_IN_PREVICE_计划表,该表有3列: 目标ID、开始日期和结束日期 如果上一个计划包含以下样本数据: Objective_id --Start_Date -- End_Date 1 -- 10-Jan-2014 -- 20-Jan-2014 Objective_id -- Start_Date -- End_Date 1 -- 12-Jan-2014 -- 15-Jan-20

我有两张表,前一张和新一张。两个表都有3列: 目标ID、开始日期和结束日期

我需要创建一个NOT_PRESENT_IN_PREVICE_计划表,该表有3列: 目标ID、开始日期和结束日期

如果上一个计划包含以下样本数据:

Objective_id --Start_Date -- End_Date
1 -- 10-Jan-2014 -- 20-Jan-2014
Objective_id -- Start_Date -- End_Date
1 -- 12-Jan-2014 -- 15-Jan-2014
如果新的_计划包含以下样本数据:

Objective_id --Start_Date -- End_Date
1 -- 10-Jan-2014 -- 20-Jan-2014
Objective_id -- Start_Date -- End_Date
1 -- 12-Jan-2014 -- 15-Jan-2014
基于上述场景,我的“未出席”日程安排应包含以下数据:

Objective_id -- Start_Date --End_Date
1 -- 10-Jan-2014 -- 11-Jan-2014
1 -- 16-Jan-2014 -- 20-Jan-2014

具有NOT_PRESENT_IN_PREVIOUS_调度输出的逻辑应该用Java实现。对于任何类型的“上一个”计划和“新”计划,它都应该是通用的,作为输入,返回“上一个”计划中的“不存在”作为输出。

要将日期字符串转换为Java日期对象,可以使用该类。Java 7的解决方案:

String string = "20-Jan-2014";
DateFormat format = new SimpleDateFormat("dd-MMM-yyyy", Locale.ENGLISH);
Date date = format.parse(string);
对于Java8,您可以找到一个

要计算两个日期之间的差异,可以使用:

计算上一个\u计划和新的\u计划之间的差异

如何解决这个问题的一个想法是将前一个_计划中的日期周期转换为单个日期,存储在一个集合中

Set<String> dates = new HashSet<String>();
dates.add( "10-Jan-2014" );
dates.add( "11-Jan-2014" );
dates.add( "12-Jan-2014" );
...
dates.add( "20-Jan-2014" );
集合中的其余元素将为在上一个日程表中创建“不存在”提供基础

也可以向集合中添加日期对象,而不是使用字符串:

Set<Date> dates = new HashSet<Date>();
dates.add( date1 );

如何将2014年1月10日至2014年1月20日这样的日期段拆分为单个日期,以及如何在之前的日程安排中创建“不在场”的反向任务,都应该来自您自己的创意。提示:您可以使用循环来解决该任务。

以下是我的建议。以下方法通过从第二个列表中的第一个列表中删除日期间隔来“减去”两个计划列表。它使用一个双循环,首先迭代第二个列表中的计划,即应该减去的计划。对于每个这样的明细表,它会从第一个列表的每个明细表中减去它,从而生成一个新的明细表列表

public static List<Schedule> scheduleListDiff(
        List<Schedule> schedules, List<Schedule> schedulesToExclude) {
    // eliminate dates from schedulesToExclude one schdule at a time
    for (Schedule toExclude : schedulesToExclude) {
        List<Schedule> result = new ArrayList<>();
        for (Schedule originalSchedule : schedules) {
            result.addAll(originalSchedule.notPresentIn(toExclude));
        }
        schedules = result;
    }
    return schedules;
}
我为Schedule类安装了一个辅助方法,该方法不存在于执行实际比较时:

/** @return a list of 0, 1 or 2 schedules with the dates from this schedule that are not in other */
List<Schedule> notPresentIn(Schedule other) {
    if (other.end.isBefore(start) || end.isBefore(other.start)) { // no overlap
        return Collections.singletonList(this);
    }
    // now we know there is an overlap
    List<Schedule> result = new ArrayList<>(2);
    if (start.isBefore(other.start)) { // need to include day/s from the first part of this
        // this bit must end the day before other.start
        result.add(new Schedule(objectiveId, start, other.start.minusDays(1)));
    }
    if (end.isAfter(other.end)) { // need day/s from the last part
        result.add(new Schedule(objectiveId, other.end.plusDays(1), end));
    }
    return result;
}
编辑:我在重复问题的样本数据上运行了代码。该示例在一个以前的明细表中有两个新的示例明细表。所以之前的时间表应该分成三部分。结果是:

107 -- 10 May 2016 -- 11 May 2016
107 -- 14 May 2016 -- 15 May 2016
107 -- 19 May 2016 -- 20 May 2016

这是因为scheduleListDiff中的每个迭代都使用上一个迭代的结果,所以首先将计划拆分为两个,下一个迭代将进一步拆分这两个迭代中的一个。

是Java问题还是SQL问题?如果这是一个Java问题,为什么要谈论表?我们可以假设其中一个表中没有重叠的时间表吗?换言之,新的_计划能否同时包含1月10日至21日和1月16日至23日?您应该向我们展示您的尝试。它将让我们更好地了解您已经知道的内容以及您的问题所在,从而为您提供更好的指导起点。这是一个Java问题。这些表表示Bean结构。@匿名请使用其他信息编辑您的问题,而不是以注释的形式发布。我需要开始日期和结束日期。假设上述表格存储在列表previous和List new中。调度bean具有以下属性:objective_id、startDate、endDate。我需要根据输出表中提到的上述逻辑打印一个列表输出,其中包含刷新的开始日期和结束日期。您将在我更新的答案中找到解决该问题的方法。希望这对你有帮助。不管怎样,StackOverflow背后的想法不是开发现成的解决方案,而是提供如何解决问题的想法。希望我能帮你做家庭作业或其他什么。谢谢你的解决方案。这对一个人和许多人都有效,但对许多人来说,我表示怀疑。这个解决方案将作为我实际解决方案的垫脚石。请解释你在这里所说的多对多是什么意思。如果以前的时间表包括1月10日至18日和1月12日至20日,而新的时间表包括1月13日至14日和1月16日至17日,这算不算多对多?我设计了我的算法来解决这个问题。它应该把原来的计划分成三部分。谢谢你的努力。如果我发现任何遗漏的情况,我会回复你。你的解决方案解决了我90%的问题。太好了。我希望你能计算出剩下的10%。如果没有,堆栈溢出还有更多问题的空间…
/** @return a list of 0, 1 or 2 schedules with the dates from this schedule that are not in other */
List<Schedule> notPresentIn(Schedule other) {
    if (other.end.isBefore(start) || end.isBefore(other.start)) { // no overlap
        return Collections.singletonList(this);
    }
    // now we know there is an overlap
    List<Schedule> result = new ArrayList<>(2);
    if (start.isBefore(other.start)) { // need to include day/s from the first part of this
        // this bit must end the day before other.start
        result.add(new Schedule(objectiveId, start, other.start.minusDays(1)));
    }
    if (end.isAfter(other.end)) { // need day/s from the last part
        result.add(new Schedule(objectiveId, other.end.plusDays(1), end));
    }
    return result;
}
int objectiveId;
// dates are inclusive; end is on or after start
LocalDate start;
LocalDate end;
107 -- 10 May 2016 -- 11 May 2016
107 -- 14 May 2016 -- 15 May 2016
107 -- 19 May 2016 -- 20 May 2016