• 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

dates in sql to dates in java

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
a section of my program that im writing requires some data comparisons betweens the current date and a date stored in the database. im running into 2 problems atm which i cant see to fix.

1. inserting the dates into the database
orginally i was using strings but was told it is better to use the DATE type. ive got a text field for a date in which the format is checked to be consistent to dd/mm/yyyy using a small class i wrote (returns a boolean) before a new record is added to the database. this was working fine with the date being stored as a string but now i am using a DATE and a date like 20/04/2006 wont be stored in the db as 2006-04-20 instead its stored as something slightly random.

2. retrieving dates from the database
i am trying to exract the date from the database using the resultset.getDate("startDate") method into a java.util.date object. from here i want to compare it to another date object (the current date) to test if the retreived date is less than say x weeks away (then do my intended handling of the data).

any help would be appreciated! i havent got much hair left to pull out!
[ April 20, 2006: Message edited by: Deyna Cegielski ]
 
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi


i am not sure i understand your questions (you dont tell us whats not working.... just what you are trying to do)

1. i dont see where there is a difference for the input part of your application if its a String or a Date object that goes to the DB since checking if its valid input should done before you send it to the database ?
old: input (String) -> validate -> to DB
new: input (String) -> validate -> convert to Date -> to DB

btw: do you know the DateFormat classes for parsing dates ? they could help you

2. what you get back from the ResultSet is a java.sql.Date object which is a subclass of java.util.Date. You can use the Calendar/GregorianCalendar classes and the Date.before() and Date.after() to to some calculations/comparison. but why arent you doiing this comparison in the database and just retrieve the rows that match the criteria ?



pascal
 
pascal betz
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
and one important thing i forgot:
use PreparedStatement to create the query and apply the params (it has a setDate(index, Date) method so you do not need to convert the date to a string in a "random" format :-)
 
Always! Wait. Never. Shut up. Look at this tiny ad.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic