wood burning stoves 2.0*
The moose likes Servlets and the fly likes EVIL DATES. Need help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "EVIL DATES. Need help" Watch "EVIL DATES. Need help" New topic
Author

EVIL DATES. Need help

James Hewitt
Greenhorn

Joined: Jul 09, 2001
Posts: 27
I want to calculate if a date retrieved from a database (in the normal Date form) is before or after the current one. Using "boolean equals(Date d)" won't work since the date is retrieved from the database as a string. How do I take this string and convert it to a date, d, so that I can use the above method?
Thanks in advance,
James
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15952
    
  19

The simplest method, assuming that the date is stored as type "date" in your database is to retrieve it in its java-equivalent form. That is:
java.sql.date xdate = (java.sql.Date) rs.getObject("xdate");
Please note that class java.sql.Date is NOT java.util.Date!
If you can only get the date as a string, you have to either parse it out yourself and use the month/day/year form of the date constructor or use the Calendar class feature to do locale-sensitive construction.


Customer surveys are for companies who didn't pay proper attention to begin with.
SAFROLE YUTANI
Ranch Hand

Joined: Jul 06, 2001
Posts: 257
Which database are you using? I can help you if it's Oracle.
SAF
James Hewitt
Greenhorn

Joined: Jul 09, 2001
Posts: 27
Thanks very much. Parsing it worked fine.
It was db2 by the way.
Thanks,
James
Kevin Wright
Ranch Hand

Joined: Jul 10, 2001
Posts: 38
I had a similar problem, and my solution was to create a DateUtil class specific to my DB - MSAccess. It implements Comparable, and returns the short date form. Yours would be similar.
Hope that helps.
Venugopal nandikolla
Greenhorn

Joined: Feb 02, 2001
Posts: 22
Try to use java.text.DateFormat.parse(String text) which returns Date object.
-venu
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: EVIL DATES. Need help
 
Similar Threads
Filter data in Java
String lentgh problem in java-oracle
finding days of two dates in jsp page
converting types
Date formatting