Java : given a list of object that has range of dates find two objects whose end month is closest to current date month

时间:2016-11-12 05:30:35

标签: java

List of dates are as below (The list can be in any order): 3-Jan to 31-Mar, 2-Apr to 30-Jun, 1-Jul to 30-Sep, 4-Oct to 31-Dec

Current Date is: 19-Feb

Can someone please help me with the logic?

My approach is:

if(the given date should be greater than start date and less than end date){//this gives current quarter}else if(difference of the month of current date from the end date of each object should be less than or equal to 5)

i am hard coding the condition less than 5, which may break if in future the range of date will be of 4 months

Second approach is:

we can sort the list in ascending order and can get the current quarter index by comparing with current date and the next quarter will be of next index. But the complexity will be more.

I tried below code, but it gives only current quarter date. I am not able to get next quarter considering there would be only 3 objects and current date month is feb.

public static List getCurrentQtrOffr(List detail,Date currentDate) throws ParseException{

    int currentQuarter = 9999, diff1;
    int nextquarter = 9999, diff2;
    Detail detail1;
    Detail detail2;
    Detail detail3 = null;
    Detail detail4 = null;
    Iterator<Detail> iterator = detail.iterator();

    List<Detail> list = new ArrayList<Detail>();

    while(iterator.hasNext()){
        detail1 = iterator.next();      
        diff1 = getDiff(currentDate,detail1.startTime());

        if(diff1>0){

        if(iterator.hasNext()){
        detail2 = iterator.next();
        }else{
        detail2 = null;
        }

        if(detail2 != null){
        diff2 = getDiff(currentDate,detail2.startTime());

        if(diff1 < diff2 ){
            if(currentQuarter > diff1){
            nextquarter = currentQuarter;
            currentQuarter = diff1;
            //how to assign detail3 before updating it with next minimum value, as if there will be only 3 object and flow comes in this if block then detail4 will be null
            detail4=detail3;
            detail3=detail1;
            }else if(nextquarter > diff1){
            nextquarter = diff1;
            detail4=detail1;
            }
        }else{

            if(currentQuarter > diff2){
                nextquarter = currentQuarter;
                currentQuarter = diff2;
                detail4=detail3;
                detail3=detail1;
            }else if(nextquarter > diff2){
                nextquarter = diff2;
                detail4=detail1;
            }
        }           
    }else{
        if(currentQuarter > diff1){
            nextquarter = currentQuarter;
            currentQuarter = diff1;
            detail4=detail3;
            detail3=detail1;
        }else if(nextquarter > diff1){
            nextquarter = diff1;
            detail4=detail1;
        }
    }
    }else{
        System.out.println("skipped "+diff1);
    }
    }
    list.add(detail3);
    list.add(detail4);
    return list;
}

1 个答案:

答案 0 :(得分:1)

如果期间是互斥的(不重叠),您只需检查第一次出现的地点:

  • 目标等于或晚于开头和...
  • 目标在停止之前。

此逻辑遵循日期工作中常用的半开放方法,其中开头是包含,而结尾是独占

说“目标等于或晚于开始”的较短方式是“不在开始之前”。感叹号!在Java语法中表示not

Boolean periodContainsTarget = ( ! target.isBefore( start ) ) && target.isBefore( stop ) ;

如果您的日期为一年,则上述逻辑将与LocalDate一起使用。如果您的字面意思是一个月和一年没有一年,请使用MonthDay课程。逻辑适用于两者。

使用Period类来表示一对LocalDate个对象之间的时间跨度。请参阅Tutorial

您可能还会发现补充java.time的Interval项目中的ThreeTen-Extra 类很有用。