How to get date set in method of calender type in

2019-08-12 15:43发布

问题:

i am using a executing a query

select lastlevel as VERSION,name as DESCRIPTION,
TO_DATE(lastdate, 'DDMONYYYY_HH24:MI:SS') as RELEASEDATE
from mytable where rownum<=10

here the column lastdate is of varchar type

which give result like this

Version     Release Date        
1.0         2002-11-22

2.0     2003-02-08

i am setting the result of the above query like this here setLastDate is of calender type,so i am setting the result of release date in the method like

if (rs.getString("VERSION") != null ) {             bean.setLastLevel(rs.getString("VERSION").trim());
}

if (rs.getDate("RELEASEDATE") != null ) 
{
Calendar calendar = Calendar.getInstance();
calendar.setTime(rs.getDate("RELEASEDATE"));
bean.setLastDate(calendar);
}

the value is being set in the bean like this 2003-02-08. at the end i am adding this result bean to an array list "list" like list.add(bean);

now i want all this value in jsp. I am using spring mvc so i return view with the arraylist "list". when i am iterating the value in jsp like this

<c:forEach var="list" items="${list}">
<td valign="top">${list.lastLevel}</td>
<td valign="top">${list.lastDate}</td>
</c:forEach>

in jsp instead of getting date i am getting like this

Version     Release Date        
1.0          java.util.GregorianCalendar[time=1037775600000,areFieldsSet=true,

areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="US/Arizona",offset=-25200000,dstSavings=0,useDaylight=false,transitions=12,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2002,MONTH=10,WEEK_OF_YEAR=47,WEEK_OF_MONTH=4,DAY_OF_MONTH=20,DAY_OF_YEAR=324,DAY_OF_WEEK=4,DAY_OF_WEEK_IN_MONTH=3,AM_PM=0,HOUR=0,HOUR_OF_DAY=0,MINUTE=0,SECOND=0,MILLISECOND=0,ZONE_OFFSET=-25200000,DST_OFFSET=0]

2.0          java.util.GregorianCalendar[time=1044687600000,areFieldsSet=true,
areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="US/Arizona",offset=-25200000,dstSavings=0,useDaylight=false,transitions=12,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2003,MONTH=1,WEEK_OF_YEAR=6,WEEK_OF_MONTH=2,DAY_OF_MONTH=8,DAY_OF_YEAR=39,DAY_OF_WEEK=7,DAY_OF_WEEK_IN_MONTH=2,AM_PM=0,HOUR=0,HOUR_OF_DAY=0,MINUTE=0,SECOND=0,MILLISECOND=0,ZONE_OFFSET=-25200000,DST_OFFSET=0] 

please help me in solving this.I need to get the date as in result.How to resolve this. Thanks in advance.

回答1:

Use

<td valign="top">${list.lastDate.time}</td>

It will call getTime() method on calendar instance which returns the Date instance

It is preferable not to use same var name as the attribute, call it row (or someother sensible variable name) instead and then ${row.lastDate.time}

<c:forEach var="row" items="${list}">
</c:forEach>

if you want the custom format output then

add

<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

and use it like

<fmt:formatDate pattern="yyyy-MM-dd"  value="${list.lastDate.time}" />