1. calculate the maximum number of days of calendar month. getinstance (); time. clear (); time. set (calendar. year, year); // year is inttime. set (calendar. month, I-1); // note that the default January Calendar Object is 0 int day = time. getactualmaximum (calendar. day_of_month); // number of days of the current month Note: before using the set method, you must clear it first. Otherwise, many information will inherit from the current system time. conversion of calendar and date (1) convert calendar to datecalendar Cal = calendar. getinstance (); Date = Cal. gettime (); (2) convert date to calendardate date = new date (); calenda R Cal = calendar. getinstance (); Cal. settime (date); 3. converts a string to Java. util. date Method 1: simpledateformat SDF = new simpledateformat ("yyyy-mm-dd"); Java. util. date dt = SDF. parse ("2005-2-19"); system. out. print (SDF. format (DT); // The output result is 2005-2-19. Method 2: Java. util. date dt = NULL; dateformat df = dateformat. getdateinstance (); dt = DF. parse ("2005-12-19"); system. out. println (DT); // The output result is: Mon Dec 19 00:00:00 CST 2005system. out. println (DF. Format (DT); // The output result is. converts a string to Java. SQL. the date string must be in the format of "yyyy-mm-dd"; otherwise, an illegalargumentexception exception occurs in Java. SQL. date SDT = Java. SQL. date. valueof ("2005-9-6"); system. out. println (SDT); // The output result is 2005-9-65. format the output date and time (more often used) date = new date (); simpledateformat df = new simpledateformat ("yyyy-mm-dd hh: mm: SS "); string time = DF. format (date); system. out. println (time); Note: When the simpledateformat class formats a string, you can call F as needed. Ormat () or parse () function; only format () returns string type, parse () returns Java. util. date type 6. calculate the day of the year (1). Calculate the day of the year as the day of the year. getinstance (); Cal. set (calendar. years, 2006); Cal. set (calendar. month, 8); Cal. set (calendar. day_of_month, 3); int weekno = Cal. get (calendar. week_of_year); (2) Number of the week in the calculation year: simpledateformat df = new simpledateformat ("yyyy-mm-dd"); Calendar ar Cal = calendar ar. getinstance (); Cal. set (calendar. year, 2006); Cal. set (calendar. week_of_year, 1); Cal. set (calendar. day_of_week, calendar. monday); system. out. println (DF. format (Cal. gettime (); output: 2006-01-027.add () and roll () usage (not commonly used) (1) Add () method simpledateformat df = new simpledateformat ("yyyy-mm-dd"); Calendar ar Cal = calendar ar. getinstance (); Cal. set (calendar. years, 2006); Cal. set (calendar. month, 8); Cal. set (calendar. day_of_month, 3); Cal. add (calendar. date,-4); Date = Cal. getti Me (); system. out. println (DF. format (date); Cal. add (calendar. date, 4); Date = Cal. gettime (); system. out. println (DF. format (date); output: 2006-08-30 2006-09-03 (2) Roll method Cal. set (calendar. years, 2006); Cal. set (calendar. month, 8); Cal. set (calendar. day_of_month, 3); Cal. roll (calendar. date,-4); Date = Cal. gettime (); system. out. println (DF. format (date); Cal. roll (calendar. date, 4); Date = Cal. gettime (); system. out. println (DF. for MAT (date); output: 2006-09-29 2006-09-03 visible, roll () method cycle in this month, generally use the add () method; 8. calculate the number of days between two arbitrary periods of time (this is commonly used) (1) Upload the value to the Calendar Object public int getintervaldays (calendar startday, calendar endday) {If (startday. after (endday) {calendar Cal = startday; startday = endday; endday = Cal;} Long SL = startday. gettimeinmillis (); long El = endday. gettimeinmillis (); long Ei = El-Sl; Return (INT) (Ei/(1000*60*60*24);} (2) input date object public int getin Tervaldays (date startday, date endday) {If (startday. after (endday) {date Cal = startday; startday = endday; endday = Cal;} Long SL = startday. gettime (); long El = endday. gettime (); long Ei = El-Sl; Return (INT) (Ei/(1000*60*60*24);} (3) improved accurate calculation of the number of days separated by public int getdaysbetween (calendar D1, calendar D2) {If (d1.after (D2) {Java. util. calendar swap = D1; D1 = d2; d2 = swap;} int days = d2.get (calendar. day_of_yea R)-d1.get (calendar. day_of_year); int y2 = d2.get (calendar. year); If (d1.get (calendar. Year )! = Y2) {d1 = (calendar) d1.clone (); do {days + = d1.getactualmaximum (calendar. day_f_year); // obtain the actual days of the current year d1.add (calendar ar. year, 1);} while (d1.get (calendar. year )! = Y2);} return days;} Note: The above method can be used to derive any time, if you want to find out the emails received by the mailbox within three weeks (get the current system time-and get the time three weeks ago), it is best to replace them with long to compare the received time. For example: date 1 year ago (note millisecond conversion) 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); 3 weeks ago, the date calendar cal3 = calendar. getinstance (); cal3.add (cal3.date,-21); // obtain the date string date = formatter three weeks ago. format (cal3.getti Me (); 9. string, date, and long are converted to the time type (string can be of any type, as long as it is consistent with the format in simpledateformat). When we take the time span, will substring the specific time -- long-compare 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"); long dvalue = D. gettime (); simpledateformat formatter = new simpledateformat ("yyyy-mm-dd hh: mm: SS"); string mdatet Ime1 = formatter. format (d); 10. obtain the date simpledateformat formatter2 = new simpledateformat ("yyyy-mm f e") by time, year, month, and week; Java. util. date date2 = formatter2.parse ("2003-05 Friday"); simpledateformat formatter3 = new simpledateformat ("yyyy-mm-dd"); string mydate2 = formatter3.format (date2 ); returns the day of the week mydate = myformatter. parse ("2001-1-1"); simpledateformat formatter4 = new simpledateformat ("e"); string mydate3 = formatter4.format (Mydate); 11. Java is combined with a specific database in the development of Web applications. For different database date types, we need to convert the date types in our program. If the corresponding database data is Oracle's date type, that is, only the year, month, and day are required, you can choose to use Java. SQL. date type. if it corresponds to the datetime type of the MSSQLServer database, that is, year, month, day, hour, minute, and second, select Java. SQL. for the timestamp type, you can use dateformat to define the time and date format and convert it to a string. Class datetest {* method converts the date of the string type into a timestamp (timestamp Java. SQL. timestamp) * @ Param datestring the string to be converted to timestamp * @ return datatime timestamppublic final static Java. SQL. timestamp string2time (string datestring) throws Java. text. parseexception {dateformat d Ateformat; dateformat = new simpledateformat ("yyyy-mm-dd KK: mm: Ss. SSS ", locale. english); // set the format // dateformat = new simpledateformat ("yyyy-mm-dd KK: mm: SS", locale. english); dateformat. setlenient (false); Java. util. date TimeDate = dateformat. parse (datestring); // Java of util type. SQL. timestamp datetime = new Java. SQL. timestamp (TimeDate. gettime (); // timestamp type, TimeDate. gettime () returns a long return datetime;} * Method Converts a string-type date to a date (Java. SQL. date) * @ Param datestring the string to be converted to date * @ return datatime datepublic final static Java. SQL. date string2date (string datestring) throws Java. lang. exception {dateformat; dateformat = new simpledateformat ("yyyy-mm-dd", locale. english); dateformat. setlenient (false); Java. util. date TimeDate = dateformat. parse (datestring); // Java of util type. SQL. date datetime = new Java. SQL. date (T Imedate. gettime (); // return datetime of the SQL type;} public static void main (string [] ARGs) {date da = new date (); Note: Da. gettime () is a long value system. out. println (DA. gettime (); first method to convert from date to timestamp: Use new timestamp (long) timestamp T = new timestamp (new date (). gettime (); system. out. println (t); Method 2: Use timestamp (INT year, int month, int date, int hour, int minute, int second, int nano) timestamp TT = new Timestamp (Calendar. getinstance (). get (calendar. year)-1900, calendar. getinstance (). get (calendar. month), calendar. getinstance (). get (calendar. date), calendar. getinstance (). get (calendar. hour), calendar. getinstance (). get (calendar. minute), calendar. getinstance (). get (calendar. second), 0); system. out. println (TT); try {string stodate = "2005-8-18"; // convert to Java. SQL. date string stotimestamp =": 12.123 "; // used for conversion to Java. SQL. timestamp string date date1 = string2date (stodate); timestamp date2 = string2time (stotimestamp); system. out. println ("Date:" + date1.tostring (); // The result shows system. out. println ("timestamp:" + date2.tostring (); // Result Display} catch (exception e) {e. printstacktrace () ;}}1. Date constructor 1.1 constructs a date instance datepublic date () that reflects the current time, constructs a date object, and initializes it to reflect the current time. 1.2 construct a date object from a long integer data to a date instance datepublic date (long date), and initialize it based on the number of milliseconds relative to GMT 00:00:00 on January 1, January 1, 1970. Parameter: the number of milliseconds between date and GMT 00:00:00 on January 1, January 1, 1970. 1.3 construct a date instance datepublic date (INT year, int month, int date) Public date (INT year, int month, int date, int hrs, int min) public date (INT year, int month, int date, int hrs, int min, int Sec) are not recommended for these three constructors. in JDK 1.1, are calendar. set (Year + 1900, month, date) or gregoriancalendar (Year + 1900, month, date), calendar. set (Year + 1900, month, date, hrs, min), gregoriancalendar (Year + 1900, month, date, hrs, min), CA Lendar. Set (Year + 1900, month, date, hrs, Min, Sec) or gregoriancalendar (Year + 1900, month, date, hrs, Min, Sec) instead.