Jsp Handling Date
π
2026-06-18 | π JSP
One of the most important advantages of using JSP is that you can use all Java APIs. This chapter will explain 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 using the current date and time.
Date( )
The second constructor takes a parameter, which represents the number of milliseconds from January 1, 1970, 00:00:00 GMT to the desired time.
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 this date is after the specified date, otherwise returns false. |
| 2 | **boolean before(Date date)** Returns true if this date is before the specified date, otherwise returns false. |
| 3 | **Object clone( )** Returns a copy of this object. |
| 4 | **int compareTo(Date date)** Returns 0 if the dates are equal; a negative value if this date is before the specified date; a positive value if this date is after the specified date. |
| 5 | **int compareTo(Object obj)** Same as compareTo(Date). Throws ClassCastException if obj is not an instance of Date or its subclass. |
| 6 | **boolean equals(Object date)** Returns true if this date is equal to the specified date, otherwise returns false. |
| 7 | **long getTime( )** Returns the number of milliseconds from January 1, 1970, 00:00:00 GMT to the time represented by this object. |
| 8 | **int hashCode( )** Returns the hash code value for this object. |
| 9 | **void setTime(long time)** Sets the date and time using the given parameter. The parameter `time` represents the number of milliseconds from January 1, 1970, 00:00:00 GMT to the specified time. |
| 10 | **String toString( )** Converts this object to a String and returns the String. |
* * *
## Getting Current Date and Time
Using JSP programming, it is very easy to get the current date and time. You just need to use the `toString()` method of the Date object, like this:
Display Current Time and DateDisplay Current Time and Date
<% Date date = new Date(); out.print( "" +date.toString()+"
"); %>
Save the above code in a file named `main.jsp`, then visit **http://localhost:8080/testjsp/main.jsp**. The result will be as follows:
Display Current Time and DateSat Jun 25 17:54:34 CST 2016
Refresh **http://localhost:8080/testjsp/main.jsp**, and you will notice that the seconds are different each time you refresh.
* * *
## 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 follow these methods:
* Use the `getTime()` method to get the number of milliseconds, then compare the milliseconds.
* Use the `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 the `Date` class.
* * *
## Formatting Dates with SimpleDateFormat
`SimpleDateFormat` formats and parses dates in a locale-sensitive manner. It allows you to format dates and times using a custom pattern.
Make a slight modification to `CurrentDate.jsp` to get the following modified code:
Display Current Time and DateDisplay Current Time and Date
<% Date dNow = new Date( ); SimpleDateFormat ft = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss"); out.print( "" + ft.format(dNow) + "
"); %>
Recompile `main.jsp`, then visit **http://localhost:8080/testjsp/main.jsp**. You will get the following result:
Display Current Time and Date2016-06-25 17:57:53
* * *
## SimpleDateFormat Format Codes
To specify the pattern string, you need to use the format codes listed in the following table:
| **Character** | **Description** | **Example** |
| --- | --- | --- |
| G | Era designator | AD |
| y | Year | 2001 |
| M | Month in year | July or 07 |
| d | Day in month | 10 |
| h | Hour in A.M./P.M. (1~12) | 12 |
| H | Hour in day (0~23) | 22 |
| m | Minute in hour | 30 |
| s | Second in minute | 55 |
| S | Millisecond | 234 |
| E | Day in 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 A.M./P.M. (0~11) | 10 |
| z | Time zone | Eastern Standard Time |
| ' | Text delimiter | Delimiter |
| " | Single quote | ` |
For more detailed information about the Date class, please refer to the Java API documentation.