Search code examples
javadatecompareperiod

Compare a date in a given period


I want to compare a date in a given period. I use the methods before and after. here is my method.

public boolean compareDatePeriod() throws ParseException
{
    [.....]
    if (period.getDateStart().after(dateLine)){
        if (period.getDateEnd().before(dateLine)){
            result = true;
          }
      }
    ;
    return result;
}

if my dateLine = "01/01/2012" and my period.getDateStart () = "01/01/2012". I return false. I do not understand why?


Solution

  •     SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
        Date startDate = dateFormat.parse("01/01/2012");
        Date endDate = dateFormat.parse("31/12/2012");
        Date dateLine = dateFormat.parse("01/01/2012");
        boolean result = false;     
        if ((startDate.equals(dateLine) || !endDate.equals(dateLine))
                || (startDate.after(dateLine) && endDate.before(dateLine)))  { // equal to start or end date or with in period
            result = true;
        }   
        System.out.println(result);