JSP date processing


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

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 the early morning of January 1, 1970 to the time to be expressed.

Date(long millisec)

After obtaining the Date object, you can use all the methods listed in the following table:

##               1               2               3             4               5               6               7               8## 9void setTime(long time)               10String toString( )
Serial numberMethod & Description
boolean after(Date date)


Returns true if it is later than the given date, otherwise returns false

boolean before(Date date)


Returns true if it is earlier than the given date, otherwise returns false

Object clone( )


Get a copy of the current object

int compareTo(Date date)


If it is equal to the given date, it returns 0, if it is earlier than the given date, it returns a negative number, if it is later than the given date, it returns a positive number

int compareTo(Object obj)


Same as compareTo(Date) method, if obj is not an object of Date class or its subclass, ClassCastException is thrown

boolean equals(Object date)


Returns true if it is the same as the given date, otherwise returns false

long getTime( )


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

int hashCode( )


Returns the hash code of this object

Use the given parameters to set the time and date. The parameter time represents the number of milliseconds that elapsed from the early morning of January 1, 1970 to time

##Convert this object to a string and return this string

Get the current date and time

You can easily get the current date and time using JSP programming. Just use the toString() method of the Date object, like the following:

<%@ 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>

Replace the above The code is saved in the main.jsp file, and then accessed http://localhost:8080/testjsp/main.jsp, the running results are as follows:

显示当前时间与日期

Sat Jun 25 17:54:34 CST 2016

Refreshhttp:/ /localhost:8080/testjsp/main.jsp, you can find that the number of seconds obtained by each refresh is different.


Date Comparison

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

you can refer to the following method:

  •                   Use the getTime() method to get the number of milliseconds, and then compare the number of milliseconds.

  •             Use before(), after(), 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 using SimpleDateFormat

SimpleDateFormat uses a locale-sensitive way to format and parse dates, which allows you to use custom patterns to format dates and times.

Slightly modify CurrentDate.jsp and get the following modified 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, and then visit http://localhost:8080/testjsp/main .jsp, you can get the following results:

显示当前时间与日期

2016-06-25 17:57:53

SimpleDateFormat format code

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

##               y             4-digit year             2001             M             moon July or 07             d             day 10             h               12-hour format, A.M./P.M. (1~12)                 12             H             24-hour format               twenty two m               minute 30             s             Second 55             S             Milliseconds             234             E             Week Tuesday##               w             A certain week of the year             40             W             A certain week of the month             1 a             A.M./P.M. Mark           PM             k             An hour of the day (1~24)             twenty four K             An hour of the day, A.M./P.M. (0~11)               10             z Time zone Eastern Standard Time##               '             "For more detailed information about the Date class, please consult the Java API documentation.
CharactersDescriptionExample
              G               Era identifier             AD
              D             One day of the year             360
            F             A certain day of the week in a month           2 (second Wed. in July)
            Text separation             Delimiter
            apostrophe `