Filtering Problems with Java Date

0

I'm trying to filter results with Java in date format, I'm not allowed to use Select to find the results, that's why it's getting complicated ...

The problem is that I always get into the if and it keeps the objects in the array, although "my logic" should not allow it to be saved in the array.

I have a series of records in the database - >

Registro 1-> Fecha Inicio "2018-09-01" , Fecha Fin -> "2018-09-30"
Registro 2-> Fecha inicio "2018-10-01" , Fecha Fin -> "2018-10-05"
Registro 3-> Fecha inicio "2017-12-31" , Fecha Fin -> "2018-11-30"
Registro 4-> Fecha inicio "2018-12-01" , Fecha Fin -> "2019-10-01"
Registro 5-> Fecha inicio "2018-12-30" , Fecha Fin -> "2018-12-31"
Registro 6-> Fecha inicio "2018-11-30" , Fecha Fin -> "2018-12-01"

The user enters "2018-12-01" up to "9999-31-12"

  • The format of the dates are String type "Year-month-day"
  • My intention is to show all the records that contain that date, that is, I should show the Record 4,5,6.

The 4th register should be inserted into the array because the user put: "2018-12-01" and the record has the same date "2018-12-01"

The 5th register should be inserted into the array because the user put: "2018-12-01" and ending date year-> 9999 and the record is included in its start date between those values and ADDITIONALLY, the end date, is also included between those values.

The 6th record because its end date corresponds to the period of time between the start date and the end date of the user, that is, the user wants between "2018-12-01" to "9999-31-12" and the end date of the record is "2018-12-01"

The record 1,2,3 does not arrive by the start date to the user's minimum, nor by end date, to the user's minimum.

The code is:

@Override
public List<Article> filterResult(String paramSelect, String dateStart, String dateEnd) {

    List<Article> list = null;

    try {
        // Sino me ponen fecha de fin, la establezco al máximo.
        if(dateStart != null && dateEnd == null) {
            dateEnd ="9999-31-12";
            list = (List<Article>) this.pgRepository.findAll();
            list =  this.getStart(list, dateStart, dateEnd);
        }


    } catch (Exception e) {
        e.printStackTrace();
        LOGGER.error(e.getMessage());
    }

    return list;
}


private List<Article> getStart(List<Article> list, String dateStart, String dateEnd) throws ParseException {

        Date paramStart, paramEnd, userDate = new SimpleDateFormat("yyyy-MM-dd").parse(dateStart) , userEnd  = new SimpleDateFormat("yyyy-MM-dd").parse(dateEnd);
        List<Article> filter = new ArrayList<Article>();
        Article param;

        for (int i = 0; i < list.size(); i++) {
            param = list.get(i);
            paramStart = new SimpleDateFormat("yyyy-MM-dd").parse(param.getStartdatevalidity());
            paramEnd = new SimpleDateFormat("yyyy-MM-dd").parse(param.getEnddatevalidity());

            if(paramStart.after(userDate) || paramEnd.before(userDate) && paramStart.after(userEnd) ||  paramEnd.before(userEnd)) {
                filter.add(param);
            }
        }

        return filter;
    }

I have 2 problems - >

The first is that it is always stored in the array. The second is that I do not know if I'm doing the logic right ...

Thank you!

    
asked by EduBw 28.09.2018 в 11:12
source

1 answer

0

The condition is a bit more complex because of the cases in the limit but it would look something like this:

if (paramStart.equals(userDate) || paramEnd.equals(userDate) || paramStart.equals(userEnd) || paramEnd.equals(userEnd)) {
    filter.add(param);
} else if (paramStart.after(userDate) && !paramStart.after(userEnd) && paramEnd.before(userEnd) && !paramEnd.before(userDate)) {
    filter.add(param);
}

I've tried it with this function:

public static void main(String[] args) {
    List<Range> dates = new ArrayList<>();
    dates.add(new Range("2018-09-01", "2018-09-30"));
    dates.add(new Range("2018-10-01", "2018-10-05"));
    dates.add(new Range("2017-12-31", "2018-11-30"));
    dates.add(new Range("2018-12-01", "2019-10-01"));
    dates.add(new Range("2018-12-30", "2018-12-31"));
    dates.add(new Range("2018-11-30", "2018-12-01"));

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    Date paramStart;
    Date paramEnd;
    try {
        Date userDate = sdf.parse("2018-12-01");
        Date userEnd = sdf.parse("9999-31-12");
        int i = 0;
        for (Range date : dates) {
            i++;
            paramStart = sdf.parse(date.start);
            paramEnd = sdf.parse(date.end);
            if (paramStart.equals(userDate) || paramEnd.equals(userDate) || paramStart.equals(userEnd) || paramEnd.equals(userEnd)) {
                System.out.println(i);
            } else if (paramStart.after(userDate) && !paramStart.after(userEnd) && paramEnd.before(userEnd) && !paramEnd.before(userDate)) {
                System.out.println(i);
            }
        }
    } catch (ParseException e) {
        e.printStackTrace();
        return;
    }
}
    
answered by 28.09.2018 в 13:14