jsp-runoob:jsp Date Processing

Source: Internet
Author: User
Tags time and date

ylbtech-jsp-runoob:jsp Date Processing

1. back to top
1. JSPDate Processing

one of the most important advantages of using JSP is that you can use all of the Java APIs. This chapter will describe in detail the date class in Java, which, under the Java.util package, encapsulates the current date and time.

The date class has two constructors. The first constructor initializes an object with the current date and time .

Date() 

The second constructor takes a parameter that represents the number of milliseconds from midnight January 1, 1970 to the time that you want to represent .

Date(long millisec)   

After you get the Date object, you are able to use all the methods listed in the following table:

Serial Number method & Description
1 Boolean after (date date)

Returns true if it is later than the given date, otherwise false

2 Boolean before (date date)

Returns true if it is earlier than the given date, otherwise false

3 Object Clone ()

Gets a copy of the current object

4 int CompareTo (date date)

Returns 0 if it is equalto a given date, returns a negative number if it is earlier than the given day, and returns a positive number if it is later than the given period

5 int compareTo (Object obj)

Same as the CompareTo (date) method, throws a ClassCastException exception if obj is not an object of the date class or its subclasses

6 Boolean equals (Object date)

Returns true if it is the same as the given date, otherwise false

7 Long GetTime ()

Returns the number of milliseconds from the early morning of January 1, 1970 to the time represented by the object

8 int Hashcode ()

Returns the hash code for this object

9 void SetTime (long time)

Sets the time and date with the given parameters, which indicates the number of milliseconds elapsed from the wee hours of January 1, 1970 to

10 String toString ()

Converts this object to a string and returns the string

Get the current date and time

Using JSP programming makes it easy to get the current date and time, as long as the ToString () method of the Date object is used, as follows:

<%@ Page Language="Java"ContentType="text/html; Charset=utf-8"pageencoding="UTF-8"%><%@ Page Import="java.io.*,java.util.*, javax.servlet.*" %><HTML><Head><title>Show current time and date</title></Head><Body><H1>Show current time and date</H1><%   Date Date = New Date(); Out.print ("

"Center\">" +Date. toString ()+"");%></Body></HTML>

Save the above code in the main.jsp file, and then access the http://localhost:8080/testjsp/main.jsp, running the result as follows:

Show current time and date  SatJune: si:      

By refreshing http://localhost:8080/testjsp/main.jsp, you can see that the number of seconds each refresh will be different.

Date comparison

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

You can do this by referring to the following methods:

    • Use the GetTime () method to get the number of milliseconds and then compare the number of milliseconds.
    • Use the Before (), after (), Equals () method. For example, new Date (99,2,12). Before (new Date (99,2,18)) returns True.
    • Using the CompareTo () method, this method is defined in the comparable interface and implemented in date.
Format dates with SimpleDateFormat

SimpleDateFormat uses a locale-sensitive way to format and parse dates, which allows you to format dates and times using a custom pattern.

Make a slight change to 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>Show current time and date</title></Head><Body><H1>Show current time and date</H1><%   DateDnow= New Date( ); SimpleDateFormat ft=    NewSimpleDateFormat ("YYYY-MM-DD HH:mm:ss"); Out.print ("

"Center\">" +Ft.format (Dnow)+ "");%></Body></HTML>

Compiling main.jsp again, and then accessing http://localhost:8080/testjsp/main.jsp, you can get the following results:

Show current time and date  : £ º     
SimpleDateFormat Format Code

To specify a pattern string, you need to use the format codes listed in the following table:

character Description Example
G Age Identifier AD
Y 4-digit year 2001
M Month July or 07
D Day 10
H 12-hour system, a.m./p.m. (1~12) 12
H 24-hour system 22
M Minutes 30
S Seconds 55
S Milliseconds 234
E Week Tuesday
D One day of the year 360
F One day of the week in one months 2 (Second Wed. In July)
W A week of the year 40
W A week in the one months 1
A a.m./p.m. Marking Pm
K An hour of the day (1~24) 24
K An hour of the day, a.m./p.m. (0~11) 10
Z Time Eastern Standard Time
Text delimited Delimiter
" Single quotation marks `

For more information about the date class, consult the Java API documentation.

2.
2. back to top
3. back to top
4. back to top
5. back to top
0, HTTP://WWW.RUNOOB.COM/JSP/JSP-HANDLING-DATE.HTML1,
6. back to top
Ylbtech
Source: http://ylbtech.cnblogs.com/
This article is copyright to the author and the blog Park, Welcome to reprint, but without the consent of the author must retain this paragraph, and in the article page obvious location to the original link, otherwise reserves the right to pursue legal responsibility.

jsp-runoob:jsp Date Processing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.