Wednesday, February 1, 2012

JSTL Practice: Number and Date Formatting Tags

In this post, we will practice the Number and Date Formatting Tags

setLocale, formatNumber, formatDate, parseDate,
parseNumber, setTimeZone, timeZone

NOTE: The test result may different since the real test time and timezone / locale

The setLocale tag

The setLocale tag is used to set the locale,
assume we have the fragment below

<!-- set the locale to 'en_US' in session scope -->
<fmt:setLocale value="${'en_US'}" scope="session" /><br />
<!-- currency, locale setting will change currency format -->
<fmt:formatNumber type="currency" value="${1234.5678}" /><br />
<!-- set the locale to 'fr_FR' in session scope -->
<fmt:setLocale value="${'fr_FR'}" scope="session" /><br />
<!-- currency, locale setting will change currency format -->
<fmt:formatNumber type="currency" value="${1234.5678}" /><br /><br />

The result will be:


The 'en_US' and  'fr_FR' is the combination of language code and country code,
for more information, please refer to the official document:
http://java.sun.com/developer/technicalArticles/J2SE/locale/

The formatNumber tag

The formatNumber tag can format number as 'number', 'percent' or 'currency' and set format pattern,
assume we have the fragment below:

<!-- number -->
<fmt:formatNumber type="number" value="${1234.5678}" /><br />
<!-- using pattern format number, # denotes 0-n digital, 0 denotes one digital -->
<fmt:formatNumber type="number" value="${1234.5678}" pattern="#0.00 pound" /><br />
<!-- currency, locale setting will change currency format -->
<fmt:formatNumber type="currency" value="${1234.5678}" /><br />

<!-- percentage -->
<fmt:formatNumber type="percent" value="${1234.5678}" /><br />
<!-- percentage with maximum integer/fraction digits -->
<fmt:formatNumber type="percent" value="${1234.5678}"
    maxIntegerDigits="10" maxFractionDigits="10" /><br />
<!-- percentage with max/min integer/fraction digits -->
<fmt:formatNumber type="percent" value="${1234.5678}"
    maxIntegerDigits="10" maxFractionDigits="10"
    minIntegerDigits="8" minFractionDigits="4" /><br /><br />

The result will be


The parseNumber  tag

The parseNumber tag can parse String to number,
assume we have the fragment below:

<!-- parse string to number -->
<fmt:parseNumber var="parsedNumber" type="number" value="${'1,234.56'}" />
<c:out value="${parsedNumber + 3}" /><br /><br />

The result will be



The formatDate tag

The formatDate tag can format a date object with specific format pattern,
assume we have the fragment below

<!-- date, init type=date, using java.util.Date -->
<fmt:formatDate value="${theDate}" /><br />
<!-- date, type=both for show all, date for ymd, time for hms -->
<fmt:formatDate type="both" value="${theDate}" /><br />
<fmt:formatDate type="date" value="${theDate}" /><br />
<fmt:formatDate type="time" value="${theDate}" /><br />
<!-- date with pattern -->
<fmt:formatDate pattern="yyyy-MM-dd" value="${theDate}" /><br />
<!-- date with pattern -->
<fmt:formatDate pattern="HH:mm:ss" value="${theDate}" /><br /><br />

The result will be



The parseDate  tag

The parseDate tag can parse string to date,
assume we have the fragment below:

<fmt:parseDate var="parsedDate" value="${'12-12-2011'}" 
    pattern="dd-MM-yyyy" />
<c:out value="${parsedDate}" /><br /><br />

The result will be


The timeZone  tag

The timeZone  tag apply all its body content to specific timezone,
assume we have the fragment below:

<fmt:timeZone value="${'GMT+3'}">
    <!-- date, init type=date, using java.util.Date -->
    <fmt:formatDate type="both" value="${theDate}" /><br />
</fmt:timeZone>
<fmt:timeZone value="${'GMT+5'}">
    <!-- date, init type=date, using java.util.Date -->
    <fmt:formatDate type="both" value="${theDate}" /><br />
</fmt:timeZone>

The result will be


The setTimeZone  tag

The setTimeZone  tag set the timezone of the content after it,
assume we have the fragment below:

<!-- change time zone of page -->
<fmt:setTimeZone value="GMT+4" />
<!-- date, init type=date, using java.util.Date -->
<fmt:formatDate type="both" value="${theDate}" /><br />
<!-- apply timezone to body content -->
<fmt:timeZone value="${'GMT+3'}">
    <!-- date, init type=date, using java.util.Date -->
    <fmt:formatDate type="both" value="${theDate}" /><br />
</fmt:timeZone>
<fmt:formatDate type="both" value="${theDate}" /><br />
<!-- change time zone of page -->
<fmt:setTimeZone value="GMT+5" />
<!-- date, init type=date, using java.util.Date -->
<fmt:formatDate type="both" value="${theDate}" /><br />

The result will be


Download:
The full project of this practice is at github:
https://github.com/benbai123/JSP_Servlet_Practice/tree/master/Practice/JSTLPractice

Files of this practice:
src/test.jstl.filters.PracticeTwoFormatData.java
WebContent/practice_two__format_data.jsp

References:
http://www.tutorialspoint.com/jsp/jsp_standard_tag_library.htm

No comments:

Post a Comment