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 ]
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 ?
Joined: Jun 19, 2001
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 :-)