JSP date processing
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 expressed.
Date(long millisec)
After obtaining the Date object, you can use all the methods listed in the following table:
Serial number | Method & Description |
---|---|
boolean after(Date date)
| |
boolean before(Date date)
| |
Object clone( )
| |
int compareTo(Date date)
| |
int compareTo(Object obj)
| |
boolean equals(Object date)
| |
long getTime( )
| |
int hashCode( )
| |
void setTime(long time) |
Use the given parameters to set the time and date. The parameter time represents the number of milliseconds that elapsed from the early morning of January 1, 1970 to time |
String toString( ) | ##Convert this object to a string and return this string |
Get the current date and time
You can easily get the current date and time using JSP programming. Just use the toString() method of the Date object, like the following:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="java.io.*,java.util.*, javax.servlet.*" %> <html> <head> <title>显示当前时间与日期</title> </head> <body> <h1>显示当前时间与日期</h1> <% Date date = new Date(); out.print( "<h2 align=\"center\">" +date.toString()+"</h2>"); %> </body> </html>
Replace the above The code is saved in the main.jsp file, and then accessed http://localhost:8080/testjsp/main.jsp, the running results are as follows:
显示当前时间与日期 Sat Jun 25 17:54:34 CST 2016
Refreshhttp:/ /localhost:8080/testjsp/main.jsp, you can find that the number of seconds obtained by each refresh is different.
Date Comparison
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 method:
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.
Format dates using SimpleDateFormat
SimpleDateFormat uses a locale-sensitive way to format and parse dates, which allows you to use custom patterns to format dates and times.
Slightly modify 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>显示当前时间与日期</title> </head> <body> <h1>显示当前时间与日期</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, and then visit http://localhost:8080/testjsp/main .jsp, you can get the following results:
显示当前时间与日期 2016-06-25 17:57:53
SimpleDateFormat format code
To specify the pattern string, you need to use the format code listed in the following table:
Characters | Description | Example |
---|---|---|
G | Era identifier | AD |
4-digit year | 2001 | |
moon | July or 07 | |
day | 10 | |
12-hour format, A.M./P.M. (1~12) | 12 | |
24-hour format | twenty two | |
minute | 30 | |
Second | 55 | |
Milliseconds | 234 | |
Week | Tuesday | |
D | One day of the year | 360 |
F | A certain day of the week in a month | 2 (second Wed. in July) |
A certain week of the year | 40 | |
A certain week of the month | 1 | |
A.M./P.M. Mark | PM | |
An hour of the day (1~24) | twenty four | |
An hour of the day, A.M./P.M. (0~11) | 10 | |
Time zone | Eastern Standard Time | |
Text separation | Delimiter | |
apostrophe | ` |