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