On Fri, 6 Apr 2001, Ryan wrote:

> I have a simple question about the Date object (or similar object)
>
> I have a mySQL table with a DATETIME cell. I want to get the date from this cell
>
> (formatted like so: e.g.  2001-03-23 13:04:59)
>
>  and retrieve the date that is exactly 7 days earlier than the retrieved date.
>
> What is the easiest way to do this? I noticed a lot of method deprecations in the 
>specs and I am having trouble using the Date object.

First off, when dealing with dates related to SQL calls, always use the
setDate() and getDate() functions of the PreparedStatement and ResultSet
respectively. These will format the date correctly for the underlying
RDBMS and they take and return a java.sql.Date. Note that a java.sql.Date
is different from a normal java.util.Date. You will need to use these full
class designations in your code to distinguish them.

To get a date that is 7 ddays prior use the Calendar class. Use
getInstance to get a Claendar instance. Set the Calendar's time to the
original date then do a cal.add(cal.DAY_OF_MONTH, -7) (where cal is the
name of your Calendar object). Note that I don't have the docs in front of
me right now, so that might not be exaclty the syntax.


Joe Laffey
LAFFEY Computer Imaging
St. Louis, MO
----------------------
Need to do multi-file string replacement in Un*x, but don't want to mess
with sed? Try rpl. It's a free text replacement utility with source.
http://www.laffeycomputer.com/rpl.html  -- Check it out!
------------------------------------------------------------------------

Reply via email to