This article was last updated on : November 6th, 2016 at 4:27 am

Date format example in jsp (java.text.DateFormat)

Step by step guide for date formatting example in jsp

In this example we will demonstrate how to format date in jsp? We will use jsp scriptlets and expressions. We will use java.text.DateFormat to format current date.

In the end of this JSP Date formatting example we will see four different ways to display current date or any java.util.Date using java.text.DateFormat.

Procedures:

Please follow these 5 steps to execute the JSP date formatting example.

  1. Please create One dynamic Web Application with name “dateFormattingExampleInJSP”.
  1. Please create one jsp with name “currentDateFormattingInJSP.jsp”
    Please See below code for  currentDateFormattingInJSP.jsp

 

<[email protected] import=”java.text.DateFormat”%>
<[email protected] import=”java.util.Date”%>

<%@ page language=”java” contentType=”text/html; charset=ISO-8859-1″

pageEncoding=”ISO-8859-1″%>

<!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3.org/TR/html4/loose.dtd”>

<html>

<head>

<meta http-equiv=”Content-Type” content=”text/html; charset=ISO-8859-1″>

<title>Date Formatting in JSP</title>

</head>

<body>

<h3 style=”text-align: left;”>

A JSP Example to show current Date formatting <br>

using scriptlet and expression. <br>

</h3>

<br>

<%

Date today = new Date();

DateFormat defaultDateFormate = DateFormat.getDateInstance();

DateFormat shortDF = DateFormat.getDateInstance(DateFormat.SHORT);

DateFormat mediumDF = DateFormat.getDateInstance(DateFormat.MEDIUM);

DateFormat longDF = DateFormat.getDateInstance(DateFormat.LONG);

DateFormat fullDF = DateFormat.getDateInstance(DateFormat.FULL);

%>

<span style=”color: blue; text-align: left;”> In this jsp date formatting

example we are using <br>(1. DEFAULT, 2. SHORT, 3. MEDIUM, 4.

LONG and 5. FULL) <br>formatting styles of java.text.DateFormat

<br> to format current date.

</span>

<br>

<br>

<table border=”0″ cellpadding=”4″ cellspacing=”4″>

<tr style=”text-align: center;”><th>Date Formatting In JSP</th></tr>

<tr>

<td style=”text-align: right;”>(DateFormat Default Style is

like):</td>

<td style=”text-align: left;”><%=defaultDateFormate.format(today)%></td>

</tr>

<tr>

<td style=”text-align: right;”>(DateFormat SHORT Style is like):</td>

<td style=”text-align: left;”><%=shortDF.format(today)%></td>

</tr>

<tr>

<td style=”text-align: right;”>(DateFormat MEDIUM Style is

like):</td>

<td style=”text-align: left;”><%=mediumDF.format(today)%></td>

</tr>

<tr>

<td style=”text-align: right;”>(DateFormat LONG Style is like):</td>

<td style=”text-align: left;”><%=longDF.format(today)%></td>

</tr>

<tr>

<td style=”text-align: right;”>(DateFormat FULL Style is like):</td>

<td style=”text-align: left;”><%=fullDF.format(today)%></td>

</tr>

</table>

</body>

</html>
I am using Eclipse IDE to develop this JSP Date Formatting Example. Simply You can follow your own IDE or you can choose your way to do this example. Here is the image of project hierarchy in my Eclipse IDE while doing this project.

Image showing complete workspace for Date Format Example In JSP
image of project structure for jsp date format example
    1. Please deploy Your Web Application war “dateFormattingExampleInJSP.war” file in Web Application Servers. I am using Apache Tomcat Server. Start you Web Server.
      Precaution : Please delete tmp, work and log folders from you application server before starting your Application Server so that new changes can be taken effectively by Application Server.

 

4. Please open Internet Explorer and type this url in it :

http://localhost:8081/dateFormattingExampleInJSP/currentDateFormattingInJSP.jsp”
  1. Out put Date Formatting Example in JSP using Scriptlets and expression will be like image given below :
    Image showing JSP Date formatting Example Output Screen. Using java.text.DateFormat
    output for date formatting exaple in jsp
    So we have completed successfully our Date Formatting Example in JSP. We have used Scriptlets and Expression tags to write date formatting code in JSP.

Share this post:

Recent Posts

Leave a Comment