Time operation in Java
I often see the issue of someone in the JSP version, which generally includes: take the current time, transform a specified string time into time type, find the number of days between two times, seeking a period of time before, seeking time Summary of these issues after a period of time.
<% @ Page ContentType = "text / html; charset = GB2312"%>
<% @ Page Import = "Java.Text. *"%>
<% @ Page Import = "java.util. *"%>
<%
// String transform into time type (string can be any type, as long as the format in SimpleDateFormat can be consistent)
Java.text.SIMPLEDATEFORMAT SDF = New Java.Text.SIMPLEDATEFORMAT ("M / DD / YYYY HH: MM: SS A", Java.util.Locale.us);
Java.util.date d = sdf.parse ("5/13/2003 10:31:37 AM");
Out.println (D);
Out.println ("
");
SimpleDateFormat Formatter = New SimpleDateFormat ("YYYY-MM-DD HH: MM: SS");
String mdatetime1 = formatter.format (d);
Out.println (MDATETIME1);
Out.println ("
");
Out.println (D.getTime ());
Out.println ("
");
//current time
Calendar Cal = Calendar.getInstance ();
// SimpleDateFormat Formatter = New SimpleDateFormat ("YYYY-MM-DD HH: MM: SS");
SimpleDateFormat Formatter = New SimpleDateFormat ("YYYY-MM-DD HH: MM: SS G E D f W W A E f");
String mdatetime = formatter.format (caver.gettime ());
Out.println (MDATETIME);
Out.println ("
");
// 1 year ago
Java.util.date mydate = new java.util.date ();
Long myTime = (mydate.gettime () / 1000) -60 * 60 * 24 * 365;
mydate.settime (MyTime * 1000);
String mdate = formatter.format (mydate);
Out.println (MDATE);
Out.println ("
");
// Tomorrow
mydate = new java.util.date ();
MyTime = (mydate.gettime () / 1000) 60 * 60 * 24;
mydate.settime (MyTime * 1000);
MDATE = formatter.format (mydate);
Out.println (MDATE);
Out.println ("
"); // The number of days between two times
SimpleDateFormat MyFormatter = New SimpleDateFormat ("YYYY-MM-DD");
Java.util.date date = myformatter.parse ("2003-05-1");
Java.util.date mydate = myFormatter.Parse ("1899-12-30");
Long day = (Date.getTime () - mydate.gettime ()) / (24 * 60 * 60 * 1000);
Out.println (DAY);
Out.println ("
");
// Half an hour
SimpleDateFormat Format = New SimpleDateFormat ("YYYY-MM-DD HH: MM: SS");
Java.util.date Date1 = format.parse ("2002-02-28 23:16:00);
Long Time = (Date1.gettime () / 1000) 60 * 30;
Date1.settime (Time * 1000);
String mydate1 = formatter.format (date1);
Out.println (MyDate1);
Out.println ("
");
// Annual monthly review date
SimpleDateFormat Formatter2 = New SimpleDateFormat ("YYYY-MM F E");
Java.util.date Date2 = formatter2.parse ("2003-05 5 Friday");
SimpleDateFormat Formatter3 = New SimpleDateFormat ("YYYY-MM-DD");
String mydate2 = formatter3.Format (date2);
Out.println (mydate2);
Out.println ("
");
// Seeking a week
mydate = myformatter.parse ("2001-1-1");
SimpleDateFormat Formatter4 = New SimpleDateFormat ("E");
String mydate3 = formatter4.format (mydate);
Out.println (MyDate3);
Out.println ("
");
%>