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 represented.
Date(long millisec)After obtaining the Date object, you can use all the methods listed in the following table:
serial number | Method & Description |
---|---|
1 | boolean after(Date date) returns true if it is later than the given date, otherwise it returns false |
2 | boolean before(Date date) returns true if it is earlier than the given date, otherwise it returns false |
3 | Object clone() obtains a copy of the current object |
4 | int compareTo(Date date) returns 0 if it is equal to the given date, a negative number if it is earlier than the given date, and a positive number if it is later than the given date |
5 | int compareTo(Object obj) is the same as compareTo(Date). If obj is not an object of the Date class or its subclass, a ClassCastException is thrown. |
6 | boolean equals(Object date) returns true if it is the same as the given date, otherwise it returns false |
7 | long getTime() returns the number of milliseconds from the early morning of 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) uses the given parameters to set the time and date. The parameter time represents the number of milliseconds that have elapsed from the early morning of January 1, 1970 to time |
10 | String toString() converts this object to a string and returns this string |
It is easy to get the current date and time using JSP programming. Just use the toString() method of the Date object, like the following:
<%@ 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 visit http://localhost:8080/CurrentDate.jsp. The running results are as follows:
Display Current Date & TimeMon Jun 21 21:46:49 GMT+04:00 2013Refresh http://localhost:8080/CurrentDate.jsp, and you will find that the seconds obtained by each refresh are different.
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 methods to do it:
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.
SimpleDateFormat uses a locale-sensitive way to format and parse dates, which allows you to use custom patterns to format dates and times.
Modify CurrentDate.jsp slightly and 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 CurrentDate.jsp again, and then visit http://localhost:8080/CurrentDate.jsp. You will get the following results:
Display Current Date & TimeMon 2013.06.21 at 10:06:44 PM GMT+04:00To specify a pattern string, use the format codes listed in the following table:
character | describe | Example |
---|---|---|
G | era identifier | AD |
y | 4-digit year | 2001 |
M | moon | July or 07 |
d | day | 10 |
h | 12-hour clock, AM/PM (1~12) | 12 |
H | 24 hour clock | twenty two |
m | minute | 30 |
s | Second | 55 |
S | millisecond | 234 |
E | Week | Tuesday |
D | day of the year | 360 |
F | a day of the week in a month | 2 (second Wed. in July) |
w | week of the year | 40 |
W | a certain week of the month | 1 |
a | AM/PM mark | PM |
k | An hour of the day (1~24) | twenty four |
K | Hour of the day, AM/PM (0~11) | 10 |
z | time zone | Eastern Standard Time |
' | text separator | Delimiter |
" | single quote | ` |
For more detailed information about the Date class, please consult the Java API documentation.