Formatting date with fmt:formatDate Action

Date and time are important matters in internationalized web applications. Different country has its own way to present date and time in its own format. JSP has an action called <fmt:formatDate> that allows you to format date and time based on a specific locale.

The following is a table of <fmt:formatDate> action attributes. As you can see only value attribute is required for <fmt:formatDate> action.

<fmt:formatDate> Attributes
NameMandatoryMeaning
valueyesDate and/or time value to be formatted.
typenoAccept date or/and time to be used to format
dateStylenoSpecify predefined formatted style for date only if date value is used for formatting.
timeStylenoSpecify predefined formatted style for time only if the time value is used for formatting.
patternnoSpecify standard customized pattern for formatting date and/or time
timeZonenoIf the time value is used to format, this attribute specifies the time zone for that time value.
varnoThis attribute is for exporting scoped variable which stores the formatted date and/or time as a string
scopenospecify the scope of var.

Let’s take a look at an example of using <fmt:formatDate> action to format the date in the different locale.

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core"
          prefix="c" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/fmt"
          prefix="fmt" %>

<html>
    <head>
        <title>format Date Time</title>
    </head>
    <body>
        <jsp:useBean id="now" class="java.util.Date" />
        <p> Date format in US locale:
            <fmt:setLocale value="en_US" />
            <fmt:formatDate value="${now}" />
        </p>
        <p> Date format in France locale:
            <fmt:setLocale value="fr_FR" />
            <fmt:formatDate value="${now}"/>
        </p>

    </body>
</html>Code language: HTML, XML (xml)

First, we use the useBean tag to initialize a scoped variable called now and store current date value into this variable. Then, we set the locale to different locales: US and FR and print out the current date in the now variable using <fmt:formatDate> action.

Formatting Date and/or Time

By default <fmt:formatDate> action formats and displays only date. If you want to display time or both date and time, you can use the type attribute. The type attribute accepts date, time or both so that you can display it in a specific locale.

Let’s take a look at an example:

  <jsp:useBean id="now" class="java.util.Date" />
        <p>Date:
            <fmt:formatDate value="${now}"
                            type="date" /></p>
        <p>Time
            <fmt:formatDate value="${now}"
                            type="time" /></p>
        <p>Date & Time:
            <fmt:formatDate value="${now}"
                            type="both" /></p>Code language: JavaScript (javascript)

Formatting Date Time with predefined styles and custom patterns

You can format and print date & time with not only predefined styles provided by JSTL but also with a custom pattern. The timeStyle and dateStyle attributes of <fmt:formatDate> specify the predefined styles that you want to format. The patterns attribute allows you to define your own style. In this case, you need the following date & time code table to find exactly what you want to format.

CodeMeaning
GThe era (A.D., B.C., and so on)
yThe year (yy for two-digit years, yyyy for four digits)
MThe month (MM for numeric month, MMM or longer for month names or abbreviations)
wThe week of the year (ww for two digits)
WThe week of the month
DThe day of the year (DDD for three digits)
dThe day of the month (dd for two digits)
FThe numeric day of the week
EThe text day of the week (EEEE or longer for full name)
aAMa.m./PMp.m. indicator
HHour of the day (0–23)
kHour of the day (1–24)
KHour in a.m./p.m. (0–11)
hHour in a.m./p.m. (1–12)
mMinutes in an hour
sSeconds in a minute
SMilliseconds in second
zFull timezone name
ZRFC 822 time zone (for example, 0500)

Here is an example of using predefined styles and custom patterns to format and print date & time.

<jsp:useBean id="now" class="java.util.Date" />
<fmt:formatDate value ="${now}"
                type="both"
                timeStyle="short"
                dateStyle="full" /><br/>
<fmt:formatDate value ="${now}"
                type="time"
                timeStyle="long"
                /><br/>

<fmt:formatDate value ="${now}"
                pattern="EEEE hh:mm a"
                />Code language: JavaScript (javascript)