简体   繁体   中英

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"/>

Answer

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

Wh yy ?

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".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM