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.