mvkraju
asked on
compare to Dates only
Hi,
I have 2 java.util.Date objects
I want to compare those 2 dates
I am using the following statement
int result = stDt.compareTo(endDt);
i am wondering sometimes even though both the dates are same, it's returning -1
I don't understanding why it's returning zero sometimes and -1 sometimes
even though both the dates are same.
Basically i need a function which should comapare 2 dates. it should give me true if both the dates have same yyyy-MM-dd, even though they have different HH:mm:ss.
It should compare only date part, not time part.
i guess you got my requirement.
Is there any such method in Java?
I appreciate your help!
Thanks
I have 2 java.util.Date objects
I want to compare those 2 dates
I am using the following statement
int result = stDt.compareTo(endDt);
i am wondering sometimes even though both the dates are same, it's returning -1
I don't understanding why it's returning zero sometimes and -1 sometimes
even though both the dates are same.
Basically i need a function which should comapare 2 dates. it should give me true if both the dates have same yyyy-MM-dd, even though they have different HH:mm:ss.
It should compare only date part, not time part.
i guess you got my requirement.
Is there any such method in Java?
I appreciate your help!
Thanks
If you don't care about the time part then you could use java.sql.Date
int result = new java.sql.Date(stDt.getTime ()).compar eTo(new java.sql.Date(endDt.getTim e()));
int result = new java.sql.Date(stDt.getTime
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Sorry, direct link to Calendar API:
http://java.sun.com/j2se/1.3/docs/api/java/util/Calendar.html
http://java.sun.com/j2se/1.3/docs/api/java/util/Calendar.html
you cant compare two dates directly, further here is an example which i have done in my old projects..
// systemdate
String systDate = Pyear+"/"+Pmonth+"/"+Pdate ;
//user selected date
String wholepath = year+"/"+month+"/"+day ;
//one fixed date
String fixDate = "2001/11/24" ;
DateFormat df = new SimpleDateFormat("yyyy/MM/ dd");
Date sys = null;
try {
sys = df.parse(systDate);
} catch (ParseException e) {
e.printStackTrace();
}
Date pre = null;
try {
pre = df.parse(wholepath);
} catch (ParseException e1) {
e1.printStackTrace();
}
Date pas = null;
try {
pas = df.parse(fixDate);
} catch (ParseException e1) {
e1.printStackTrace();
}
compare it as,
if(pre.after(sys)) {
//
}
else if(pre.before(pas)) {
//
}
else {
//
}
hope i could help!!
// systemdate
String systDate = Pyear+"/"+Pmonth+"/"+Pdate
//user selected date
String wholepath = year+"/"+month+"/"+day ;
//one fixed date
String fixDate = "2001/11/24" ;
DateFormat df = new SimpleDateFormat("yyyy/MM/
Date sys = null;
try {
sys = df.parse(systDate);
} catch (ParseException e) {
e.printStackTrace();
}
Date pre = null;
try {
pre = df.parse(wholepath);
} catch (ParseException e1) {
e1.printStackTrace();
}
Date pas = null;
try {
pas = df.parse(fixDate);
} catch (ParseException e1) {
e1.printStackTrace();
}
compare it as,
if(pre.after(sys)) {
//
}
else if(pre.before(pas)) {
//
}
else {
//
}
hope i could help!!
are you sure about that?
the "Date" does not only represent a date, but also a time (with hours, minutes, seconds and milliseconds).