Java About the date of calculation of the date

xiaoxiao2021-03-06  110

http://www.blueidea.com/bbsdetail.asp?id=989464 http://www.java-cn.com/bbs-jsp/show.jsp?id=133651&Forum=base

Date issues in JSP and others: D: D: D: D / *** @Author ImageBear * / Date Question 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 result is: 2003-5-30 4. Get the field name "public_time" in the database, 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 symbol, please refer to 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 1222 7, 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: DIFCERENCE IS 29 Days. 8, Date Add or subtraction Operation Method: Use the Calendar class Add () method

<% @ 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, comparison Date Method: Equals (), before (), After () 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"; else reason = "after"; out.println (D1 "IS" rate ' d2);%> The output is: SAT Jan 01 00:00:00 CST 2000 IS AFTER FRI DEC 31 00:00:00 CST 1999 10

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

转载请注明原文地址:https://www.9cbs.com/read-99073.html

New Post(0)