Ioan
Ioan

Reputation: 5187

JSTL formatDate shows year + 1 in JSP

I have a problem. I am using in jsp the following:

<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

<fmt:formatDate type="date" pattern="dd.MM.YYYY" value="${booking.startDate}"/>

having date as : 2014-12-29 or represented as long 1419807600000, which is displayed as 29.12.2015, which is wrong.

What I have found: formatDate uses internally Gregoriancalendar#getWeekYear() right after the following piece of code :

if (field == CalendarBuilder.WEEK_YEAR) {
        if (calendar.isWeekDateSupported()) {
            value = calendar.getWeekYear();
        }...

which goes to this piece of code inside GregorianCalendar#getWeekYear() :

        if (year > gregorianCutoverYear + 1) {
        int weekOfYear = internalGet(WEEK_OF_YEAR);
        if (internalGet(MONTH) == JANUARY) {
            if (weekOfYear >= 52) {
                --year;
            }
        } else {
            if (weekOfYear == 1) {
                ++year;
            }
        }
        return year;
    }

Obviosly, the problem is here :

if (weekOfYear == 1) {
      ++year;
}

So, week of the year is 1, because 29.12.2014 actually is in the first week of the year 2015. I use German locale for this, which means that the first week of year is the first week with 4 or more days in the new year.

Still, my format doesn't work properly. Any suggestions?

Thanks a lot.

Upvotes: 2

Views: 1179

Answers (1)

JB Nizet
JB Nizet

Reputation: 691775

YYYY is the week year, not the year. yyyy is the year. See http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

Upvotes: 3

Related Questions