The code for obtaining the current time and displaying it in JSP. The time displayed in JSP is year, month, and day.

Source: Internet
Author: User
1. First, let's see how to get the current time and display the Code:
------------------------------------------------
<%
Java. Text. simpledateformat formatter = new java. 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-dd hh: mm: SS": <% = str_date1 %>
Unformatted Date and Time in string format: <% = str_date2 %>
Unformatted Date Format: <% = currenttime %>
-------------------------------------------------
Page output content:
Formatted as "yyyy-mm-dd hh: mm: SS" Date and Time: 09: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 );
%>
----------------------------------------------------------------------------------
Now this problem has been solved. However, we will think that every time we need to split a string that represents year, month, and day, we need. writing such a piece of code on the JSP page is not only troublesome but also confusing the page. Can you solve this problem in a clear way? Of course you can. Just Use Javabean.

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.