I'm having a problem completing the question below. I am a Java noob so be gentle ;0. The problem is in relation to the equals() method. I have to use this method to check dates to see whether they match. Please see code below.

public boolean equals(){

    if(date1==date2){
        return true;
    }
    else
        return false;

}
    
}

Now I realise that my problem is in the code above. I was reading about objects and how the equals method compares information with an object and not the actual objects themselves. I've been messing around with this code below but to be honest I'm not 100% sure what to do here.

public boolean equals(object obj){

    if(obj instanceof Date){ //date is the name of the class
        return true;
    }
    else
        return false;

}

Below is the rest of my code.

class DateEqualityCheck {
    public static void main(String[] args){

        Date check1 = new Date(2,2,2009);
        Date check2 = new Date(3,2,2007);


        System.out.println(check1);

        System.out.println(check2);

        if (check1.equals(check2)){

            System.out.println("The dates are equal.");
        }
            else
            {
                System.out.println("The dates are not equal");
        }
    }
}

I'm looking to learn as much as I can about Java and really try to rely on forums like this as a last resort as I want to force myself to learn. However, sometimes you need a bit of guidance. I'd really appreciate it if someone could help me here.

Thank you in advance.

You have the right idea, but the main problem is in your equals() method that you created. Your method just checks to see if the inputted object is a date object. So for instance, calling if(check1.equals(check2)) will basically check if check2 is an object that is also a date. Since check2 is a date object, the method will return true. It will not compare check2 and check1. However, the Date class actually already has a .equals() method that works correctly, so all you really need is the DateEqualityCheck code you posted before. None of the other code is needed and you should be fine. This way, when you call the equals() method it will go into the Date Class' equals() method.

@ OP:

You're using the Date class constructor incorrectly. It says to put the arguments in as year, month, day. Furthermore if you read the Javadoc for the Date class's constructor you are using you will see that even if you reorder your parameters, it still won't work as expected. Month according to the description must be between 0-11. Oh, and the method also says that it is deprecated - i.e. its use is discouraged and it is only really there so that older programs do not break.


http://www.java-forums.org/java-util/7176-how-compare-two-dates.html

Kvass, I got rid of the code you said was not needed. However the program is still not working properly. It gives the same value if the dates are the same or not. Any other ideas at all?

JC, I read the link you put up. Is the jist of it that you have to declare the year variable first followed by month and day and when inputting data, I should follow this way?

The jist of it is to say new Date(year, month, day) and to make sure that year, month, and day all conform to the guidelines in the documentation.

Construct the objects correctly like BJSJC said and then see if it works

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.