Avinash Mishra
Avinash Mishra

Reputation: 1506

Get Month from int value

Can i get name of month in JSLT by giving an int value. Like If i=1 than January and So no...

It is not working

<fmt:formatDate pattern="M" dateStyle="long" value="${monthInt}" />

Upvotes: 3

Views: 2073

Answers (4)

dap.tci
dap.tci

Reputation: 2485

Another way:

<fmt:parseDate value="${monthInt}" dateStyle="long" pattern="M" var="monthDate"></fmt:parseDate> 
<fmt:formatDate value="${monthDate}" pattern="MMMM"></fmt:formatDate>

Upvotes: 1

Shubam Singh
Shubam Singh

Reputation: 96

You could use <jsp:useBean> as:

<jsp:useBean id="monthNames" class="java.text.DateFormatSymbols" />
<c:set value="${monthNames.months}" var="months" />

Now, I have initialized list of months using <c:set> corresponding to the index. You could access it using months which is declared in var.

You could use it as ${months[0]} it will give you January.

Hope it will work.!!

Upvotes: 3

Qwerky
Qwerky

Reputation: 18445

This won't work. The tag formats a Date object into a String, so the value needs to be a Date. You're giving it an int.

If you need a list of months then you might want to consider creating a list and putting it in the session.

Upvotes: 1

Kevin Bowersox
Kevin Bowersox

Reputation: 94469

JSTL expects a Date to be provided to the fmt:formatDate tag. In the example an int is provided. If you have extracted this int from a Date object, just pass that Date instead, no need for extracting the month value.

<fmt:formatDate pattern="M" dateStyle="long" value="${someDate}" />

Upvotes: 2

Related Questions