JSP Date Handling

One of the most important advantages of using JSP is that you can use all Java APIs. This chapter will explain in detail the Date class in Java. It is under 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 accepts a parameter, which represents the number of milliseconds from midnight on January 1, 1970 to the time to be represented.

Date(long millisec)

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

No. Method & Description
1 boolean after(Date date)If it is later than the given date, returns true; otherwise returns false.
2 boolean before(Date date)If it is earlier than the given date, returns true; otherwise returns false.
3 Object clone( )Returns a copy of the current object.
4 int compareTo(Date date)Returns 0 if equal to the given date, a negative number if earlier than the given date, and a positive number if later than the given date.
5 int compareTo(Object obj)Same as the compareTo(Date) method. If obj is not an object of the Date class or its subclass, throws a ClassCastException.
6 boolean equals(Object date)If the same as the given date, returns true, otherwise returns false.
7 long getTime( )Returns the number of milliseconds from midnight on January 1, 1970 to the time represented by this object.
8 int hashCode( )Returns the hash code of this object.
9 void setTime(long time)Sets the time and date using the given parameter, where the parameter time represents the number of milliseconds elapsed from midnight on January 1, 1970 to time.
10 String toString( )Converts this object to a string and returns the string.

Get current date and time

Using JSP programming, you can easily get the current date and time. Just use the toString() method of the Date object, like this:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.io.*,java.util.*, javax.servlet.*" %>
<html>
<head>
<title>显示当前时间与日期</title>
</head>
<body>

<h1>显示当前时间与日期</h1>

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

Save the above code in the main.jsp file, then visithttp://localhost:8080/testjsp/main.jsp, the result is as follows:

显示当前时间与日期

Sat Jun 25 17:54:34 CST 2016

Refreshhttp://localhost:8080/testjsp/main.jsp, you will find that the seconds obtained each time you refresh are different.


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 refer to the following methods:

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

Format dates with SimpleDateFormat

SimpleDateFormat formats and parses dates in a locale-sensitive manner. It allows you to format dates and times using custom patterns.

Modify CurrentDate.jsp slightly to get the following revised 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>显示当前时间与日期</title>
</head>
<body>

<h1>显示当前时间与日期</h1>

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

</body>
</html>

Compile main.jsp again, then visithttp://localhost:8080/testjsp/main.jsp, you can get the following result:

显示当前时间与日期

2016-06-25 17:57:53

SimpleDateFormat format codes

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

Character Description Example
G Era designator AD
y 4-digit year 2001
M month July or 07
d day 10
h 12-hour clock, A.M./P.M. (1~12) 12
H 24-hour clock 22
m Minute 30
s second 55
S Millisecond 234
E Weekday Tuesday
D Day of the year 360
F Day of the week in a month 2 (second Wed. in July)
w Week of the year 40
W Week of the month 1
a A.M./P.M. marker PM
k Hour in a day (1~24) 24
K Hour in a day, A.M./P.M. (0~11) 10
z Time zone Eastern Standard Time
' Text delimiter Delimiter
" Single quote `

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

Other extensions