We help IT Professionals succeed at work.

jsp date format

xandrr asked
We just started programming jsp and I have a basic question. I'm retrieving records from a table and one of the columns is a db2 date field. When I print out the data value (rs.getString("rdate")) it reads as follows:
2001-07-16 13:00:04.000000

I needed to format the date on the jsp page to read as something without the time. I know there is date formatting classes but for some reason it won't let me feed it a string...what code should i use to format this string? (how do i convert it into a date, etc?)

thanks ahead of time.
Watch Question

take a look at java.sql.ResultSet  getDate(java.lang.String columnName) method

It returns a java.sql.Date.  (which is a child of the java.util.Date)

so you can do cool stuff like

Date myDate;
Calendar cal = Calendar.getInstance();  // current date

myDate = rs.getDate("rdate");
cal.setTime(myDate);  // sets the calendar to the retrieved time

// now you can use all of the cool Calendar methods.


use the java.text.SimpleDateFormat class to format the dates (using Sumir's code above to get the date) i.e.

SimpleDateFormat sdf = new SimpleDateFormat("dd MMM yyyy");
String dateString = sdf.format(rs.getDate("rdate"));

you can also use it to change a String into a date i.e:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSSSSS");
try {
  Date myDate = sdf.parse(rs.getString("rdate"));
} catch (ParseException e) {}

Why don't you just read the date part of the timestamp in your SQL?

Select date(rdate) from yourtable ...

by using the TO_CHAR() function you can change the sql call to format the date for you:

for example:  SELECT TO_CHAR(SYSDATE, 'Month DD, YYYY') FROM....

will return:  September 25, 2001

mkowales, there isn't a function called TO_CHAR() in DB2. Please read the guideline below on proposing answers.