JSP obtains the current date and time

Source: Internet
Author: User
Tags ranges

1. First, let's see how to get the current time and display the Code:

------------------------------------------------

<%

Java. Text. simpledateformat formatter = newjava. Text. simpledateformat ("yyyy-mm-dd hh: mm: SS ");

Java. util. Date currenttime = new java. util. Date (); // obtain the current system time

String str_date1 = formatter. Format (currenttime); // format the date and time.

String str_date2 = currenttime. tostring (); // converts a date to a string.

%>

 

 

Formatted as "yyyy-mm-DDHH: mm: SS": <% = str_date1 %>

Unformatted Date and Time in string format: <% = str_date2 %>

Unformatted Date Format: <% = currenttime %>

 

 

-------------------------------------------------

Page output content:

Formatted as "yyyy-mm-DDHH: mm: SS" Date and Time: 2005-03-1709: 55: 40

Unformatted string format Date and Time: Thu Mar 17 09:55:40 CST 2005

Unformatted Date Format: Thu Mar 17 09:55:40 CST 2005

 

Usually we need the formatted time: 09:55:40. Now there is a question: "09:55:40" is a string. Sometimes we need to extract information about the year, month, and day in this string. What should we do? See the following code:

-------------------- Rule string ----------------------------------------------

String of the rule (four digits of the year, two digits of the month, two digits of the day, separated by the character "-" in the middle ):

<Br> the original string is:

<%

String date = "1989-12-30 ";

Out. println (date + "<br> ");

String year = date. substring (0th); // The value ranges from 4th.

String month = date. substring (5th); // The value ranges from 7th.

String day = date. substring (8th); // you can specify 10th + 1 to digits.

Out. println ("year =" + year + ", month =" + month + ", Day =" + Day );

%>

----------------------------------------------------------------------------------

From the code above, we can see that this is only applicable to the rule string (4 digits in a year, 2 digits in a month, and 2 digits in a day). What if it is irregular? The year may have two or four digits, and the month and the day may have one or two digits. What should I do? By modifying the code above, you can get the following general code (this "general" has a premise that the "-" must be separated between years, months, and days ):

---------------------- Irregular string ----------------------------------------------

Irregular string (the length of year, month, and day is not necessarily separated by the character "-" in the middle ):

<Br> the original string is:

<%

String date = "04-05-6 ";

Out. println (date + "<br> ");

Int A = date. indexof ("-"); // calculates the number of digits of the first "-".

Int B = date. lastindexof ("-"); // calculates the number of digits of the last "-".

Int Len = date. Length (); // evaluate the length of a string

Year = date. substring (0, a); // obtain the string before the first "-".

Month = date. substring (a + 1, B); // obtain the string between two "-".

Day = date. substring (B + 1, Len); // obtain the string after the last "-"

Out. println ("year =" + year + ", month =" + month + ", Day =" + Day );

%>

 

 

 

JSP obtains the current date and time (2009-05-0901: 24: 45) label: JSP
Time it
Category: JSP-Servlet

 

<Script language = "JavaScript">

VaR mydate = new date ();

Mydate. getyear (); // obtain the current year (2 digits)

Mydate. getfullyear (); // obtain the complete year (4 bits, 1970 -????)

Mydate. getmonth (); // obtain the current month (0-11, 0 represents January)

Mydate. getdate (); // obtain the current day (1-31)

Mydate. getday (); // obtain X of the current week (0-6, 0 indicates Sunday)

Mydate. gettime (); // obtain the current time (milliseconds starting from 1970.1.1)

Mydate. gethours (); // obtain the current hour (0-23)

Mydate. getminutes (); // obtain the current number of minutes (0-59)

Mydate. getseconds (); // obtain the current number of seconds (0-59)

Mydate. getmilliseconds (); // obtain the current number of milliseconds (0-999)

Mydate. tolocaledatestring (); // obtain the current date

Varmytime = mydate. tolocaletimestring (); // obtain the current time

Mydate. tolocalestring (); // obtain the date and time

 

If (mytime <"23:30:00 ")

{Alert (mytime );}

</SCRIPT>

 

<%

Java. Text. simpledateformat formatter = newjava. Text. simpledateformat ("yyyy-mm-dd hh: mm: SS ");

 

Java. util. Date currenttime = new java. util. Date (); // obtain the current system time

 

String str_date1 = formatter. Format (currenttime); // format the date and time.

String str_date2 = currenttime. tostring (); // converts a date to a string.

%>

 

 

<Tdclass = "classtd"> <% = str_date1 %> </TD>

 

 

 

On (rolover ){

_ Root. Stop ();

} On (rolout ){

_ Root. Play ();

}

 

Related Article

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.