Search code examples
javadateformattingjstl

JSTL fmt how to format date like "yy"


I want to get a short year representation - "19" instead of "2019". How can it do that?

<fmt:formatDate value="${tempDate}" pattern="yyyy"/>

Solution

  • Answer

    <fmt:formatDate value="${tempDate}" pattern="yy"/>
    

    Whyy?

    Thanks to @gurioso for pointing out the JSTL spec here.

    Attribute: Patttern

    Required: No

    Description: Custom formatting pattern for dates and times. The patterns are specified by the java.text.SimpleDateFormat class. See table below.

    The table clarifies that y means year, and that by using the y argument is is possible to get formats like "2019" or "19".

    This functionality is further detailed in the java.text.SimpleDateFormat documentation.

    For formatting, if the number of pattern letters is 2, the year is truncated to 2 digits; otherwise it is interpreted as a number.

    Thus yyyy produces "19", while yy produces "2019".