This article introduces the date related operations during the Java development process. The shared code is as follows:
Package JSE; Import Java.io.unsupportedEdEndException; Import Java.Text.parsexception; Import Java.Text.SimpleDateFormat; Import java.util. Calendar; Import java.util.date; Import java.util.locale; /*** commonly used Date Operation * * * @Author Puck * Createdate 2015-07-30 22:54:38 */Public Class Testdate {Public Static Void Main (String [] ARGS) Throws PARSEEEEXCETION, Unsupport EdencodingException {Calendar Cal = Calendar.getInstance ();/ / Cal.add (Calendar.Day_of_month, -48); System.out.println (DateTostring (CAL));} / ** * Date Formats * @Param date * @Return * / Public Stration Tostring (date date) {// SimpleDateFormat format = new SimpleDateFormat("y年MM月dd日E HH时mm分ss秒", Locale.CHINA);// SimpleDateFormat format = new SimpleDateFormat("y年M月d日EH时m分s "Second", local.china); SimpleDateFormat SDF = New SimpleDateFormat ("Yyyy-MM-DD HH: SS", Locale.china); // Example RETURN SDF.FORMAT (Date);} /*** date Format @Param date * @Return * / Public Static String DateTostring (Calendar Cal) {Return Datetostring (Cal.Gettime ())); am date * format: 2015-06-16 Date * @Return Calendar * @throws PARSEEXCEPTION */ Public Static Calendar DatestringOcalendar (String Datestr) Thrown Parseexception {// Calendar Caln dar.getInstance (); // Cal.clear (); // Cal.set (integer.parseint (Date.substring (0, 4)), Integer.PARSEINT (date.Substring (5, 7)) -1, // Integer.parseint (date.substring (8, 10))); // Return Cal; SimpleDateFormat SDF = New SimpleDateFormat ("Yyyy-Mm-DD HH: MM: SS"); date date = sdf.parse (datestr); Calendar Cal = Calendar.getInstance (); Cal.Settime (date); CAL;} / ** * Datestring to date * * @param date * format: yyyy-mm-dd HH: mm: ss date * @Return carer * @throws parseexception */ PUBLIC Static DateStrowtodate (String Date) Throws PARSEEEXCEPTION {SimpleDateFormat SDF = New SimpleDateFormat ("Yyyy-MM-DD HH: MM: SS"); Return SDF.PARSE (Date);} / ** * Date Convert to Calendar * * @param date * @Return * / Public Staticar DateTocalendar (date date ) {Calendar C1 = Calendar.getInstance (); C1.Settime (Date); Return C1;} / ** * Calendar convert to date * @param Cal * @Return * / Public Static Date dartodate (Calendar Cal) {Return Cal .gettime ();} / ** * Calculate the difference between the two dates. CALCULEDIFFERDAY (date d1, date d2) {Calendar C1 = Calendar.getInstance (); C1.Settime (D1); Calendar C2 = Calendar.getInstance (D2); Urn CalculatedifferDay (C1, C2);} / ** * Calculate the difference between the two dates. 1. Calendar C2) {int [] p1 = {c1.get (calendar.year), C1.Get (Calendar.month), C1.get (Calendar.day_of_month)}; int [] p2 = {CALENDAR.YEAR. ), C2.get (Calendar.month), C2.get (Calendar.day_of_month)}; System.out.println ("P1 [0] =" + P1 [0] + "P1 [1] =" + P1 [ 1] + "p1 [2] =" + p1 [2]); System.out.println ("p2 [0] =" + p2 [0] + "p2 [1] =" + p2 [1] + " p2 [2] = " + p2 [2]); int Year = P2 [0] -p1 [0]; int Month = (p2 [0] * 12) + p2 [1] - (P1 [0] * 12) + p1 [1]); int days = (int) ((C2.Gettimeinmillis () -C1.Gettimeinmillis ()) / (24 * 60 * 60 * 1000)); Return New Int [] {Year, Month Month , Day};}/ ** * On the first day of the date of the acquisition date * * * * @param C * @Return */ Public Static Calendar GetlastDayofweek (Calendar C) {// SimpleDateFormat2 = New SimpleDate Format ("Y Year Moon D D When EH is divided into S seconds ", local.china); // System.out.println (" current time: " + format2.Format (c.Gettime ())); Monday); // System.out.println ("Monday time:" + format2.format (c.gettime ())); Return C;}/** * Date * @param c * @param day * @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ Return* / Public Static Calendar AddordEcreaseday (Calendar C, Int Day) {CALENDAR.DAY_OF_MONTH, Day); Return C;} / *** Last day of the month* @param * @param Month * @Return * / Public Static Int GetlastDayofmonth (int Year, int Month) {Calendar C = Calendar.getInstance (); C.Set (year, month -1, 1); CALENDAR.DAY_OF_MONTH);} /*** Get the last day of the month * @Param Cal * @Return */ Public Static Int GetlastDayofmonth (Calendar Cal) {Return Cal.getActualMaxumum (Calendar.Day_MONTH);}}