验证项目在秋季开始日期和结束日期 [英] Validate Item Fall Within Start Date And End Date

查看:109
本文介绍了验证项目在秋季开始日期和结束日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Java程序,它会检查开始日期和每个项目的结束日期。每个项目必须有自己的具体开始日期和结束日期范围。而这个系统会提示错误信息,如果新的开始日期和结束日期落在上了年纪的范围开始日期和结束日期。例如:

I have a java program which will check for start date and end date of each item. Each Item must have their own specific start date and end date range. And this system will prompt error message if the range of new start date and end date fall within the older start date and end date. For example:

 Company: ABC
 Item_Number     Start Date      End Date
 Item 11A        01/08/2014      01/09/2014  
 Item 11B        02/09/2014      30/09/2014
 Item 11C        18/08/2014      30/08/2014

系统将提示错误信息,当最终用户尝试添加项目11C。任何建议来解决这个问题?非常感谢你。

The system will prompt error message when end user try to add item 11C. Any suggestion to resolve this issues? Thank you very much.

推荐答案

日期范围可能会非常棘手...

Date ranges can be tricky...

首先要确保原有的开始日期不等于比较开始或结束日期和原来的结束日期不等于比较开始或结束日期

First you want to make sure that the original start date is not equal to the compare start or end date and that the original end date is not equal to the compare start or end date

startDate.equals(originalStartDate) ||
startDate.equals(originalEndDate) ||
endDate.equals(originalStartDate) ||
endDate.equals(originalEndDate)

如果这一切都没事(),然后需要检查的比较开始日期在原来的开始或结束日期之间,如果比较结束日期​​落在之间原来的开始或结束日期...

If that's all okay (false), you then need to check if the compare start date falls between the original start or end date and if the compare end date falls between the original start or end date...

(startDate.after(originalStartDate) || startDate.before(originalEndDate) ||
(endDate.after(originalStartDate) || endDate.before(originalEndDate)

这试图捕捉到任何地步两个范围包含或互相重叠...

This tries to capture any point where the two ranges either include or overlap each other...

和,因为我写的实际一些测试code ...

And because I actual wrote some test code...

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class CompareDates {

    public static void main(String[] args) {
        try {
            List<Event> events = new ArrayList<Event>(3);
            events.add(new Event(toDate("01/08/2014"), toDate("01/09/2014")));
            events.add(new Event(toDate("02/09/2014"), toDate("30/09/2014")));

            Date start = toDate("18/08/2014");
            Date end = toDate("30/08/2014");

            for (Event evt : events) {
                System.out.println(evt.conflicts(start, end));
            }

        } catch (ParseException exp) {
            exp.printStackTrace();
        }
    }

    public static Date toDate(String value) throws ParseException {
        return new SimpleDateFormat("dd/MM/yyyy").parse(value);
    }

    public static class Event {
        private Date startDate;
        private Date endDate;

        public Event(Date startDate, Date endDate) {
            this.startDate = startDate;
            this.endDate = endDate;
        }

        public boolean conflicts(Date start, Date end) {

            return start.equals(startDate) ||
                            start.equals(endDate) ||
                            end.equals(startDate) ||
                            end.equals(endDate) ||
                            (start.after(startDate) && start.before(endDate)) || 
                            (end.after(startDate) && end.before(endDate));

        }

    }

}

这篇关于验证项目在秋季开始日期和结束日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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