user3556159
user3556159

Reputation: 87

Java Date comparison not working properly

I am getting actual date using this:

SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy");
Calendar cal = Calendar.getInstance();
String today = dateFormat.format(cal.getTime());
System.out.println(today);

It prints today`s date: 21.04.2014

Then, I want to compare this date whether it is between two dates:

SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
Date actualdate = sdf.parse(today);
Date date1 = sdf.parse("20.04.2014");  
Date date2 = sdf.parse("23.04.2014");
Calendar actual = Calendar.getInstance();
Calendar cal1 = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();
actual.setTime(actualdate);
cal1.setTime(date1);
cal2.setTime(date2);

if(actual.after(date1) && actual.before(date2)) System.out.println("Yes");
else System.out.println("No");

It always prints No. What is the problem ? When I set date1 to 21.04.2014 and use actual.equals(date1) it works. But when I am using before or after it doesnt. Thanks

Upvotes: 1

Views: 5559

Answers (5)

Fahad Alkamli
Fahad Alkamli

Reputation: 138

I am sorry to tell you that the function that compares between two dates is useless and not working. If you would like to have a go this is the function i tested

private static boolean validateEndDate(String startDate, String endDate)
{
    try {
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd/mm/yyyy");
        Date date1 = dateFormat.parse(startDate);
        Date date2 = dateFormat.parse(endDate);
        if(date1.before(date2))
        {
            return true;
        }
    }catch(Exception e)
    {
        System.out.println(e.getMessage());
    }

    return false;
}

Now to see what i am talking about give it these two dates:

System.out.println(validateEndDate("31/07/2016","1/08/2016"));

Now to fix this problem i had to implement my own function:

private boolean compareDates(String startDate,String endDate)
    {
    int day=Integer.parseInt(startDate.split("/")[0]);
    int month=Integer.parseInt(startDate.split("/")[1]);
    int year=Integer.parseInt(startDate.split("/")[2]);

    int day2=Integer.parseInt(endDate.split("/")[0]);
    int month2=Integer.parseInt(endDate.split("/")[1]);
    int year2=Integer.parseInt(endDate.split("/")[2]);
    if(year<year2 || year==year2)
    {
        if(year<year2)
     {
        return true;
      }
        //Check the month
        if(month<month2 || month==month2)
        {
            if(month<month2)
            {
                return true;
            }
            //Check the day
            if(day<day2)
            {
                return true;
            }
        }
    }

    return false;
}

Upvotes: 1

Leo
Leo

Reputation: 6570

you are comparing a calendar with dates

you must compare calendars with calendars or dates with dates

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


public class Cal {

    public static void main(String[] args) throws ParseException {
        SimpleDateFormat dateFormat = new SimpleDateFormat("ddMMyyyy");
        Calendar cal = Calendar.getInstance();
        String today = dateFormat.format(cal.getTime());
        System.out.println(today);

        SimpleDateFormat sdf = new SimpleDateFormat("ddMMyyyy");
        Date actualdate = sdf.parse(today);
        Date date1 = sdf.parse("20042014");  
        Date date2 = sdf.parse("23042014");
        Calendar actual = Calendar.getInstance();
        Calendar cal1 = Calendar.getInstance();
        Calendar cal2 = Calendar.getInstance();
        actual.setTime(actualdate);
        cal1.setTime(date1);
        cal2.setTime(date2);

        System.out.println(actual);
        System.out.println(cal1);
        System.out.println(cal2);

        if(actualdate.after(date1) && actualdate.before(date2)){
            System.out.println("Yes");
        }
        else {
            System.out.println("No");
        }

        if(actual.after(cal1) && actual.before(cal2)){
            System.out.println("Yes");
        }
        else {
            System.out.println("No");
        }

    }

}

Upvotes: 3

Ahsan Mahboob Shah
Ahsan Mahboob Shah

Reputation: 4029

see the working one:

SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
Date actual = sdf.parse("21.04.2014");
Date date1 = sdf.parse("20.04.2014");  
Date date2 = sdf.parse("23.04.2014");
if(actual.after(date1) && actual.before(date2)) {
    System.out.println("Yes");
} else {
    System.out.println("No");
}

Upvotes: 0

Ruchira Gayan Ranaweera
Ruchira Gayan Ranaweera

Reputation: 35557

You are comparing Calender value with Date. Just use Date values. or just use Calender values. Don't mix them both.

actual.after(cal1) && actual.before(cal2)

or

actualdate.after(date1) && actualdate.before(date2)

Upvotes: 0

Thudani Hettimulla
Thudani Hettimulla

Reputation: 774

change it to actual.after(cal1) && actual.before(cal2)

Upvotes: 7

Related Questions