This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Code for date comparison Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Code for date comparison" Watch "Code for date comparison" New topic

Code for date comparison

A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979

I have a field in the database(type varchar)...I am storing...

the date time in this field.

I wish to compare two dates in this column in my java code..

how can i do it...It wud be helpful if i can get some code.

The datetime format is "yyyy-MM-dd HH:mm:ss".

Thanks in advance,
A Kumar.
Nischal Topno
Ranch Hand

Joined: Nov 24, 2001
Posts: 45
create a SimpleDateFormat object with the format of your date as the parameter. Use the SimpleDateFormat's parse() method to get a date object, passing your date from the database as the argument.

With this date object you can create a Calendar object.

Once you have the Calendar objects, these objects can be compared.

Horatio Westock
Ranch Hand

Joined: Feb 23, 2005
Posts: 221

The short answer is, search the forums; this gets asked quite regularly.

The medium answer is, why are you storing the date in a varchar?

The longer answer is, read the API docs for SimpleDateFormat, in particular the parse() method. You can create an instance of this class initialized with your date pattern, then use it to parse the dates, creating Date objects. You can then compare these by getting their time in milliseconds, then using simple maths to convert to hours/days/whatever.

Hope this helps.
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
Thanks for the replies...

I would try to work as said by u..both

Would soon log with an update
I agree. Here's the link:
subject: Code for date comparison
Similar Threads
Convert a String with a date valus for Oracle
have anyone got the solution?
Date question
jdbc date object
Invalid field value for field "Date"