Latest web development tutorials

JSP date processing

One of the most important advantages of using JSP, is that you can use all the Java API. This chapter describes in detail the Java Date class, which in the java.util package encapsulates the current date and time.

Date class has two constructors. The first constructor uses the current date and time to initialize the object.

Date( )

The second constructor accepts a parameter, which represents from 1 January 1970 to the early morning to express the number of milliseconds.

Date(long millisec)

After obtaining the Date object, you can use all the methods listed in the following table:

No. Method & description
1 boolean after (Date date)

If later than the date given, returns true, false otherwise
2 boolean before (Date date)

If the given date is earlier than, returns true, false otherwise
3 Object clone ()

Get a copy of the current object
4 int compareTo (Date date)

If the given date is equal to 0 is returned if the given date is earlier than that, it returns a negative number, if later than the given date, returns a positive number
5 int compareTo (Object obj)

The same method compareTo (Date), Date if obj is not an object class or sub-class, a ClassCastException
6 boolean equals (Object date)

If given the same date, it returns true, false otherwise
7 long getTime ()

Returns the number of milliseconds from January 1, 1970 in the morning to this object represents
8 int hashCode ()

Returns the hash code for this object
9 void setTime (long time)

Using the given parameters to set the time and date, time parameter represents the number of milliseconds from January 1, 1970 to the early hours of elapsed time
10 String toString ()

Converting this object to a string and returns the string

Gets the current date and time

Use JSP programming can easily get the current date and time, just use the Date object's toString () method on the line, like this:

<%@ 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>

The above code is stored in main.jsp file, and then visit http: // localhost: 8080 / testjsp / main.jsp, results are as follows:

显示当前时间与日期

Sat Jun 25 17:54:34 CST 2016

Refresh http: // localhost: 8080 / testjsp / main.jsp, you can find the number of seconds each refresh obtained are not the same.


Compare dates

As I mentioned at the beginning, you can use any Java methods in the JSP script. If you want to compare two dates,

You can refer to the following method to do:

  • Use getTime () method to get the number of milliseconds, then compare the number of milliseconds on the line.
  • Use before (), after (), equals () method. For example, new Date (99,2,12) .before (new Date (99,2,18)) returns true.
  • Use compareTo () method, which is defined in the Comparable interface, implemented in the Date.

The use SimpleDateFormat to format dates

SimpleDateFormat using a sensitive way to the region to date formatting and parsing, which allows you to use a custom pattern to format dates and times.

CurrentDate.jsp minor modifications to give the code as revised:

<%@ 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 codes are listed in the following table:

character description Example
G Times identifier AD
y 4-digit year 2001
M month July or 07
d day 10
h 12-hour, AM / PM (1 ~ 12) 12
H 24-hour twenty two
m minute 30
s second 55
S millisecond 234
E week Tuesday
D Day of year 360
F Day of the month for a week 2 (second Wed. in July)
w Year for a week 40
W Month in a week 1
a AM / PM mark PM
k Certain hours of the day (1 to 24) twenty four
K The day an hour, AM / PM (0 ~ 11) 10
z Time zone Eastern Standard Time
' Delimited text Delimiter
" apostrophe `

For more detailed information about the Date class, please consult the Java API documentation.