• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Code for date comparison

 
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 221
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic