Import Java. SQL. timestamp; import Java. text. dateformat; import Java. text. parseexception; import Java. text. simpledateformat; import Java. util. calendar; import Java. util. date; import Java. util. gregoriancalendar; import Java. util. locale; public class dateutils {/*** get the current time, in the format of yyyy-mm-dd hh: mm: SS * @ Param Format * @ return */public static string getcurrentdate () {return getcurrentdate ("yyyy-mm-dd hh: mm: SS ");} /*** Get the current time, custom format * @ Param Format * @ return */public static string getcurrentdate (string format) {calendar day = calendar ar. getinstance (); Day. add (calendar. date, 0); simpledateformat SDF = new simpledateformat (format); // "yyyy-mm-dd" string date = SDF. format (Day. gettime (); return date;}/*** get yesterday's time, custom format * @ Param Format * @ return */public static string getyesterdaydate (string format) {calendar day = Calendar. getinstance (); Day. add (calendar. date,-1); simpledateformat SDF = new simpledateformat (format); // "yyyy-mm-dd" string date = SDF. format (Day. gettime (); return date;}/*** @ Param date1 the time to be compared cannot be null. The correct date format is required, for example: * @ Param date2 if the time to be compared is null, it is the current time * @ Param stype. The Return Value Type 0 is the number of days, and 1 is the number of months, 2 for multiple teenagers * @ return * example: * comparedate ("2009-09-12", null, 0); // compare the day * comparedate ("2009-09-12", nu LL, 1); // compare the month * comparedate ("2009-09-12", null, 2); // compare the year */public static int comparedate (string startday, string endday, int stype) {int n = 0; // string [] U = {"day", "month", "year"}; string formatstyle = stype = 1? "Yyyy-mm": "yyyy-mm-dd"; endday = NULL? Getcurrentdate ("yyyy-mm-dd"): endday; dateformat df = new simpledateformat (formatstyle); Calendar C1 = calendar. getinstance (); Calendar C2 = calendar. getinstance (); try {c1.settime (DF. parse (startday); c2.settime (DF. parse (endday);} catch (exception E3) {system. out. println ("wrong occured");} // list = new arraylist (); While (! C1.after (C2) {// compare the results until they are equal. n is the expected result. // list. add (DF. format (c1.gettime (); // you can save the date of the interval to the array to print n ++; If (stype = 1) {c1.add (calendar. month, 1); // compare month, month + 1} else {c1.add (calendar ar. date, 1); // compare the number of days, date + 1} n = n-1; If (stype = 2) {n = (INT) N/365 ;} // system. out. println (startday + "--" + endday + "difference" + U [stype] + ":" + n); Return N ;} /*** determine whether the time conforms to the time format */public static Boolean isdate (string date, stri Ng dateformat) {If (date! = NULL) {Java. text. simpledateformat format = new Java. text. simpledateformat (dateformat); format. setlenient (false); try {format. format (format. parse (date);} catch (parseexception e) {// todo auto-generated Catch Block return false;} return true;} return false ;} /*** specify a date to determine the day of the week * Date: The format must be yyyy-mm-dd */public static string getweekday (string date) {simpledateformat SD = new simpledateformat ("yyyy-mm-dd"); simpledateformat SDW = new simpledateformat ("e"); Date d = NULL; try {d = SD. parse (date);} catch (parseexception e) {e. printstacktrace ();} return SDW. format (d);}/*** is used to globally control the number of weeks in the previous week, this week, and next week */Private Static int weeks = 0; /*** get the number of days between the current date and the current Monday */Private Static int getmondayplus () {calendar ar Cd = calendar ar. getinstance (); // obtain the day of the week, Sunday, and Tuesday ...... int dayofweek = CD. get (calendar. day_of_week); If (dayofweek = 1) {return-6;} else {return 2-dayofweek ;}} /*** get the date of Monday of this week */public static string getcurrentmonday (string format) {weeks = 0; int mondayplus = getmondayplus (); Calendar currentdate = calendar. getinstance (); currentdate. add (calendar. date, mondayplus); simpledateformat SDF = new simpledateformat (format); // "yyyy-mm-dd" string date = SDF. format (currentdate. gettime (); return date;}/*** get the date of last Monday */public static string getpreviusmonday (string format) {weeks --; int mondayplus = getmondayplus (); calendar currentdate = calendar. getinstance (); currentdate. add (gregoriancalendar. date, mondayplus + 7 * weeks); simpledateformat SDF = new simpledateformat (format); // "yyyy-mm-dd" string date = SDF. format (currentdate. gettime (); return date;}/*** get the Date of next Monday */public static string getnextmonday (string format) {weeks ++; int mondayplus = getmondayplus (); // gregoriancalendar currentdate = new gregoriancalendar (); Calendar currentdate = calendar. getinstance (); currentdate. add (gregoriancalendar. date, mondayplus + 7 * weeks); simpledateformat SDF = new simpledateformat (format); // "yyyy-mm-dd" string date = SDF. format (currentdate. gettime (); return date;}/*** get the Sunday date of the corresponding week * This method must be written in getcurrentmonday, after the getpreviusmonday or getnextmonday Method */public static string getsunday (string format) {int mondayplus = getmondayplus (); Calendar currentdate = calendar ar. getinstance (); currentdate. add (calendar. date, mondayplus + 7 * weeks + 6); simpledateformat SDF = new simpledateformat (format); // "yyyy-mm-dd" string date = SDF. format (currentdate. gettime (); return date;}/*** method converts a string-type date to a timestamp (timestamp Java. SQL. timestamp) * @ Param datestring the string to be converted to timestamp * @ return datatime timestamp */public static timestamp string2time (string datestring) {dateformat; dateformat = new simpledateformat ("yyyy-mm-dd hh: mm: SS", locale. english); // set the format dateformat. setlenient (false); Java. util. date = NULL; try {date = dateformat. parse (datestring);} catch (parseexception e) {// todo auto-generated Catch Block E. printstacktrace ();} // Java. SQL. timestamp datetime = new Java. SQL. timestamp (date. gettime (); return New timestamp (date. gettime (); // timestamp type, TimeDate. gettime () returns a long type}/*** method to convert a string-type date into a date (Java. SQL. date) ** @ Param datestring * the string to be converted to date * @ return datatime date */public final static Java. SQL. date string2date (string datestring) {dateformat; dateformat = new simpledateformat ("yyyy-mm-dd", locale. english); dateformat. setlenient (false); Java. util. date = NULL; try {date = dateformat. parse (datestring);} catch (parseexception e) {// todo auto-generated Catch Block E. printstacktrace ();} // Java. SQL. date datetime = new Java. SQL. date (date. gettime (); // return New Java of the SQL type. SQL. date (date. gettime ());}}