Re: Date question




hoho123 wrote:
String dateStr = "01-May-2006"

I have a string of date like above, is there java class I can use to
make it a sql.date object?

Thank you!

There are a few classes that you could use in combination for this job,
specifically SimpleDateFormat, java.util.Date and java.sql.Date.

I had an existing method that did exactly that for a different date
format, namely ISO format. (Today would be "2006-06-29" in ISO format.)
I just modified my method to expect your format instead, wrote some
test cases for JUnit and gave it a try; it worked perfectly. Here's the
method, as revised to handle your date format:

/**
* Converts a date expressed as a String into a java.sql.Date.
*
* <p><i>Example:</i><br>
* <xmp>
* java.sql.Date mySqlDate =
DateTimeUtils.toSqlDate2("01-May-2006"); //should return an SQL date
equivalent to the input date.
* </xmp></p>
*
* <p><b>NOTE: </b>Although the input parameter expresses a date
with only a year, month and day,
* the date returned by this method includes a year, month, day,
hour, minute, second, and milliseconds.
* The date returned by this method will have the same year, month,
and day as the input parameters
* while the hours, minutes, seconds, and milliseconds will all be
zero, which effectively means
* midnight on the date (year, month, and day) in question.</p>
*
* @param date a date given in DD-Mon-YYYY format, e.g.
01-May-2006.
* @return java.sql.Date expressed with a default time of midnight.
*/
static public java.sql.Date toSqlDate(String date) {

java.text.SimpleDateFormat sdf = new
java.text.SimpleDateFormat("dd-MMM-yyyy");
sdf.setLenient(false);

java.util.Date utilDate = null;
try {
utilDate = sdf.parse(date);
} catch (ParseException p_excp) {
throw new IllegalArgumentException("Encountered
ParseException at offset " + p_excp.getErrorOffset() + " while
attempting to convert the String " + date + " to a java.sql.Date.
Details: " + p_excp.getMessage());
}

java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());

return (sqlDate);
}

--
Rhino

.



Relevant Pages

  • Re: converting between time and string
    ... I have a file with lots of times in the format "01:22:33,456", where the numbers after the comma represent the milliseconds. ... I first thought to convert the string to a sort of date format, and then to do a minus operation, and then to convert the result back to a string of the original form. ...
    (perl.beginners)
  • Re: Int->String formatting method
    ... I have an amount of milliseconds, in the range and I want to format it to a String of the format "HH:nn:ss,mmm" where HH ranges between 00-99, nn between 00-59, ss between 00-59 and mmm between 000-999. ...
    (comp.lang.java.programmer)
  • converting between time and string
    ... I have a file with lots of times in the format "01:22:33,456", where the numbers after the comma represent the milliseconds. ... I first thought to convert the string to a sort of date format, and then to do a minus operation, and then to convert the result back to a string of the original form. ...
    (perl.beginners)
  • Re: Date format detection
    ... Specifies the locale for which the date string is to be formatted. ... date format for this locale. ... the system default-date format for the specified locale. ... be enclosed within single quotation marks in the date format picture. ...
    (borland.public.delphi.thirdpartytools.general)
  • Re: Date format detection
    ... > Specifies the locale for which the date string is to be formatted. ... > date format for this locale. ... > the system default-date format for the specified locale. ... > be enclosed within single quotation marks in the date format picture. ...
    (borland.public.delphi.thirdpartytools.general)