I开发者_如何学运维n a foreach loop, I set curMonth
and curDisplayedMonth
as follows:
<fmt:formatDate value="${curDate}" type="date" pattern="m" var="curMonth" />
<fmt:formatDate value="${curDate}" type="date" pattern="MMM" var="curDisplayedMonth" />
and use them in the dropdown list as
<option value="<c:out value="${curMonth}"/>" <c:if test="${selectedMonth == curMonth}">selected</c:if>>
<c:out value="${curDisplayedMonth}"/>
</option>
but it formats all to Jan:
and the values in the options are correct:
You need to correct your pattern
option. For displaying the month with JSTL formatDate, use the following options:
<%-- Displays numeric month (ex: 1) --%>
<fmt:formatDate value="${curDate}" pattern="M" />
<%-- Displays two-digit numeric month (ex: 01) --%>
<fmt:formatDate value="${curDate}" pattern="MM" />
<%-- Displays the month abbreviation (ex: Jan) --%>
<fmt:formatDate value="${curDate}" pattern="MMM" />
<%-- Displays the full month name (ex: January) --%>
<fmt:formatDate value="${curDate}" pattern="MMMM" />
Also, in your example above:
<fmt:formatDate value="${curDate}" type="date" pattern="m" var="curMonth" />
pattern="m"
represents the minute.
The type
and pattern
attributes are mutually exclusive. You use one or the other, but not both.
The type
attribute is just a convenient way to specify certain common patterns.
If you want to use pattern
, you should remove the type="date"
attributes.
精彩评论