Date issues and other issues in JSP

Source: Internet
Author: User
Date Problems
1. Get the current server Date:

[Copy to clipboard] [-]

Code:

<% @ Page import = "Java. util. Date" %>
<%
Date mydate = new date ();
%>

2. Obtain the current year, month, and day:

[Copy to clipboard] [-]

Code:

<% @ Page import = "Java. util. Date" %>
<%
Date mydate = new date ();
Int Thisyear = mydate. getyear () + 1900; // Thisyear = 2003
Int thismonth = mydate. getmonth () + 1; // thismonth = 5
Int thisdate = mydate. getdate (); // thisdate = 30
%>

3. output the current date by local time zone

[Copy to clipboard] [-]

Code:

<% @ Page import = "Java. util. Date" %>
<%
Date mydate = new date ();
Out. println (mydate. tolocalestring ());
%>

Output result:
2003-5-30
4. Obtain the value of the field name "publish_time" and datetime type in the database.

[Copy to clipboard] [-]

Code:

<% @ Page import = "Java. util. Date" %>
<%
... Connect to the database...
Resultset rs =...
Date sdate = Rs. getdate ("publish_time ");
%>
[Code]
5. Print the date in the specified format
[Code]
<% @ Page import = "Java. util. Date" %>
<% @ Page import = "Java. Text. dateformat" %>
<%
Date dnow = new date ();
Simpledateformat formatter = new simpledateformat ("e YYYY. Mm. dd' at 'hh: mm: ss a Zzz ");
Out. println ("it is" + formatter. Format (dnow ));
%>

The output result is:
It is Friday 2003.05.30 at 11:30:46 am CST
(For more detailed format symbols, see simpledateformat)
6. convert a string to a date

[Copy to clipboard] [-]

Code:

<% @ Page import = "Java. util. Date" %>
<% @ Page import = "Java. Text. dateformat" %>
<%
String inputs = "1222-11-11 ";
Simpledateformat formatter = new simpledateformat ("yyyy-mm-dd ");
Date T = NULL;
Try {
T = formatter. parse (input );
Out. println (t );
} Catch (parseexception e ){
Out. println ("unparseable using" + formatter );
}
%>

Output result:
Fri Nov 11 00:00:00 CST 1222
7. interval between calculation dates

[Copy to clipboard] [-]

Code:

<% @ Page import = "Java. util. Date" %>
<% @ Page import = "Java. Text. dateformat" %>
<%
String input = "2003-05-01 ";
Simpledateformat formatter = new simpledateformat ("yyyy-mm-dd ");
Date d1 = NULL;
Try {
D1 = formatter. parse (input );
} Catch (parseexception e ){
Out. println ("unparseable using" + formatter );
}

Date D2 = new date ();

Long diff = d2.gettime ()-d1.gettime ();
Out. println ("difference is" + (diff/(1000*60*60*24) + "days .");
%>

Output result:
Difference is 29 days.
8. addition and subtraction of dates
Method: Use the add () method of the calendar class

[Copy to clipboard] [-]

Code:

<% @ Page import = "Java. util. *" %>
<% @ Page import = "Java. Text. *" %>
<%
Calendar now = calendar. getinstance ();
Simpledateformat formatter = new simpledateformat ("e YYYY. Mm. dd' at 'hh: mm: ss a Zzz ");
Out. println ("it is now" + formatter. Format (now. gettime ()));
Now. Add (calendar. day_of_year,-(365*2 ));
Out. println ("<br> ");
Out. println ("two years ago was" + formatter. Format (now. gettime ()));
%>

Output result:
It is now Friday 2003.05.30 at 01:45:32 pm CST
Two years ago was wed2001.05.30 at 01:45:32 pm CST
9. Comparison date
Method: Use the equals (), before (), and after () methods.

[Copy to clipboard] [-]

Code:

<% @ Page import = "Java. util. *" %>
<% @ Page import = "Java. Text. *" %>
<%
Dateformat df = new simpledateformat ("yyy-mm-dd ");
Date d1 = DF. parse ("2000-01-01 ");
Date D2 = DF. parse ("maid ");

String relation = NULL;
If (d1.equals (D2 ))
Relation = "the same date ";
Else if (d1.before (D2 ))
Relation = "before ";
Else
Relation = "after ";
Out. println (d1 + "is" + Relation + ''+ D2 );
%>

Output result:
Sat Jan 01 00:00:00 CST 2000 is after Fri Dec 31 00:00:00 CST 1999
10. record the time spent on one thing
Method: Call the system. gettimemillis () method twice to calculate the difference value.

[Copy to clipboard] [-]

Code:

<% @ Page import = "Java. Text. *" %>
<%
Long T0, T1;
T0 = system. currenttimemillis ();
Out. println ("Cyc starts at" + T0 );
Int K = 0;
For (INT I = 0; I <100000; I ++ ){
K + = I;
}
T1 = system. currenttimemillis ();
Out. println ("<br> ");
Out. println ("Cyc ends at" + T1 );
Out. println ("<br> ");
Out. println ("this run took" + (t1-t0) + "ms .");
%>

Output result:
Cyc starts 1054275312432
Cyc ends 1054275312442
This run took 10 ms.

Others: how to format Decimals

[Copy to clipboard] [-]

Code:

<% @ Page import = "Java. Text. *" %>
<%
Decimalformat df = new decimalformat (", ###. 00 ");
Double anumber = 33665448856.6568975;
String result = DF. Format (anumber );
Out. println (result );
%>

Output result:
33,665,448,856.66

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.