Import Java. io. printstream; import Java. SQL. time; import Java. SQL. timestamp; import Java. text. dateformat; import Java. text. parseposition; import Java. text. simpledateformat; import Java. util. calendar; public class dateutil {public static final string c_time_patton_default = "yyyy-mm-dd hh: mm: SS"; public static final string c_date_patton_default = "yyyy-mm-dd "; public static final int c_one_second = 1000; P Ublic static final int c_one_minute = 60000; public static final long c_one_hour = 3600000l; public static final long c_one_day = 86400000l; Public String getmonthbegin (string strdate) {Java. util. date = parsedate (strdate); Return formatdatebyformat (date, "yyyy-mm") + "-01";} Public String getmonthend (string strdate) {Java. util. date = parsedate (getmonthbegin (strdate); Calendar calenda R = calendar. getinstance (); Calendar. settime (date); Calendar. add (2, 1); Calendar. add (6,-1); Return formatdate (calendar. gettime ();} Public String formatdate (Java. util. date) {return formatdatebyformat (date, "yyyy-mm-dd");} Public String formatdatebyformat (Java. util. date, string format) {string result = ""; if (date! = NULL) {try {simpledateformat SDF = new simpledateformat (format); Result = SDF. format (date);} catch (exception ex) {ex. printstacktrace () ;}} return result;} public static Java. util. date parsedate (string datestr, string format) {Java. util. date = NULL; try {dateformat df = new simpledateformat (format); string dt = datestr; If ((! DT. equals ("") & (DT. length () <format. length () {dt = DT + format. substring (DT. length ()). replaceall ("[yymmddhhss]", "0");} date = DF. parse (DT);} catch (exception localexception) {} return date;} public static Java. util. date parsedate (string datestr) {return parsedate (datestr, "yyyy-mm-dd");} public static Java. util. date parsedate (Java. SQL. date) {return date;} public static Java. SQL. Date partsesqldate (Java. util. Date) {If (date! = NULL) {return New Java. SQL. date (date. gettime ();} return NULL;} public static Java. SQL. date parsesqldate (string datestr, string format) {Java. util. date = parsedate (datestr, format); Return parsesqldate (date);} public static Java. SQL. date parsesqldate (string datestr) {return parsesqldate (datestr, "yyyy-mm-dd");} public static timestamp parsetimestamp (string datestr, string format) {ja Va. util. Date = parsedate (datestr, format); If (date! = NULL) {long T = date. gettime (); return New timestamp (t);} return NULL;} public static timestamp parsetimestamp (string datestr) {return parsetimestamp (datestr, "yyyy-mm-dd hh: mm: SS ");} public static string format (Java. util. date, string format) {string result = ""; try {If (date! = NULL) {dateformat df = new simpledateformat (format); Result = DF. format (date) ;}} catch (exception localexception) {} return result;} public static string format (Java. util. date) {return format (date, "yyyy-mm-dd");} public static int getyear (Java. util. date) {calendar c = calendar. getinstance (); C. settime (date); Return C. get (1);} public static int getmonth (Java. util. date) {calendar c = calendar. getinstance (); C. settime (date); Return C. get (2) + 1;} public static int getday (Java. util. date) {calendar c = calendar. getinstance (); C. settime (date); Return C. get (5);} public static int gethour (Java. util. date) {calendar c = calendar. getinstance (); C. settime (date); Return C. get (11);} public static int getminute (Java. util. date) {calendar c = calendar. getinstance (); C. settime (date); Return C. get (12);} public static int getsecond (Java. util. date) {calendar c = calendar. getinstance (); C. settime (date); Return C. get (13);} public static long getmillis (Java. util. date) {calendar c = calendar. getinstance (); C. settime (date); Return C. gettimeinmillis ();} public static long getmillis () {return system. currenttimemillis ();} public static string getdate (Java. util. date) {return format (date, "yyyy-mm-dd");} public static string gettimestr (Java. util. date) {return format (date, "HH: mm: SS");} public static string getdatetime (Java. util. date) {return format (date, "yyyy-mm-dd hh: mm: SS");} public static Java. util. date adddate (Java. util. date, int day) {calendar c = calendar. getinstance (); C. settimeinmillis (getmillis (date) + day * 24l * 3600l * 1000l); Return C. gettime ();} public static int diffdate (Java. util. date, Java. util. date date1) {return (INT) (getmillis (date)-getmillis (date1)/86400000l);} public static Java. util. date getdatetime (string datetime) {simpledateformat dt = new simpledateformat ("yyyy-mm-dd hh: mm: SS"); If (datetime. length () <11) {datetime = datetime + "12:00:00";} Try {Java. util. date udate = DT. parse (datetime); Return udate;} catch (exception e) {e. printstacktrace ();} return NULL;} public static Java. util. date getenddatetime () {string date = "2039-12-31 12:00:00"; return getdatetime (date);} public static Java. util. date getbegindatetime () {string date = "12:00:00"; return getdatetime (date);} public static timestamp getnowtimestamp () {return New timestamp (New Java. util. date (). gettime ();} public static timestamp gettimestamp (Java. util. date) {return New timestamp (date. gettime ();} public static time gettime (Java. util. date) {return New Time (date. gettime ();} public static time getnowtime () {return New Time (New Java. util. date (). gettime ();} public static Java. util. date getnowdate () {return New Java. util. date ();} public static string getnowdatestr () {string format = "yyyy-mm-dd hh: mm: SS"; simpledateformat SDF = new simpledateformat (format); Return SDF. format (New Java. util. date ();} public static string getdatestr (Java. util. date) {string format = "yyyy-mm-dd hh: mm: SS"; simpledateformat SDF = new simpledateformat (format); Return SDF. format (date);} public static int findmaxdayinmonth () {return findmaxdayinmonth (0, 0);} public static int findmaxdayinmonth (Java. util. date) {If (date = NULL) {return 0;} return findmaxdayinmonth (date2calendar (date);} public static int findmaxdayinmonth (calendar) {If (calendar = NULL) {return 0;} return calendar. getactualmaximum (5);} public static int findmaxdayinmonth (INT month) {return findmaxdayinmonth (0, month);} public static int findmaxdayinmonth (INT year, int month) {calendar = calendar. getinstance (); If (Year> 0) {calendar. set (1, year);} If (month> 0) {calendar. set (2, month-1);} return findmaxdayinmonth (calendar);} public static Java. util. date calendar2date (calendar) {If (calendar = NULL) {return NULL;} return calendar. gettime ();} public static calendar date2calendar (Java. util. date) {If (date = NULL) {return NULL;} calendar = calendar. getinstance (); Calendar. settime (date); Return calendar;} public static simpledateformat getsimpledateformat () {return getsimpledateformat (null);} public static simpledateformat getsimpledateformat (string format) {simpledateformat SDF; if (format = NULL) SDF = new simpledateformat ("yyyy-mm-dd hh: mm: SS"); else {SDF = new simpledateformat (format );} return SDF;} public static string formatdate2str () {return formatdate2str (New Java. util. date ();} public static string formatdate2str (Java. util. date) {return formatdate2str (date, "yyyy-mm-dd hh: mm: SS");} public static string formatdate2str (Java. util. date, string format) {If (date = NULL) {return NULL;} If (format = NULL) | (format. equals ("") {format = "yyyy-mm-dd hh: mm: SS";} simpledateformat SDF = getsimpledateformat (format); Return SDF. format (date);} public static Java. util. date formatstr2date (string datestr) {return formatstr2date (datestr, null);} public static Java. util. date formatstr2date (string datestr, string format) {If (datestr = NULL) {return NULL;} If (format = NULL) | (format. equals ("") {format = "yyyy-mm-dd hh: mm: SS";} simpledateformat SDF = getsimpledateformat (format); Return SDF. parse (datestr, new parseposition (0);} public static string formatdefault2define (string datestr, string defineformat) {return formatsource2target (datestr, "yyyy-mm-dd hh: mm: SS ", defineformat);} public static string formatsource2target (string datestr, string sourceformat, string targetformat) {Java. util. date = formatstr2date (datestr, sourceformat); Return formatdate2str (date, targetformat);} public static int findweeksnoinyear () {return findweeksnoinyear (New Java. util. date ();} public static int findweeksnoinyear (Java. util. date) {If (date = NULL) {return 0;} return findweeksnoinyear (date2calendar (date);} public static int findweeksnoinyear (calendar) {If (calendar = NULL) {return 0;} return calendar. get (3);} public static Java. util. date finddateinweekonyear (INT year, int weekinyear, int dayinweek) {calendar ar calendar = calendar ar. getinstance (); If (Year> 0) {calendar. set (1, year);} calendar. set (3, weekinyear); Calendar. set (7, dayinweek); Return calendar. gettime ();} public static Java. util. date daybefore2date (INT days) {Java. util. date = new Java. util. date (); Return (Java. util. date) daybefore2object (days, null, date);} public static string daybefore2str (INT days) {string = formatdate2str (); Return (string) daybefore2object (days, null, string);} public static <t> T daybefore2object (INT days, string format, t instance) {calendar ar calendar = calendar ar. getinstance (); If (days = 0) {return NULL;} If (format = NULL) | (format. equals ("") {format = "yyyy-mm-dd hh: mm: SS";} calendar. add (5, days); If (instance instanceof Java. util. date) return calendar. gettime (); If (instance instanceof string) {return formatdate2str (calendar2date (calendar), format);} return NULL;} public static Java. util. date definedaybefore2date (Java. util. date, int days) {Java. util. date dateinstance = new Java. util. date (); Return (Java. util. date) definedaybefore2object (date, days, null, dateinstance);} public static string definedaybefore2str (Java. util. date, int days) {string stringinstance = formatdate2str (); Return (string) definedaybefore2object (date, days, null, stringinstance);} public static <t> T definedaybefore2object (Java. util. date, int days, string format, t instance) {If (date = NULL) | (days = 0) {return NULL ;} if (format = NULL) | (format. equals ("") {format = "yyyy-mm-dd hh: mm: SS";} calendar = date2calendar (date); Calendar. add (5, days); If (instance instanceof Java. util. date) return calendar. gettime (); If (instance instanceof string) {return formatdate2str (calendar2date (calendar), format);} return NULL;} public static Java. util. date monthbefore2date (INT months) {Java. util. date = new Java. util. date (); Return (Java. util. date) monthbefore2object (Months, null, date);} public static string monthbefore2str (INT months) {string = formatdate2str (); Return (string) monthbefore2object (Months, null, string) ;}public static <t> T monthbefore2object (INT months, string format, t instance) {If (months = 0) {return NULL ;} if (format = NULL) | (format. equals ("") {format = "yyyy-mm-dd hh: mm: SS";} calendar = calendar ar. getinstance (); Calendar. add (2, months); If (instance instanceof Java. util. date) return calendar. gettime (); If (instance instanceof string) {return formatdate2str (calendar2date (calendar), format);} return NULL;} public static Java. util. date definemonthbefore2date (Java. util. date, int months) {Java. util. date dateinstance = new Java. util. date (); Return (Java. util. date) definemonthbefore2object (date, months, null, dateinstance);} public static string definemonthbefore2str (Java. util. date, int months) {string stringinstance = formatdate2str (); Return (string) definemonthbefore2object (date, months, null, stringinstance);} public static <t> T definemonthbefore2object (Java. util. date, int months, string format, t instance) {If (months = 0) {return NULL;} If (format = NULL) | (format. equals ("") {format = "yyyy-mm-dd hh: mm: SS";} calendar = date2calendar (date); Calendar. add (2, months); If (instance instanceof Java. util. date) return calendar. gettime (); If (instance instanceof string) {return formatdate2str (calendar2date (calendar), format);} return NULL;} public static int caculate2days (Java. util. date firstdate, Java. util. date seconddate) {calendar = calendar. getinstance (); Calendar. settime (firstdate); int daynum1 = calendar. get (6); Calendar. settime (seconddate); int daynum2 = calendar. get (6); Return math. ABS (daynum1-daynum2);} public static int caculate2days (calendar firstcalendar, calendar secondcalendar) {If (firstcalendar. after (secondcalendar) {calendar = firstcalendar; firstcalendar = secondcalendar; secondcalendar = calendar;} Long calendarnum1 = firstcalendar. gettimeinmillis (); long calendarnum2 = secondcalendar. gettimeinmillis (); Return math. ABS (INT) (calendarnum1-calendarnum2)/86400000l);} public static Java. util. date getdefaultdate () {Java. util. date = NULL; simpledateformat dateformat = new simpledateformat ("yyyy-mm-dd"); try {date = dateformat. parse ("1900-01-01");} catch (exception localexception) {} return date;} public static Java. util. date getdatebyformat (string datestring, string formatstring) {If (datestring = NULL) | (datestring. trim (). equals ("") {return NULL;} If (formatstring = NULL) {formatstring = "yyyy-mm-dd";} simpledateformat dateformat = new simpledateformat (formatstring ); try {return dateformat. parse (datestring);} catch (exception localexception) {} return NULL;} public static string getdatetimestring (Java. util. date, string formatstring) {If (formatstring = NULL) {formatstring = "yyyy-mm-dd hh: mm: SS" ;}simpledateformat dateformat = new simpledateformat (formatstring ); string STR = dateformat. format (date); If (Str. indexof ("00:00:00")>-1) {STR = Str. substring (0, 10);} return STR;} public static string getdatetimestring (Java. util. date) {return getdatetimestring (date, null);} public static string getdatestring (Java. util. date, string formatstring) {If (formatstring = NULL) {formatstring = "yyyy-mm-dd";} simpledateformat dateformat = new simpledateformat (formatstring); Return dateformat. format (date);} public static string getdatestring (Java. util. date) {return getdatetimestring (date, null);} public static string getnowdate (string formatstring) {Java. util. date = new Java. util. date (); If (formatstring = NULL) {formatstring = "yyyy-mm-dd hh: mm: SS" ;}simpledateformat dateformat = new simpledateformat (formatstring); Return dateformat. format (date);} public static string getformatetime (Java. util. date begintime, Java. util. date endtime) throws exception {long between = endtime. gettime ()-begintime. gettime (); long hour = between/1000l/3600l; long minute = between/1000l % 3600l/60l; long second = between/1000l % 3600l % 60l; if (hour <0l) | (minute <0l) | (second <0l) {Throw new exception ("the start time is later than the end time ");} stringbuffer active = new stringbuffer (). append (hour ). append (":"); If (minute <10l) active. append ("0 "). append (minute ). append (":"); else {active. append (minute ). append (":");} If (second <10l) active. append ("0 "). append (second); else {active. append (second);} return active. tostring ();} public static string getformatetemebylong (long between) {long hour = between/3600l; long minute = between % 3600l/60l; long second = between % 3600l % 60l; stringbuffer active = new stringbuffer (). append (hour ). append (":"); If (minute <10l) active. append ("0 "). append (minute ). append (":"); else {active. append (minute ). append (":");} If (second <10l) active. append ("0 "). append (second); else {active. append (second);} return active. tostring ();} public static void main (string [] ARGs) {calendar firstc = date2calendar (formatstr2date ("", "yyyy-mm-dd ")); calendar secondc = date2calendar (formatstr2date ("2009-07-18", "yyyy-mm-dd"); system. out. println (caculate2days (firstc, secondc ));}}