Servlet processing date
One of the most important advantages of using Servlets is that you can use most of the methods available in core Java. This chapter will explain the Date class in the java.util package provided by Java. This class encapsulates the current date and time.
The Date class supports two constructors. The first constructor initializes an object with the current date and time.
Date( )
The following constructor accepts a parameter equal to the number of milliseconds that have elapsed since midnight on January 1, 1970.
Date(long millisec)
Once you have a available Date object, you can call any of the following supported methods to use the date:
Serial number | Method & Description |
---|---|
1 | boolean after(Date date) If the date contained in the called Date object is specified by date After the date, it returns true, otherwise it returns false. |
2 | boolean before(Date date) If the date contained in the called Date object is before the date specified by date, It returns true, otherwise it returns false. |
3 | Object clone( ) Repeatedly call the Date object. |
4 | int compareTo(Date date) Compare the value of the calling object with the value of date. If the two values are equal, 0 is returned. If the calling object precedes date, a negative value is returned. If the calling object comes after date, a positive value is returned. |
5 | int compareTo(Object obj) If obj is a Date class, the operation is equivalent to compareTo(Date). Otherwise, it throws a ClassCastException. |
6 | boolean equals(Object date) If the time and date contained in the called Date object are the same as those specified by date , returns true, otherwise returns false. |
7 | long getTime( ) Returns the number of milliseconds that have elapsed since January 1, 1970. |
8 | int hashCode( ) Returns the hash code for the calling object. |
9 | void setTime(long time) Set time to the specified time and date, which means since January 1, 1970 The elapsed time in milliseconds since midnight of the day. |
10 | String toString( ) Convert the called Date object to a string and return the result. |
Get the current date and time
Getting the current date and time in Java Servlet is very easy. You can use the toString() method of a simple Date object to output the current date and time, as shown below:
// 导入必需的 java 库 import java.io.*; import java.util.Date; import javax.servlet.*; import javax.servlet.http.*; // 扩展 HttpServlet 类 public class CurrentDate extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 设置响应内容类型 response.setContentType("text/html"); PrintWriter out = response.getWriter(); String title = "显示当前的日期和时间"; Date date = new Date(); String docType = "<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n"; out.println(docType + "<html>\n" + "<head><title>" + title + "</title></head>\n" + "<body bgcolor=\"#f0f0f0\">\n" + "<h1 align=\"center\">" + title + "</h1>\n" + "<h2 align=\"center\">" + date.toString() + "</h2>\n" + "</body></html>"); } }
Now, let us compile the above Servlet and run Create the appropriate entries in the web.xml file and then call the servlet by accessing http://localhost:8080/CurrentDate. This will produce the following result:
Display the current date and timeMon Jun 21 21:46:49 GMT+04: 00 2010 |
Try refreshing the URL http://localhost:8080/CurrentDate, you will notice the difference in the displayed time every few seconds.
Date Comparison
As mentioned above, you can use all available Java methods in Servlet. If you need to compare two dates, here's how:
You can use getTime() to get the elapsed time (in milliseconds) since midnight on January 1, 1970 for both objects unit) and then compare the two values.
You can use the methods before( ), after( ) and equals( ). Since the 12th is before the 18th in a month, for example, new Date(99, 2, 12).before(new Date (99, 2, 18)) returns true.
You can use the compareTo( ) method, which is defined by the Comparable interface and implemented by Date.
Formatting dates using SimpleDateFormat
SimpleDateFormat is a concrete class for formatting and parsing dates in a locale-sensitive manner. SimpleDateFormat allows you to choose any user-defined date-time formatting mode.
Let us modify the above example as follows:
// 导入必需的 java 库 import java.io.*; import java.text.*; import java.util.Date; import javax.servlet.*; import javax.servlet.http.*; // 扩展 HttpServlet 类 public class CurrentDate extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 设置响应内容类型 response.setContentType("text/html"); PrintWriter out = response.getWriter(); String title = "显示当前的日期和时间"; Date dNow = new Date( ); SimpleDateFormat ft = new SimpleDateFormat ("E yyyy.MM.dd 'at' hh:mm:ss a zzz"); String docType = "<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n"; out.println(docType + "<html>\n" + "<head><title>" + title + "</title></head>\n" + "<body bgcolor=\"#f0f0f0\">\n" + "<h1 align=\"center\">" + title + "</h1>\n" + "<h2 align=\"center\">" + ft.format(dNow) + "</h2>\n" + "</body></html>"); } }
Compile the above Servlet again, and then call the Servlet by accessing http://localhost:8080/CurrentDate. This will produce the following result:
Display the current date and timeMon 2010.06.21 at 10:06:44 PM GMT +04:00 |
Format codes for simple date formats
Use event pattern strings to specify the time format. In this mode, all ASCII letters are reserved as mode letters, which are defined as follows:
Character | Description | Example |
---|---|---|
G | Era indicator | AD |
y | four digits The year represented by the number | 2001 |
M | The month in the year | July or 07 |
d | Days of January | 10 |
With A.M./P.M. The hour (1~12) | 12 | |
The hour of the day (0~23) | 22 | |
What minute of the hour | 30 | |
The number of seconds in a minute | 55 | |
Milliseconds | 234 | |
Day of the week | Tuesday | ##D |
##360 | F | |
2 (second Wed. in July) | w | |
40 | W | |
1 | a | |
PM | k | |
24 | K | |
10 | z | |
Eastern Standard Time | ' | |
Delimiter | " | |
` | For a complete list of available methods for working with dates, you can refer to the standard Java documentation |