user2872856
user2872856

Reputation: 2051

Compare dates from string without time

I need to compare whether a date is after another date. For example, today is 12 January 2020. First, I tried this:

SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
Date pDate = dateFormat.parse("12/01/2020");
            Date currentDate = new Date();
            if (currentDate.after(pDate)) {
                Toast.makeText(getApplicationContext(), "after", Toast.LENGTH_LONG).show();
            }

Although both date are the same, I got the toast "after". Then I tried this:

Date currentDate = new Date();
        if (currentDate.after(currentDate)) {
            Toast.makeText(getApplicationContext(), "after", Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(getApplicationContext(), "same day", Toast.LENGTH_LONG).show();
        }

This time I got the toast "same day". Lastly I changed to date to 13/01/2020:

Date pDate = dateFormat.parse("13/01/2020");
            Date currentDate = new Date();
            if (currentDate.after(pDate)) {
                Toast.makeText(getApplicationContext(), "after", Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(getApplicationContext(), "before", Toast.LENGTH_LONG).show();
            }

And I got the toast "before". The method seems working, but why the first code returned "after" even both date are the same?

Upvotes: 0

Views: 64

Answers (3)

user2872856
user2872856

Reputation: 2051

Problem solved by converting the today time to 00:00:00

private Date getZeroTimeDate(Date date) {
        Date res;
        Calendar calendar = Calendar.getInstance();

        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);

        res = calendar.getTime();

        return res;
    }

Then

SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
Date pDate = dateFormat.parse("12/01/2020");
            Date currentDate = new Date();
            if (getZeroTimeDate(currentDate).after(pDate)) {
                Toast.makeText(getApplicationContext(), "after", Toast.LENGTH_LONG).show();
            }

Upvotes: 0

Md. Asaduzzaman
Md. Asaduzzaman

Reputation: 15433

Actually your first scenario works as expected. The date that you compare with current date is really before of current date. Let me explain:

pDate contains Sun Jan 12 00:00:00 where time part is 00:00:00

currentDate contains Sun Jan 12 17:05:19 where time part is 17:05:19

So, your currentDate always after of your pDate.

To overcome this you have to compare date part only.

SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");

Date pDate = dateFormat.parse("12/01/2020");
Date currentDate = dateFormat.parse(dateFormat.format(new Date()));

if (currentDate.after(pDate)) {
    Toast.makeText(getApplicationContext(), "after", Toast.LENGTH_LONG).show();
}

Upvotes: 1

Ezaldeen sahb
Ezaldeen sahb

Reputation: 739

you can look at date class source code here, it shows that they use the number of milliseconds for each dates. you are ignoring the hours, minutes and seconds values, and that's why you got the first result

Upvotes: 0

Related Questions