May 15, 2021 Servlet
2. Gets the current date and time
3. Mon Jun 21 21:46:49 GMT+04:00 2010
5. Format the date with SimpleDateFormat
One of the most important advantages of using servlets is that you can use most of the methods available in core Java. This chapter explains the Date class in the java.util package provided by Java, which encapsulates the current date and time.
The Date class supports two constructors. The first constructor initializes the object of the current date and time.
Date( )
The constructor below accepts an argument equal to the number of milliseconds that have passed since midnight on January 1, 1970.
Date(long millisec)
Once you have an 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)
Returns true if the date contained in the Date object is after the date specified, otherwise returns false. |
2 |
boolean before(Date date)
Returns true if the date included in the called Date object is returned before the date specified, otherwise returns false. |
3 |
Object clone( )
Repeat the Date object again. |
4 |
int compareTo(Date date)
Compare the value of the call object with the value of the Date.Returns 0 if the two values are equal.Returns a negative value if you call an object before Date.Returns a positive value if you call an object after Date. |
5 |
int compareTo(Object obj)
If the OBJ is a DATE class, the operation is equivalent to Compareto (Date).Otherwise, it will throw a ClassCastException. |
6 |
boolean equals(Object date)
Returns true if the time and date included in the called Date object is specified as Date, otherwise returns false. |
7 |
long getTime( )
Returns the number of milliseconds after January 1, 1970. |
8 |
int hashCode( )
Returns the hash code for the call object. |
9 |
void setTime(long time)
Set the time and date specified by TIME, which means that the time (in milliseconds) since Midnight on January 1, 1970. |
10 |
String toString( )
The DATE object that converts the call is a string and return the result. |
Getting the current date and time in Java Servlet is easy. You can use a simple Date object toString() method to output the current date and time, as follows:
// 导入必需的 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's compile the servlet above, create the appropriate entry .xml the web-based file, and then call http://localhost:8080/CurrentDate the servlet by accessing the http://localhost:8080/CurrentDate. This will produce the following results:
Displays the current date and timeMon Jun 21 21:46:49 GMT+04:00 2010 |
Try refreshing the URL http://localhost:8080/CurrentDate, and every few seconds you'll notice a difference in display time.
As mentioned above, you can use all available Java methods in servlets. If you need to compare two dates, here's how:
SimpleDateFormat is a specific class that formats and parses dates in a language-sensitive way. SimpleDateFormat allows you to select any user-defined pattern for date-time formatting.
Let's modify the example above, 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 servlet above again, and then call http://localhost:8080/CurrentDate access to the servlet. This will produce the following results:
Displays the current date and timeMon 2010.06.21 at 10:06:44 PM GMT+04:00 |
Use the event pattern string to specify the time format. In this mode, all ASCII letters are retained as pattern letters, which are defined as follows:
Character | Describe | Instance |
---|---|---|
G | Era indicator | AD |
Y | Four digits represent the year | 2001 |
M | The month of the year | Julie or 07 |
D | The first day of the month | 10 |
H | Hours .M A./P.M. (1-12) | 12 |
H | Hours of the day (0-23) | 22 |
M | The first minute of the hour | 30 |
s | The second of a minute | 55 |
S | Milliseconds | 234 |
E | The day of the week of the week | Tuesday |
D | The first day of the year | 360 |
F | The week is the first week of the month | 2 (second Wed. in July) |
W | The first few weeks of the year | 40 |
W | The first few weeks of January | 1 |
A | A.M./P.M. Tag | Pm |
K | The first few hours (1 ~ 24) | 24 |
K | Hours with A.M./p.m (0 ~ 11) | 10 |
Z | Time | Eastern Standard Time |
' | Escape for text | Delimiter |
" | Single quotes | ` |
For a complete list of available processing date methods, you can refer to the standard Java documentation.