Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

EVIL DATES. Need help

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Saloon Keeper
Posts: 25459
180
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which database are you using? I can help you if it's Oracle.
SAF
 
James Hewitt
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks very much. Parsing it worked fine.
It was db2 by the way.
Thanks,
James
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try to use java.text.DateFormat.parse(String text) which returns Date object.
-venu
 
We don't have time for this. We've gotta save the moon! Or check this out:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic