Win a copy of Spring in Action (5th edition) this week in the Spring 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

dates in sql to dates in java  RSS feed

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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 :-)
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!