0

I need to compare 2 dates to a third date and ignore the time portion of all of them.

The code below generates a parse exception because the toString() method returns something like "Wed Feb 26 00:00:00 EST 2014".

Any suggestions on how I might fix this?

private boolean needToSendEmail(EmSelfCertEntity escd) throws ParseException {
        boolean sendEmail = false;

        DateFormat df = new SimpleDateFormat("MM/dd/yyyy");

        Date justTheDate = df.parse(escd.getCurrentFCESDate().toString());
        Calendar firstSent = Calendar.getInstance();
        firstSent.setTime(justTheDate);

        justTheDate = df.parse(new Date().toString());
        Calendar firstFollowUp = Calendar.getInstance();
        firstFollowUp.setTime(justTheDate);
        firstFollowUp.add(Calendar.DATE, -daysToFirstFollowUpEmail);

        Calendar secondFollowUp = Calendar.getInstance();
        secondFollowUp.setTime(justTheDate);
        secondFollowUp.add(Calendar.DATE, -daysToSecondFollowUpEmail);

        if ((firstSent.before(firstFollowUp) && escd.countEmailsSent <= 1)
                || (firstSent.before(secondFollowUp) && escd.countEmailsSent <= 2)) {

            sendEmail = true;
        }
    return sendEmail;
}

Thanks!

2
  • It's not at all clear what your inputs are here... or what time zone you're interested in. Bear in mind that a Date is just an instant in time; it can occur on different days around the world. If you can possibly use Joda Time (or Java 8) that will make your life easier too. Commented Mar 20, 2014 at 17:33
  • What does getCurrentFCESDate return? Wich class? Commented Mar 20, 2014 at 17:46

4 Answers 4

1

Why are you parsing the String when you already have the Date?

If you want to format your existing Date into the format you specified, use the format() method instead:

String justTheDate = df.format(new Date());

Then you can compare the Strings using the equals() method to check for matches.

Edit- By the way, if Java 8 is an option (it came out on Tuesday!), its new DateTime features will do exactly what you're looking for: http://docs.oracle.com/javase/tutorial/datetime/

Sign up to request clarification or add additional context in comments.

2 Comments

with equals you cannot compar for before or after. usually dates need a before or after or < or > compare
I didn't see the before and after, but it would be trivial to then convert back from a String to a Date without any of the time information associated with it.
0

The cause for your exception is that toString in escd.getCurrentFCESDate().toString() delivers another format than "MM/dd/yyyy". So make sure that either your format String in line SimpleDateFormat("MM/dd/yyyy") is correct. Or check if you can get the year, month, and day directly from getCurrentFCESDate().

Just Use the calendar to create a date, where you take the year, months, day from the existing date but set the hours, minutes, seconds and millis to zero.
The result will be a Date object:

Something like

firstSent.set(Calendar.HOUR, 0);
firstSent.set(Calendar.MINUTE, 0);
firstSent.set(Calendar.SECOND, 0);
firstSent.set(Calendar.MILLISECOND, 0);

Then use before() and after()

Comments

0

The easiest approach would be to convert the dates to numbers in this format: yyyyMMdd.

And after that you can just compare the numbers.

But yes, please work with timezone adjustments before converting to numbers.

Comments

0

you can calculate the time in millis and substract the time with a simple / division. This way you can compare 2 longs and check if one is bigger than another.

Take this example where we get to different dates for today (500 milliseconds from one to another) but... if you divide by 86400000 then... you get the same number.

Try this:

    public static void main(String[] args) throws Exception {
    Date d1= new Date();

    Thread.sleep(500);
    Date d2= new Date();

    final int MILLISECONDS = 1000;
    final int SECONDS = 60;
    final int MINUTES = 60;
    final int HOURS = 24;

    final long MILLI_PER_DAY= MILLISECONDS*SECONDS*MINUTES*HOURS;
    System.out.println(MILLI_PER_DAY);

    System.out.println(d1.getTime());
    System.out.println(d2.getTime());
    System.out.println(d1.getTime()/MILLI_PER_DAY);
    System.out.println(d2.getTime()/MILLI_PER_DAY);
}

You will see that the last 2 entries are the same:

1395338535623 --> time 1 in millis 1395338536123 --> time 2 in millis 16149 --> time 1 / 86400000 16149 --> time 2 / 86400000 --> THE SAME

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.