JSP Date Handling
One of the most important advantages of using JSP is that you can use all Java APIs. This chapter will discuss in detail the Date class in Java, which is in the java.util package and encapsulates the current date and time.
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 representing the number of milliseconds from midnight, January 1, 1970, to the time to be represented.
Date(long millisec)
After obtaining a Date object, you can use all the methods listed in the following table:
| No. | Method** & **Description |
|---|---|
| 1 | boolean after(Date date) Returns true if later than the given date, otherwise returns false |
| 2 | boolean before(Date date) Returns true if earlier than the given date, otherwise returns false |
| 3 | Object clone( ) Returns a copy of the current object |
| 4 | int compareTo(Date date) Returns 0 if equal to the given date, a negative number if earlier than the given date, and a positive number if later than the given date |
| 5 | int compareTo(Object obj) Same as the compareTo(Date) method; throws a ClassCastException if obj is not an object of the Date class or its subclass |
| 6 | boolean equals(Object date) Returns true if the same as the given date, otherwise returns false |
| 7 | long getTime( ) Returns the number of milliseconds from midnight, 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) Sets the time and date using the given parameters; the parameter time represents the number of milliseconds elapsed from midnight, January 1, 1970, to time |
| 10 | String toString( ) Converts this object to a string and returns the string |
Get the current date and time
Getting the current date and time with JSP programming is very easy; 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.*" %>
显示当前时间与日期
显示当前时间与日期
<%
Date date = new Date();
out.print( "" +date.toString()+"
");
%>
Save the above code in the main.jsp file, then access http://localhost:8080/testjsp/main.jspthe result is as follows:
显示当前时间与日期
Sat Jun 25 17:54:34 CST 2016
Refresh http://localhost:8080/testjsp/main.jspyou will find that the seconds obtained with each refresh are different.
Date Comparison
As I mentioned at the beginning, you can use any Java method in JSP scripts. If you want to compare two dates,
you can refer to the following methods:
- Use the getTime() method to get the milliseconds, then simply compare the milliseconds.
- Use the before(), after(), and 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.
Formatting Dates with SimpleDateFormat
SimpleDateFormat formats and parses dates in a locale-sensitive way; it allows you to format dates and times using custom patterns.
With a slight modification to CurrentDate.jsp, the modified code is as follows:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.io.*,java.util.*" %>
<%@ page import="javax.servlet.*,java.text.*" %>
显示当前时间与日期
显示当前时间与日期
<%
Date dNow = new Date( );
SimpleDateFormat ft =
new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss");
out.print( "" + ft.format(dNow) + "
");
%>
Compile main.jsp again, then access http://localhost:8080/testjsp/main.jspto get the following result:
显示当前时间与日期
2016-06-25 17:57:53
SimpleDateFormat Format Codes
To specify a pattern string, you need to use the format codes listed in the following table:
| Character | Description | Example |
|---|---|---|
| G | Era designator | AD |
| y | 4-digit year | 2001 |
| M | Month | July or 07 |
| d | Day | 10 |
| h | 12-hour format, A.M./P.M. (1~12) | 12 |
| H | 24-hour format | 22 |
| m | Minute | 30 |
| s | Second | 55 |
| S | Millisecond | 234 |
| E | Week | Tuesday |
| D | Day in year | 360 |
| F | Day of week in month | 2 (second Wed. in July) |
| w | Week in year | 40 |
| W | Week in month | 1 |
| a | A.M./P.M. marker | PM |
| k | Hour in day (1~24) | 24 |
| K | Hour in day, A.M./P.M. (0~11) | 10 |
| z | Timezone | Eastern Standard Time |
| ' | Text separator | Delimiter |
| " | Single quote | ` |
For more detailed information about the Date class, please refer to the Java API documentation.