Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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>
share|improve this question
Where does <mytag:format> comes from? And why do you want to do this in Javascript rather than in Java? – Etienne Miret Mar 11 at 12:10
I just wanna change the view section not the original date in core java and <mytag> is a defined taglib in project, it has getDate() method but when I either changed it and convert the date there, nothing change. – Matt Stone Mar 11 at 12:27

1 Answer

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.

share|improve this answer
Thanks for answer, but Pattern is not my problem, I want to convert Date Value, I mean Gregorian To Solar Calendar. – Matt Stone Mar 11 at 13:16
Alright, you may want to consider revising your question. It's definitely not clear what you're trying to accomplish. – ach Mar 11 at 13:25
@MattStone does my edit help? – ach Mar 11 at 15:53
as a matter of fact, no dude! cos I could not do changes in java. my question is about converting date in javascript and only on view section I mean my jsp page not other places. – Matt Stone Mar 12 at 6:04
@MattStone In one of your comments to the question you mention trying to convert the date in the taglib code, which would imply that you have access to the Java. – ach Mar 12 at 12:41
show 3 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.