• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to compare two dates  RSS feed

 
Dave Bosky
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can I compare two dates to see if one is greater or equal to the other?
I tried the following but it fails. I know I must be close.....
------------------------------------------------------------
String dateAgreed = "01/01/2004 10:50:57 AM";
String paymentDate = "01/31/2004 10:50:58 AM";
DateFormat dateFormatter = new SimpleDateFormat("MM/dd/yyyy");
Calendar aCalendar = Calendar.getInstance();
Calendar bCalendar = Calendar.getInstance();
Date dateAgreed_fmt = dateFormatter.parse(dateAgreed);
Date paymentDate_fmt = dateFormatter.parse(paymentDate);
aCalendar.setTime(dateAgreed_fmt);
aCalendar.add(Calendar.DATE, 0);
bCalendar.setTime(paymentDate_fmt);

if (bCalendar.equals(aCalendar)) {
out.println("OK"+"<br>");
out.println("Date Agreed: "+dateAgreed+"<br>");
out.println("Payment Date: " +paymentDate+"<br>");
} else {
out.println("INVALID PAYMENT"+"<br>");
out.println("Date Agreed: "+dateAgreed+"<br>");
out.println("Payment Date: " +paymentDate+"<br>");
}
------------------------------------------------------
Thanks,
Dave
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66304
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nothing to do with JSP, so this is off to the Java in General(intermediate) forum
[ February 25, 2004: Message edited by: Bear Bibeault ]
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Comparable c1 = date1;
Comparable c2 = date2;
int result = c1.compareTo(c2);
// result holds a value that determines which is greater than the other
// (or if they are equal).
// @see java.util.Comparable#compareTo(java.lang.Object)
 
Bikramjit Singh Bajwa
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey guys,
Date class have follwoing methods to compare two dats.
For example: If there aer two dates
Date d1=new Date();
Date d2=new Date();
after(d1,d2) : This method would return true or false whether date is after other date.
before(d1,d2) : This method would return true or false whether date is before to other date.
hopefully this would hel you alot..
Cheers
 
Dave Bosky
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My code keeps returning the following error.
java.text.ParseException: Unparseable date: "Jan 9 2003 9:34AM"
I have a MSSQL datetime(8) field that contains "1/9/2003 9:34:37 AM"
What do I need to change in the SimpleDateFormat?
Thanks.
~Dave
 
Don Kiddick
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
DateFormat dateFormatter = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss a");
D.
 
Bikramjit Singh Bajwa
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think, you are retrieving date from the database as a String instead of as a date. First of all retrieve date as a date and then parse it in to java.util.Date class becaosue database return date object of java.sql.Date class. So you will have to parse it first into util package's date object. then you can use SimpleDateFormat Class. It takes paraeter as date object in the constructor.
Chakk de
Bikram
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!