JSTL Formatting Tags

In JSTL, the role of formatting tag is to specify the type of format that represents date and time. The following syntax represent the URL of formatting tag: -

<%@ taglib uri = http://java.sun.com/jsp/jstl/fmt prefix = "fmt"  %>

Formatting tags

These are some commonly used formatting tags: -

  • <fmt:parseNumber>: -  This tag is used to parse the string on the basis of attribute associate with it.
  • <fmt:parseDate>: - This tag is used to parse the string of a date and time.
  • <fmt:formatDate>: - This tag handles the date in the various format.
  • <fmt:message>: - This tag is used to display the internationalized message.
  • <fmt:setTimeZone>: - This tag specifies the type of time zone.

Example of JSTL Format Tag

This is a simple example of JSTL format tag.

index.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> 
 <html> 
  <head> 
<title>Tutorial and Example</title> 
 </head> 
  <body> 
<h2>Format Tag example</h2>  
<c:set var="number" value="99.40" /> 
    <fmt:parseNumber var="n" integerOnly="true" type="number" value="${number}" /> 
    Parse Number is :: <c:out value="${n}" /> <br>    
<c:set var="date" value="02-04-2018" />  
<fmt:parseDate value="${date}" var="pd"  pattern="dd-MM-yyyy" /> 
Parse Date is :: <c:out value="${pd}" />
  <br>
  <c:set var="Date" value="<%=new java.util.Date()%>" /> 
Format Date is :: <fmt:formatDate type="time" value="${Date}"/>
  </body> 
</html>
Output: