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 | ||
---|---|---|
Name | Mandatory | Meaning |
value | yes | Date and/or time value to be formatted. |
type | no | Accept date or/and time to be used to format |
dateStyle | no | Specify predefined formatted style for date only if date value is used for formatting. |
timeStyle | no | Specify predefined formatted style for time only if the time value is used for formatting. |
pattern | no | Specify standard customized pattern for formatting date and/or time |
timeZone | no | If the time value is used to format, this attribute specifies the time zone for that time value. |
var | no | This attribute is for exporting scoped variable which stores the formatted date and/or time as a string |
scope | no | specify 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.
Code | Meaning |
---|---|
G | The era (A.D., B.C., and so on) |
y | The year (yy for two-digit years, yyyy for four digits) |
M | The month (MM for numeric month, MMM or longer for month names or abbreviations) |
w | The week of the year (ww for two digits) |
W | The week of the month |
D | The day of the year (DDD for three digits) |
d | The day of the month (dd for two digits) |
F | The numeric day of the week |
E | The text day of the week (EEEE or longer for full name) |
a | AMa.m./PMp.m. indicator |
H | Hour of the day (0–23) |
k | Hour of the day (1–24) |
K | Hour in a.m./p.m. (0–11) |
h | Hour in a.m./p.m. (1–12) |
m | Minutes in an hour |
s | Seconds in a minute |
S | Milliseconds in second |
z | Full timezone name |
Z | RFC 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)