0

I have an Identity class with necessary accessors and mutators.

In a seperate program I am comparing the Identity's Date to todays date which always returns <= 10. However when I go to print it I get the same result no matter what the date is. Is there a different way to access the correct date?

I need to get the number of days between two dates and I'm having trouble on the formating:

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

        Date a = df.parse("06/18/2015");
        Date b = df.parse("01/04/2015");
        Date c = df.parse("02/04/2015"); 
        Date d = df.parse("03/04/2015"); 
        Date e = df.parse("07/04/2015"); 
if(a.compareTo(today) == 30{
//do stuff
}

I've tried multiple methods to no avail.

3
  • What does javadoc of compareTo say? Commented Dec 4, 2014 at 17:19
  • @SotiriosDelimanolis Returns: the value 0 if the argument Date is equal to this Date; a value less than 0 if this Date is before the Date argument; and a value greater than 0 if this Date is after the Date argument. -- To me this returns the difference between them but i could be wrong Commented Dec 4, 2014 at 17:21
  • 3
    It says 0, greater or lower. That is not the same as the difference between the two dates. Commented Dec 4, 2014 at 17:25

3 Answers 3

1

You have two mistakes in your code :

  1. compareTo() will only return 0 if dates are identical, a positive value if the date is after the argument, or a negative value if it is before. It does not return the difference between the two dates. To get the difference you can write :

    today.getTime() - expiration.getTime()

    to get the number of milliseconds between the two dates, and then compare it to 10 days converted in milliseconds.

  2. You don't initialize your date correctly : Date a = new Date(06/18/2015); won't work. It is supposed to create a new Date with a number of milliseconds as a parameter. So your code will divide 6 by 18 and by 2015 to obtain a number of milliseconds... You can use :

    Date a = new GregorianCalendar(2015, 06, 18).getTime();

  3. You have a simpler way to get today : Date today = new Date();

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

Comments

0

You're performing integer division, not calling the Date constructor you think.

Date a = new Date(06/18/2015);

Use a DateFormat like

public static void main(String[] args) {
    DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
    try {
        Date a = df.parse("06/18/2015");
        System.out.println("1. " + a);
        System.out.println("2. " + df.format(a));
    } catch (ParseException e) {
        e.printStackTrace();
    }
}

Note Date is an instant in time, so it's default textual representation may vary from what you might expect. See the differences in lines 1 and 2 below,

1. Thu Jun 18 00:00:00 EDT 2015
2. 06/18/2015

Finally, you need to calculate the difference between two dates. compareTo will tell you smaller or bigger, not magnitude.

Comments

0

I made a method that takes in two dates, converts them and returns a long that works fine. 86400000 is the number of milliseconds in a day. I am using GregorianCalendar format

public long compareDates(Date exp, Date today){

         return (exp.getTime()-today.getTime())/86400000;
    }

2 Comments

I'm not sure to understand what you're trying to do. Why do you substract one day to each date before converting it? If your goal is to get the number of days between the two dates, you can simply write : return Maths.abs(exp.getTime()-today.getTime())/86400000 (Math.abs is here to always return a positive value as the difference could be negative)
@JulienR that's what I ended up doing without the Math.abs. A seperate function will be implemented if indeed there is a negative value

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.