Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

JSP date processing


May 12, 2021 JSP


Table of contents


JSP date processing

One of the most important advantages of using JSP is that you can use all Java APIs. This chapter details the Date class in Java, which encapsulates the current date and time under the java.util package.

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

Date( )

The second constructor accepts an argument that represents the number of milliseconds from the early hours of January 1, 1970 to the time to be represented.

Date(long millisec)

After you get the Date object, you'll be able to use all the methods listed in the following table:

Serial number Method . . .
1 boolean after(Date date)

If it is later than the given date, true is returned, otherwise false is returned

2 boolean before(Date date)

If it is earlier than the given date, true is returned, otherwise false is returned

3 Object clone( )

Gets a copy of the current object

4 int compareTo(Date date)

If equal to a given date, 0 is returned, a negative number is returned earlier than a given date, and a positive number is returned later than a given date

5 int compareTo(Object obj)

As with the CompareTo method, if the obj is not an object of the Date class or its subclasses, throw the ClassCastEx exception

6 boolean equals(Object date)

If it is the same as the given date, true is returned, otherwise false is returned

7 long getTime( )

Returns the number of milliseconds from the early hours of January 1, 1970, to the time represented by this object

8 int hashCode( )

Returns the hash code for this object

9 void setTime(long time)

Using a given parameter to set the time and date, the parameter time represents the number of milliseconds passing from the early hours of January 1, 1970 to the time

10 String toString( )

Convert this object to a string and return it


Gets the current date and time

Programming with JSP makes it easy to get the current date and time, as long as you use the toString() method of the Date object, as this is the case:

<%@ page import="java.io.*,java.util.*, javax.servlet.*" %>
<html>
<head>
<title>Display Current Date & Time</title>
</head>
<body>
<center>
<h1>Display Current Date & Time</h1>
</center>
<%    Date date = new Date();    out.print( "<h2 align=\"center\">" +date.toString()+"</h2>");
%>
</body>
</html>

Save the above code in the CurrentDate .jsp file, and then access http://localhost:8080/CurrentDate.jsp, which looks like this:

Display Current Date & Time
Mon Jun 21 21:46:49 GMT+04:00 2013

By http://localhost:8080/CurrentDate.jsp, you'll find that you get different seconds per refresh.


Date comparison

As I mentioned at the beginning, you can use any Java method in JSP scripts. If you want to compare two dates,

You can do this by following these methods:

  • Use the getTime() method to get milliseconds, and then compare milliseconds.
  • Use the before(), after(), equals() method. For example, new Date (99, 2, 12). before (99, 2, 18) returns true.
  • Using the comtareTo() method, which is defined in the Compale interface and implemented in Date.

Format the date using SimpleDateFormat

SimpleDateFormat formats and parses dates in a region-sensitive way, allowing you to format dates and times using custom patterns.

The CurrentDate .jsp be slightly modified to get the following modified code:

<%@ page import="java.io.*,java.util.*" %>
<%@ page import="javax.servlet.*,java.text.*" %>
<html>
<head>
<title>Display Current Date & Time</title>
</head>
<body>
<center>
<h1>Display Current Date & Time</h1>
</center>
<%    
Date dNow = new Date( );    
SimpleDateFormat ft = new SimpleDateFormat ("E yyyy.MM.dd 'at' hh:mm:ss a zzz");    
out.print( "<h2 align=\"center\">" + ft.format(dNow) + "</h2>");
%>
</body>
</html>

Compile the CurrentDate .jsp, and then http://localhost:8080/CurrentDate.jsp and you'll get the following results:

Display Current Date & Time
Mon 2013.06.21 at 10:06:44 PM GMT+04:00

SimpleDateFormat format code

To specify a pattern string, you need to use the format code listed in the following table:

Character Describe Example
G Time identifier AD
Y 4-digit year 2001
M Month July or 07
D Day 10
H 12-hour, A.M./P.M. (1-12) 12
H 24-hour system 22
M Minutes 30
s Seconds 55
S Milliseconds 234
E Week Tuesday
D One day of the year 360
F One day of the week of the month 2 (second Wed. in July)
W A week of the year 40
W A week of the month 1
A A.M./P.M. Tag Pm
K Hour of the day (1 to 24) 24
K An hour of the day, A.M./P.M. (0-11) 10
Z Time Eastern Standard Time
' Text separation Delimiter
" Single quotes `

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