0

In a JSP page I have to display some dates, but none of the available date formats (short, medium or long) suits me.

<fmt:formatDate value="${someDate}" type="date" dateStyle="short"/>
<fmt:formatDate value="${someDate}" type="date" dateStyle="medium"/>
<fmt:formatDate value="${someDate}" type="date" dateStyle="long"/>

I have to resort to a specified pattern:

<fmt:formatDate value="${someDate}" type="date" pattern="dd-MM/yyyy"/>

But, the pattern itself is locale dependent, I mean, I want to load the pattern from the message bundle. How can I do it?

I tried

<fmt:formatDate value="${someDate}" type="date" pattern="<fmt:message key='date.format.short'/>"/>

but it doesn't work.

rslemos
  • 2,454
  • 22
  • 32
  • I guess you want a German format? If so, set locale to German, then the dateStyle might suit better. – BalusC Mar 24 '20 at 20:38
  • @BalusC per your suggestion, the problem would remain unchanged, since for one locale (say cs) I would map another one (say de) and so on, so the "locale to set to" would be locale-dependent. No, I don't want a German format. That pattern there in my post is just an example. It could be virtually anything (any valid pattern, of course). I just want it to be locale-dependent. I'll edit the question to let it clear. – rslemos Mar 24 '20 at 20:49

1 Answers1

2

You can use the <c:set> tag to store the value of the pattern.

Something like this would do what you want:

<c:set var="myPattern"><fmt:message key="date.format.short" /></c:set>

<fmt:formatDate value="${someDate}" type="date" pattern="${myPattern}" />

Or better yet, as pointed by @Sachin, use the var attribute:

<fmt:message key="date.format.short" var="${myPattern}"/>

<fmt:formatDate value="${someDate}" type="date" pattern="${myPattern}" />
areus
  • 2,880
  • 2
  • 7
  • 17