Using fmt:formatDate for different locales
Guys,
I have this code in my jsp..
<fmt:formatDate value="${startDate}" pattern="MMMM dd, yyyy" />
I want to use a different pattern for french locale which is set in the page above. For the french I need the pattern to look like 'dd MMMM yyyy'. Whats the best approach where I dont have to put a "if then" locale check in the code.
any hints/clues.
[388 byte] By [
Debottama] at [2007-11-26 22:18:43]

# 1
<h1>Formatting with a locale set by setLocale</h1>
<c:if test="${!empty cookie.preferredLocale}">
<fmt:setLocale value="${cookie.preferredLocale.value}" />
</c:if>
<jsp:useBean id="now" class="java.util.Date" />
Date: <fmt:formatDate value="${now}" dateStyle="full" />
Number: <fmt:formatNumber value="${now.time}" />
mors1a at 2007-7-10 11:13:46 >

# 2
One option (as mentioned already)
- use the predefined formatting styles "full", "long", "medium", "short"
That will automatically reformat based on the locale set in JSTL
If none of those default options suit, another option is to use a resource bundle to define the date format string
ie in your resources.properties file:
dateFormatPattern = MMMM dd yy
and resources_fr.properties
dateFormatPattern = dd MMMM yy
and then in your code
<c:set var="dateFormatPattern"><fmt:message key="dateFormatPattern"/></c:set>
<fmt:formatDate value="${startDate}" pattern="${dateFormatPattern}"/>
Cheers,
evnafets