简体   繁体   中英

Convert Java Gregorian Calendar in Javascript

I have a JSP page with a java variable inside it which name is ${entry.date} , type of this variable is Gregorian Calendar and it places in a loop and generates a table of error log at last. now my question is that is it possible in my current page, by use of javascript , I get the variable and converted to Solar Calendar object and placed it again in a my html?

The output of ${entry.date} is:

java.util.GregorianCalendar[time=1361788629000,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="GMT+03:30",offset=12600000,dstSavings=0,useDaylight=false,transitions=0,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2013,MONTH=1,WEEK_OF_YEAR=9,WEEK_OF_MONTH=5,DAY_OF_MONTH=25,DAY_OF_YEAR=56,DAY_OF_WEEK=2,DAY_OF_WEEK_IN_MONTH=4,AM_PM=1,HOUR=2,HOUR_OF_DAY=14,MINUTE=7,SECOND=9,MILLISECOND=0,ZONE_OFFSET=12600000,DST_OFFSET=0]

And the HTML Block is:

<c:forEach var="entry" items="${errorLog}">
<tr>
    <td align="center" nowrap="nowrap">
        <mytag:format dateTime="${entry.date}"/>
    </td>
</tr>
</c:forEach>

There's no need to do it in Javascript. Just use the standard JSTL formatDate tag along with Calendar 's getTime() accessor:

<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
...
<c:forEach var="entry" items="${errorLog}">
<tr>
    <td align="center" nowrap="nowrap">
        <fmt:formatDate value="${entry.date.time}" pattern="yyyy-MM-dd hh:mm:ss a" />
    </td>
</tr>
</c:forEach>

You can adjust the pattern to suit your needs.

Edit: The most elegant way to achieve what you want would be to implement the conversion/formatting in a custom tag, but another way would be:

<c:set var="calendar" value="${entry.date}" />
<c:set var="convertedCalendar" value='<%=SomeHelperClass.convertCalendar((Calendar)pageContext.findAttribute("calendar")) %>' />

where SomeHelperClass.convertCalendar() is some static utility method to convert your Calendar .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM