WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

JSP date processing


One of the most important advantages of using JSP is that you can use all Java.  API. This chapter will detail 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 with the current date and time.

Date( )

The second constructor accepts a parameter indicating the number of milliseconds from the early morning of January 1, 1970 to the time to be represented.

Date(long millisec)

Once you have the Date object, you will be able to use all the methods listed in the table below:

Serial number Method & description
1 boolean after(Date date)

Return true if it is later than the given date, otherwise return false
2 boolean before(Date date)

Return true if it is earlier than the given date, otherwise return false
3 Object clone( )

Get a copy of the current object
4 int compareTo(Date date)

Returns 0 if it is equal to the given date, returns a negative number if it is earlier than the given date, and returns a positive number if it is later than the given date
5 int compareTo(Object obj)

Same as the compareTo(Date) method, throwing a ClassCastException if obj is not an object of the Date class or its subclasses
6 boolean equals(Object date)

Return true if it is the same as the given date, otherwise return false
7 long getTime( )

Returns the number of milliseconds from the early morning of January 1, 1970 to the time indicated by this object
8 int hashCode( )

Returns a hash code for this object
9 void setTime(long time)

Set the time and date using the given parameters, the parameter time represents the number of milliseconds elapsed from the early morning of January 1, 1970 to time
10 String toString( )

Convert this object to a string and return this string

Get current date and time

Using JSP programming makes it easy to 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>Show current time and date</title>
</head>
<body>

<h1>Show current time and date</h1>

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

Save the above code in the main.jsp file and access http://localhost:8080/testjsp/main.jsp. The results are as follows:

Show current time and date

Sat Jun 25 17:54:34 CST 2016

Refresh http://localhost:8080/testjsp/main.jsp and you'll see that the number of seconds you get per refresh is different.


date comparison

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

You can do this by referring to the following method:

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

Format the date using SimpleDateFormat

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

Make a slight modification to 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>Show current time and date</title>
</head>
<body>

<h1>Show current time and date</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 visit http://localhost:8080/testjsp/main.jsp to get the following result:

Show current time and date

2016-06-25 17:57:53

SimpleDateFormat format code

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

Character Description Example
G Age identifier AD
y 4-digit year 2001
M Month July or 07
d Day 10
h 12-hour system, 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 Someday of the year 360
F One day of the week in a month 2 (second Wed. in July)
w a week of the year 40
W a week in a month 1
a A.M./P.M. tag PM
k An hour of the day (1~24) 24
K One hour of the day, A.M./P.M. (0~11) 10
z Time zone Eastern Standard Time
' Text separation Delimiter
" Single quotes `

See the Java API documentation for more details on the Date class.