0

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?

3 Answers 3

1

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();
}
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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();
            }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.