Java date Processing

Source: Internet
Author: User
Tags dateformat

Java date Processing

1. Get the current server Date:

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

 

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

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

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

Output result:
2003-5-30

4. Obtain the value of "publish_time" and datetime type in the database.

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

<% @ 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. Between calculation dates

Output result:
Difference is 29 days.

8. addition and subtraction of dates
Method: Use the add () method of the calendar class

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

  1. <% @ Page import = "Java. util. *" %>
  2. <% @ Page import = "Java. Text. *" %>
  3. <%
  4. Dateformat df = new simpledateformat ("yyy-mm-dd ");
  5. Date d1 = DF. parse ("2000-01-01 ");
  6. Date D2 = DF. parse ("maid ");
  7. String relation = NULL;
  8. If (d1.equals (D2 ))
  9. Relation = "the same date ";
  10. Else if (d1.before (D2 ))
  11. Relation = "before ";
  12. Else
  13. Relation = "after ";
  14. Out. println (d1 + "is" + Relation + ''+ D2 );
  15. %>

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.

  1. <% @ Page import = "Java. Text. *" %>
  2. <%
  3. Long T0, T1;
  4. T0 = system. currenttimemillis ();
  5. Out. println ("Cyc starts at" + T0 );
  6. Int K = 0;
  7. For (INT I = 0; I <100000; I ++)
  8. T1 = system. currenttimemillis ();
  9. Out. println ("<br> ");
  10. Out. println ("Cyc ends at" + T1 );
  11. Out. println ("<br> ");
  12. Out. println ("this run took" + (t1-t0) + "ms .");
  13. %>

Output result:

  1. Cyc starts 1054275312432
  2. Cyc ends 1054275312442
  3. This run took 10 ms.
  4. Others: how to format Decimals
  5. <% @ Page import = "Java. Text. *" %>
  6. <%
  7. Decimalformat df = new decimalformat (", ###. 00 ");
  8. Double anumber = 33665448856.6568975;
  9. String result = DF. Format (anumber );
  10. Out. println (result );
  11. %>

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.