Date issues in JSP and others: D: D: D
/ **
* @Author imagebear
* /
Date issue
1. Get the current date of the server:
<% @ page import = "java.util.date"%> <% DATE mydate = new date ();%>
2, get the current year, month, day:
<% @ page import = "java.util.date"%> <% DATE mydate = new date (); int thisear = mydate.getyear () 1900; // thisyear = 2003 int thismonth = mydate.getMonth () 1; // thismonth = 5 int thisdate = mydate.getdate (); // thisdate = 30%>
3, output the current date according to the local time zone
<% @ page import = "java.util.date"%> <% Date mydate = new date (); out.println (mydate.tolocalestring ());%>
The output is:
2003-5-30
4. Get the field name "publish_time" in the database, the type of type DATETIME
<% @ page import = "java.util.date"%> <% ... connection database ... result set = rs.getdate ("public_time");%> [code] 5, Print the date according to 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 result of the output is:
IT IS Friday 2003.05.30 At 11:30:46 AM CST
(More detailed format symbols, please see the SimpleDateFormat class)
6. Convert the string to the date
<% @ page import = "java.util.date"%> <% @ page import = "java.text.dateformat"%> <% string input = "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 useless formatter); }%>
The output is:
FRI NOV 11 00:00:00 CST 12227, the interval between calculation
<% @ 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 (" unparseable using formatter);} DATE D2 = New Date () - D1.Gettime (); out.println ("DIFFERENCE IS" (Diffrence IS " (Diff / (1000 * 60 * 60 * 24)) " DAYS. ");%>
The output is:
Difference is 29 days.
8. Date's addition and subtraction operation
Method: A method of 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: s a zzz "); out.println (" it is now " formatter.format (now.gettime ())); now.add (Calendar.day_of_year, - (365) * 2)); Out.println ("
The output is:
IT is now Friday 2003.05.30 at 01:45:32 afternoon CST
Two years ago WAS Wednesday 2001.05.30 at 01:45:32 afternoon CST
9, comparative date
Method: method () method: use equals (), method () method
<% @ 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 ("1999-12-31"); string rate = null; if (D1.Equals (d2)) rate = "The Same Date As "; Else if (D1.Before"), = "before"; else reason = "after"; out.println (D1 "IS" RELATION ' D2);%> The output is:
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 both system.gettimemillis () method, seeking difference
<% @ 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 ("
"); OUT.PRINTLN ("CYC ENDS AT" T1); Out.println ("
The output is:
Cyc Starts at 1054275312432
CYC Ends at 1054275312442
THIS Run TOOK 10ms.
Others: How to format the decimal
<% @ page import = "java.text. *"%> <% decimalformat df = new decimalformat (", ###. 00"); Double Anumber = 33665448856.6568975; String Result = DF.FORMAT (anumber); OUT. Println (Result);%>
The output is:
33, 665, 448, 856.66