Easy Tutorial
❮ Jstl Core Import Tag Jsp Architecture ❯

JSP Date Handling

One of the most important advantages of using JSP is the ability to utilize all Java APIs. This chapter will delve into the Java Date class, which is part of the java.util package and encapsulates the current date and time.

The Date class has two constructors. The first constructor initializes the object with the current date and time.

Date( )

The second constructor takes a single parameter representing the number of milliseconds since January 1, 1970, 00:00:00 GMT.

Date(long millisec)

Once you have a Date object, you can use all the methods listed in the table below:

No. Method & Description
1 boolean after(Date date) Returns true if the date is after the given date, otherwise false
2 boolean before(Date date) Returns true if the date is before the given date, otherwise false
3 Object clone( ) Returns a copy of the current object
4 int compareTo(Date date) Returns 0 if the dates are equal, a negative number if the date is before the given date, and a positive number if the date is after the given date
5 int compareTo(Object obj) Same as compareTo(Date), throws ClassCastException if obj is not an instance of Date or its subclass
6 boolean equals(Object date) Returns true if the dates are equal, otherwise false
7 long getTime( ) Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT
8 int hashCode( ) Returns the hash code for this object
9 void setTime(long time) Sets the time and date using the given parameter, which represents the number of milliseconds since January 1, 1970, 00:00:00 GMT
10 String toString( ) Converts the object to a string and returns this string

Retrieving Current Date and Time

It is easy to retrieve the current date and time using JSP by simply using the toString() method of the Date object, as shown below:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.io.*,java.util.*, javax.servlet.*" %>
<html>
<head>
<title>Display Current Date & Time</title>
</head>
<body>

<h1>Display Current Date & Time</h1>

<%
   Date date = new Date();
   out.print( "&lt;h2 align=\"center\">" +date.toString()+"</h2>");
%>
</body>
</html>

Save the above code in a file named main.jsp and access http://localhost:8080/testjsp/main.jsp. The output will be:

Display Current Date & Time

Sat Jun 25 17:54:34 CST 2016

Refreshing http://localhost:8080/testjsp/main.jsp will show different seconds each time.


Date Comparison

As mentioned earlier, you can use any Java method in your JSP scripts. If you want to compare two dates, you can do so as follows:


Using SimpleDateFormat to Format Dates

SimpleDateFormat provides a locale-sensitive way to format and parse dates, allowing you to format dates and times using custom patterns.

Modify CurrentDate.jsp slightly to get the following code:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.io.*,java.util.*" %>
<%@ page import="javax.servlet.*,java.text.*" %>
<html>
<head>
<title>Display Current Date and Time</title>
</head>
<body>

<h1>Display Current Date and Time</h1>

<%
   Date dNow = new Date( );
   SimpleDateFormat ft = 
   new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss");
   out.print( "&lt;h2 align=\"center\">" + ft.format(dNow) + "</h2>");
%>

</body>
</html>

Recompile main.jsp and then access http://localhost:8080/testjsp/main.jsp to get the following result:

Display Current Date and Time

2016-06-25 17:57:53

SimpleDateFormat Format Codes

To specify the pattern string, you need to use the format codes listed in the table below:

Character Description Example
G Era indicator AD
y Year in four digits 2001
M Month July or 07
d Day in month 10
h Hour in A.M./P.M. (1~12) 12
H Hour in day (0~23) 22
m Minute in hour 30
s Second in minute 55
S Millisecond 234
E Day in week Tuesday
D Day in year 360
F Day of week in month 2 (second Wed. in July)
w Week in year 40
W Week in month 1
a A.M./P.M. marker PM
k Hour in day (1~24) 24
K Hour in A.M./P.M. (0~11) 10
z Time zone Eastern Standard Time
' Escape for text Delimiter
" Single quote `

For more detailed information about the Date class, please refer to the Java API documentation. ```

❮ Jstl Core Import Tag Jsp Architecture ❯