Doubt in compare to method

Hi,

I have two dates both are same.(ie. 2007-07-26 and 2007-07-26). I used the following code to compare that But it returns a negative value. So help me to resolve this problem

System.out.println(date1.compareTo(date2));

It prints the following result: -1 I couldn't find out actually what is the problem in that statement. So help me to resolve this.

Thanks in Advance,

Maheshwaran Devaraj

[430 byte] By [mheshpmra] at [2007-11-27 11:48:57]
# 1

i assume you are using the java.util.Date class?

keep in mind that a date object doesnt just hold a date, but also a time, so the following two dates

29-07-2007 14:12:01

29-07-2007 09:04:12

might have the same date properties, but the time is different, thus a comparison will return that those dates are not equal

if you want to check if two dates are the same, you might want to use the .equals method instead, but once again this will also compare the time part of a date

Vectorizeda at 2007-7-29 18:22:18 > top of Java-index,Java Essentials,Java Programming...