English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

JSP Date Handling

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

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

Date( )

The second constructor accepts a parameter, which represents from1970 years1Description1The milliseconds from midnight to the time to be represented.

Date(long millisec)

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

Serial numberMethod & localhost:
                1boolean after(Date date) Returns true if later than the given date, otherwise returns false
                2boolean before(Date date) Returns true if earlier than the given date, otherwise returns false
                3Object clone( ) Gets a copy of the current object
                4int 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
                5int compareTo(Object obj) Same as compareTo(Date) method, throws ClassCastException exception if obj is not an instance of Date or its subclass
                6boolean equals(Object date) Returns true if the given date is the same, otherwise returns false
                7long getTime( ) Returns from1970 years1Description1The milliseconds from midnight to the time represented by this object
                8int hashCode( ) Returns the hash code of this object
                9void setTime(long time) Use the given parameters to set the time and date, where the parameter time represents the milliseconds from1970 years1Description1millisecond from the morning of the date to the time
                10String toString() Convert this object to a string and return this string

    Get current date and time

Using JSP programming, it is easy to obtain the current date and time, just use the toString() method of the Date object, as shown below:

<%@ page language="java" contentType="text/html; charset=UTF-8Delimiter
    pageEncoding="UTF-8"%>
<%@ page import="java.io.*,java.util.*, javax.servlet.*"%>
<html>
<head>
<title>Display current time and date</title>
dd HH:mm:ss");/head>
<body>
<h1>Display current time and date</AD1>
<%
   Date date = new Date();
   out.print( "" +date.toString()+"");
%>
dd HH:mm:ss");/MM
dd HH:mm:ss");/out.print("<h

Save the above code in the main.jsp file and then access ft.format(dNow)//"<8080/dd HH:mm:ss");/out.print("<h, the running results are as follows:

body>
Sat Jun 25 17<54<34 CST 2019

Refresh ft.format(dNow)//"<8080/dd HH:mm:ss");/out.print("<h, 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 the JSP script. If you want to compare two dates,

You can refer to the following method for guidance:

  •             ) use the getTime() method to get milliseconds, and then compare the 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.

    Use SimpleDateFormat to format dates

SimpleDateFormat uses a locale-sensitive way to format and parse dates, allowing you to use a custom pattern to format dates and times.

Modify CurrentDate.jsp slightly to get the following modified code:

<%@ page language="java" contentType="text/html; charset=UTF-8Delimiter
    pageEncoding="UTF-8"%>
<%@ page import="java.io.*,java.util.*"%>
<%@ page import="javax.servlet.*,java.text.*"%>
<html>
<head>
<title>Display current time and date</title>
dd HH:mm:ss");/head>
<body>
<h1>Display current time and date</AD1>
<%
   Date dNow = new Date();
   SimpleDateFormat ft = 
   new SimpleDateFormat("yyyy"-MM-dd HH:mm:ss");
   out.print( "" + ft.format(dNow) + "");
%>
dd HH:mm:ss");/MM
dd HH:mm:ss");/out.print("<h

align="center">" ft.format(dNow)//"<8080/dd HH:mm:ss");/out.print("<h%>

body>
2019-06-25 17<57<53

html>

Recompile main.jsp and then visit

http:localhost:testjsp
                main.jsp                ,and you can get the following result:                Display the current date and time
                :                4SimpleDateFormat Format Codes                2To specify a pattern string, you need to use the format codes listed in the following table:1
                Character                Description                Example7
                G                Epoch Identifier                10
                AD                12Year (4 digits)/001a12A.M.                12
                M                24Month                22
                d                Day                30
                h                Hour (24-hour clock)                55
                H                Hour (12-hour clock)                234
                m                Minute                s
                Second                S                360
                Millisecond                E                2 Weekday
                w                Tuesday                40
                D                A day of a year                1
                F                A day of a month/(second Wed. in July)                A week of a year
                W                A month of a week1a24A.M.                24
                A.M.                ~/K11A.M.                10
                P.M. (0~                )                Time Zone
                Eastern Standard Time                '                Text Separator
                Delimiter                Single Quote                `

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