Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Code for date comparison

 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

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
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Nischal
 
Horatio Westock
Ranch Hand
Posts: 221
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the replies...

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

Would soon log with an update
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic