• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

How to compare two dates

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
When it is used for evil, then watch out! When it is used for good, then things are much nicer. Like this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic