0

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?

Mercer
  • 9,736
  • 30
  • 105
  • 170

2 Answers2

1

If you would kindly check the Java documentation before posting your question, you would know that the method after returns :

true if and only if the instant represented by this Date object is strictly later than the instant represented by when; false otherwise.

In your case, the dates are equal which means they are not strictly later. Thus it will return false

UPDATE:

public boolean compareDatePeriod() throws ParseException
{
    [.....]
    if (!period.getDateStart().equals(dateLine)) {
        if (period.getDateStart().after(dateLine)){
            if (period.getDateEnd().before(dateLine)){
                result = true;
              }
          }
    return result;
}
Adel Boutros
  • 10,205
  • 7
  • 55
  • 89
1
    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);
Vaandu
  • 4,857
  • 12
  • 49
  • 75
  • 1
    He didn't ask for code, he just asked why didn't it behave the way he wanted. But he chose your answer. :) – Adel Boutros Jan 06 '12 at 11:22
  • tried what? you lazily helped him and I bet he still didn't get why it didn't work. All I am saying is that the purpose of answering in StackOVerflow is to help the asker to find out what went wrong not code for him and make him do the same mistake in the future. It's the only thing I am saying – Adel Boutros Jan 06 '12 at 11:26