JSTL FUNCTION

Add the Number of Days in the Date in JSTL

In this tutorial, we  will learn how to use the <fmt:formatDate> tag in the JSTL date format tags library with code example.

We use <fmt:formatDate> tag to format the date or time information using provided styles and pattern to pursue the current time in jstl.

The <fmt:formatDate> is very useful tag to display date and time in various formats we needed. Using the pattern we can have custom formatting options.

Supposingly the current time  in JSTL should in the date format like JSTL date format dd/mm/yyyy.

Attributes โ€“

NameRequiredTypeDescription
    
valueTruejava.lang.StringDate or time to be formatted.
typeFalsejava.lang.StringDetermines whether date or time or both to be formatted in the given date.
dateStyleFalsejava.lang.StringFormatting style for date. The date format can be specified with similar semantics in class java.text.DateFormat.
timeStyleFalsejava.lang.StringFormatting style for time. The time format can be specified with similar semantics in class java.text.DateFormat.
patternFalsejava.lang.StringPattern to be used for date and time when formatting.
timeZoneFalsejava.lang.StringTime zone to represent for the formatted time.
varFalsejava.lang.StringName of the variable to store the resulted formatted date or time.
scopeFalsejava.lang.StringScope to store the var.

Example –

<html>

ย ย ย <head>

ย ย ย ย ย ย <title>JSTL fmt:dateNumber Tag</title>

ย ย ย </head>

ย ย ย <body>

ย ย ย ย ย ย <h3>Number Format:</h3>

ย ย ย ย ย ย <c:set var = "now" value = "<% = new java.util.Date()%>" />

ย ย ย ย ย ย <p>Formatted Date (1): <fmt:formatDate type = "time"ย 

ย ย ย ย ย ย ย ย ย value = "${now}" /></p>

ย ย ย ย ย ย <p>Formatted Date (2): <fmt:formatDate type = "date"ย 

ย ย ย ย ย ย ย ย ย value = "${now}" /></p>

ย ย ย ย ย ย <p>Formatted Date (3): <fmt:formatDate type = "both"ย 

ย ย ย ย ย ย ย ย ย value = "${now}" /></p>

ย ย ย ย ย ย <p>Formatted Date (4): <fmt:formatDate type = "both"ย 

ย ย ย ย ย ย ย ย ย dateStyle = "short" timeStyle = "short" value = "${now}" /></p>

ย ย ย ย ย ย <p>Formatted Date (5): <fmt:formatDate type = "both"ย 

ย ย ย ย ย ย ย ย ย dateStyle = "medium" timeStyle = "medium" value = "${now}" /></p>

ย ย ย ย ย ย <p>Formatted Date (6): <fmt:formatDate type = "bothย  dateStyle = "long" timeStyle = "long" value = "${now}" /></p>

ย ย ย ย ย ย <p>Formatted Date (7): <fmt:formatDate pattern = "yyyy-MM-dd"ย 

ย ย ย ย ย ย ย ย ย value = "${now}" /></p>

ย ย ย </body>

</html>


OUTPUT โ€“

The above code will generate the following result โˆ’

Date Format:

Formatted Date (1): 14:27:18

Formatted Date (2): 23-Sep-2010

Formatted Date (3): 23-Sep-2010 14:27:18

Formatted Date (4): 23/09/10 14:27  

Formatted Date (5): 23-Sep-2010 14:27:18

Formatted Date (6): 23 September 2010 14:27:18 GST

Formatted Date (7): 2010-09-23

JSTL (JSP Standard Tag Library) is a good way to code in JSP pages. JSTL reduce the use of scriptlet and increase efficiency.
 
<fmt:setLocale value=”en_US” />
<fmt:parseDate type=”both” value =”2018-03-31 00:00:00.000″ var=”var” pattern=”yyyy-MM-dd HH:mm:ss” />
<c:set var=”now” value=”${var}” />
<c:set target=”${now}” property=”time” value=”${now.time + 86400000 * 4}” />
<fmt:formatDate value=”${now}” pattern=”yyyy-MM-dd HH:mm:ss” />
 
Line number 1: Declaration of Locale
 
Line number 2 : 
 
Type can be time or date or both: For now, we are taking Both
 
Value is the given input: For now, we are taking 2018-03-31 00:00:00.000
 
var is just a variable name for future use
 
Pattern: Which pattern you want to give in value, For now, we are taking “yyyy-MM-dd HH:mm:ss”
 
 
Line number 3: We just set var variable to a new variable “now”
 
Line number 4: Now we adding a number of days in the given time. 8640000 is the milliseconds in a day.
 
Line number 5: Now we give pattern in which we want to show the modified date.
 
Thanks for the reading post. I hope you like and understand the post. If you have any doubt regarding this post please comment below.

Leave a comment

Your email address will not be published. Required fields are marked *