/ * * Create Date 2004-11-3 * * Todo To change the template for this generated file, go to * Window - Preference - Java - Code - Code Template * / package com.pub.date;
Importa.util.calendar; import java.util.date; import java.util.arraylist; import java.text.SIMPLEDATEFORMAT; import com.pub.str.stringfunction;
/ ** * @Author Explorerwen * * Todo To change the template of this generated type annotation, go to the window - Preference - Java - Code style - Code Template * / Public Class DateFormat Implements Comparable {// year protected string year;
/ Month protected string month;
// day protected string day;
// hour protected string hour;
// minute protected string minute;
// second protected string second;
/ ** * Constructs a DateFormat, the value is the current system date / time * / public DateFormat () {Calendar Date = Calendar.GetInstance (); year = string.valueof (Date.Get (Calendar.Year); Month = String .valueof (Date.Get (Calendar.Mont) 1); day = string.valueof (Date.Get (Calendar.date));
Year = StringFunction.Updatezero (Year, 4); Month = StringFunction.Updatezero (Month, 2); day = stringfunction.Updatezero (day, 2);
Hour = string.valueof (Date. Hour_Of_DAY); minute = string.valueof (Date.Get (Calendar.minute)); second = string.valueof (Date.Get (Calendar.Second);
Hour = StringFunction.Updatezero (Hour, 2); minute = stringfunction.Updatezero (Minute, 2); second = stringfunction.Updatezero (Second, 2);}
/ ** * constructed a DateFormat according to the specified date, if the specified date is not legal, throw an error message * @Param _date * Specify the date * / public DateFormat (String _date) {this (_date, false);}
public DateFormat (String _date, boolean _timeFlag) {if (_date == null) {throw new NullPointerException ();} String inputDate; if (_timeFlag) {inputDate = StringFunction.getDateTimeFormat (_date); if (checkDateTime (inputDate)!) Throw new error ("Date is not legal");} else {INPUTDATE = StringFunction.getdateFormat (_date); if (! checkdate (inputdate)) throw new error ("Date is not legal");} year = inputdate.substring (0 , 4); Month = INPUTDATE.SUBSTRING (4, 6); day = INPUTDATE.SUBSTRING (6, 8); if (_timeflag) {hours = Inputdate.Substring (8, 10); Minute = INPUTDATE.SUBSTRING (10, 12); Second = INPUTDATE.SUBSTRING (12, 14);} else {hour = "00"; minute = "00"; second = "00";}}
/ ** * constructed a DateFormat according to the specified date and time, if the specified date and time are not legal, throw an error message * * @Param _date * specified date * @Param _ho * Specify hours * @Param _mi * Specified minutes * @Param _se * Specify second * / public DateFormat (string _date, string _ho, string _mi, string _se) {this (); if (! Checkdate (_date)) throw new nullpointRexception ("Date is not "Legal"); if (! Checktime (_ho, _mi, _se)) throw new nullpointerException ("time is not legal"); year = _date.substring (0, 4); Month = _date.substring (4, 6); day = _date.substring (6, 8); hour = _ho; minute = _mi; second = _se;}
Public DateFormat (Date D) {Calendar Date = Calendar.GetInstance (); Date.SetTime (D); Year = String.Valueof (Date.Get (Calendar.Year)); Month = String.Valueof (Date.Get (Calendar .Month) 1); day = string.valueof (date.get (calendar.date));
year = StringFunction.updateZero (year, 4); month = StringFunction.updateZero (month, 2); day = StringFunction.updateZero (day, 2); hour = String.valueOf (date.get (Calendar.HOUR_OF_DAY)); minute = String.Valueof (Date.Get (Calendar.minute)); second = string.valueof (Date.Get (Calendar.Second));
Hour = StringFunction.Updatezero (Hour, 2); minute = stringfunction.Updatezero (Minute, 2); second = stringfunction.Updatezero (Second, 2);}
/ / -------------------------------------------------------------------------------------------- ----------------------------- / ** * Get the current system date / time * * @return current system date / time * / Public synchronized static dateformat getcurrentdate () {return new dateformat ();
/ / -------------------------------------------------------------------------------------------- ----------------------------- / ** * Set date, if the set date is not legal, throw a Error message * * @Param _date * Set the date value * / public void setdate (String _date) {if (! Checkdate (_date)) throw new nullpointReption ("Date is not legal"); Year = _date.substring (0, 4); Month = _date.substring (4, 6); day = _date.substring (6, 8);}
/ ** * Set date, if the set date is not legal, throw an error message * * @Param _yyyy * @Param _mm * month * @param _dd * day * / public void setdate (int _yyyy, int _mm, int _dd) {String yyyy = StringFunction.updateZero (String.valueOf (_yyyy), 4); String mm = StringFunction.updateZero (String.valueOf (_mm), 2); String dd = StringFunction.updateZero (String. Valueof (_dd), 2); SetDate (YYYY MM DD);
/ ** * Set time, if the set time is not legal, throw an error message * * @Param _ho * hu * @Param _Mi * minutes * @Param _se * second * / public void settime (string _ho String_mi, string _se) {if (! Checktime (_ho, _mi, _se)) Return; hour = _ho; minute = _mi; second = _se;} // ----------- -------------------------------------------------- ------------- // Remove the value of the member variable / take out the value of the member variable in the way the output of the member variables will be removed.
Public String getMonth () {return month;}
PUBLIC STRING GETDAY () {Return Day;}
Public string getMonthdate () {return year month;}
Public string getdate () {return year month day;}
Public string gethour () {return hour;}
Public string getminute () {return minute;}
Public string getsecond () {return second;}
Public string gettime () {Return Hour minute second;
Public string getprintmonth () {return year "-" month;
Public string getprintdate () {return year "-" MONTH "-" day;
Public String getprintTime () {return hour ":" minute ":" second;
Public string getprintdatetime () {return getprintdate () " getprinttime ();
Public string getsqldate () {return "to_date ('" getdate () "', 'yyyymmdd')
/ / -------------------------------------------------------------------------------------------- ------------------------------ / ** * Check the date if the date is legal * @Param Date _Date * Need to check the date * @ If the date was legitimate return true, otherwise false * / public synchronized static boolean checkDate (date _date) {SimpleDateFormat dtFmt = new SimpleDateFormat ( "yyyyMMdd"); String date = dtFmt.format (_date); boolean flag = checkDate (date ); Returnif;} // ------------------------------------------ -------------------------------------- / ** * Check the date legitimate * @param _date * The date you need to check * @return If the date is legal, true, otherwise false * / public synchronized static boolean checkdate (string _date) {if (_date == null) {Return false;} if (_date.length () < 8) Return False; string yy = _date.substring (0, 4); string mm = _date.substring (4, 6); string dd = _date.substring (6, 8); try {int = integer.parseint yy); int imm = integer.parseint (mm); int IDd = integer.parseint (DD);
IF (IYY> 2050 || IYY <1950) {RETURN FALSE;} IF (IMM <1 || IMM> 12) Return False; int imaxmonthday = integer.parseint (GetMaxMontHDay (YY, MM)); if (IDD <1 || IDD> imaxmonthday) Return False; Return true;} catch (Exception E) {system.err.println (E.GetMessage ()); return false;}} / ** * Check date legitimate * @Param Date _date * * @return dates to check if the date was legally true, otherwise false * / public synchronized static boolean checkDateTime (date _date) {SimpleDateFormat dtFmt = new SimpleDateFormat ( "yyyyMMddHHmmss"); String date = dtFmt.format (_date) System.out.Println (Date); Boolean Flag = CheckdateTime (Date); Return flag;} / ** * Check the date * * @Param _date * Need to check the date * @return if the date is True, Otherwise, false * / public synchronized static boolean checkdatetime (string _date) {r (_date == null) {Return False;}} (_date.Length () <14) Return False; string yy = _date.substring (0, 4 ); String mm = _date.substring (4, 6); string dd = _date.substring (6, 8); Strin g ho = _date.substring (8, 10); string mi = _date.substring (10, 12); string se = _date.substring (12, 14); try {int = integer.parseint (yy); int IMM = INTEGER.PARSEINT (mm); int IDd = integer.parseint (DD);
IF (IYY> 2050 || IYY <1950) {RETURN FALSE;} IF (IMM <1 || IMM> 12) Return False; int imaxmonthday = integer.parseint (GetMaxMontHDay (YY, MM)); if (IDD <1 || IDD> iMaxMontHDAY) RETURN FALSE; int = integer.Parseint (HO); int = integer.parseint (mi); int = integer.parseint (se);
IF (Iho <0 || IHO> 24) Return False; IF (IMI <0 || IMI> 60) Return False; IF (ISE <0 | ISE> 60) Return False; Return True;} catch (Exception E ) {System.err.println (E.GetMessage ()); return false;}} / ** * Check time is legal * * @Param _ho * hour * @param _mi * minutes * @Param _se * second * @ Return if true, otherwise false * / public static synchronized boolean checktime (string _ho, string _mi, string _se) {Try {Int h = integer.parseint (_ho); int = integer.parseint (_MI); INT ISE = INTEGER.PARSEINT (_SE);
IF (Iho <0 || IHO> 24) Return False; IF (IMI <0 || IMI> 60) Return False; IF (ISE <0 | ISE> 60) Return False; Return True;} catch (Exception E ) {System.err.println (E.getMessage ()); return false;}}
/ / -------------------------------------------------------------------------------------------- ----------------------------- // / ** * Get the maximum number of days in the month * * @Param Yy * year * @Param mm * month * @return's maximum number of days in this month * / public static synchronized string getMaxMontHDAY (String Yy, String mm) {
Calendar cal = calendar.getInstance (); try {int y = integer.parseint (yy); int m = integer.Parseint (mm); Cal.Set (Y, M - 1, 1);} catch (Exception E) {System.err.Println ("DataFunction Exception" E.GetMessage ());} String MaxDay = String.Valueof (Cal, Day_OF_MONTH); Return MaxDay;}
/ ** * Get a maximum number of days * * @Param _yyyyymm * years * @return this month's maximum number of days * / public static synchronized string getMaxMontHDAY (String _yyyyymm) {string yy = _yyyyymm.substring (0, 4); String mm = _yyyymm.substring (4, 6); return getmaxmonthday (yy, mm);} // --------------------------- -------------------------------------------------- - // / ** * Get a month * * @Param _mm * @Return this month * / public DateFormat getPreviousMonth (int _mm) {if (_mm <0) Return GetNextMonth (-_mm); int = integer.parseint (year); int imm = integer.parseint (MONTH); while (IMM - _MM <= 0) {_mm - = 12; iYy--;} IMM - = _mm;
String yy = string.valueof (iey); string mm = string.valueof (IMM);
YY = StringFunction.Updatezero (YY, 4); mm = StringFunction.Updatezero (mm, 2);
DateFormat DF = New DateFormat (YY MM "01"); Return DF;}
/ ** * Get a month * * @Param _mm * @Return this month's date * / public DateFormat getNextMonth (int _mm) {if (_mm <0) Return getPreviousMonth (-_ mm ); Int = integer.parseint (year); int imm = integer.parseint (month); while (_mm imm> 12) {_mm - = 12; iYy ;} IMM = _mm;
String yy = string.valueof (iey); string mm = string.valueof (IMM);
YY = StringFunction.Updatezero (YY, 4); mm = StringFunction.Updatezero (mm, 2);
DateFormat DF = New DateFormat (YY MM "01"); Return DF;}
/ ** * Get last month * * @return this month * / public dateformat getpreviousmonth () {return getpreviousmonth (1);}
/ ** * Get next month * @return this month * / public DateFormat getNextMonth () {return getnextMonth (1);}
/ / -------------------------------------------------------------------------------------------- ------------------------------ // ** * Get a day of the past * @Param _Day * forward Number * @return Date of this day * / public dateformat getpreviousDay (int _Day) {if (_Day <0) Return getNextDay (-_ day); int = integer.Parseint (Year); int imm = integer.parseint (Month ); Int IDd = integer.parseint (day); DateFormat DF = New DateFormat (); DF.SetDate (iYy, IMM, IDD);
IF (IDD - _DAY> 0) {IDD - = _Day; Df.Setdate (IYY, IMM, IDD);} else {_day - = IDD; DF = Df.getPreviousMonth (); Df.SetDate (DF.GETMONTHDATE () getMaxMontHDay (DF.GETMONTHDATE ()))); DF = DF.GetPreviousDay (_DAY);} Return DF;}
/ ** * Get a day * @Param _Day * @Return This day * / public dateformat getnextday (int _Day) {if (_day <0) return getpreviousDay (-_ day); INT IYY = INTEGER.PARSEINT (YEAR); int imm = integer.parseint (month); int IDd = integer.parseint (day); DateFormat DF = New DateFormat (); DF.SetDate (iYy, IMM, IDD);
IF (IDD _DAY <= INTEGER .PARSEINT (Dateformat.getMaxMontHDay (DF.GETMONTHDATE ()))) {IDD = _Day; Df.Setdate (IMM, IDD);} else {_day - = INTEGER.PARSEINT Dateformat.getMaxMonthday (DF.GETMONTHDATE ()) - IDD 1; DF = DF.GetNextMonth (); df.setdate (DF.GETMONTHDATE () "01"); DF = DF.GetNextDay (_DAY);} return DF;
/ / -------------------------------------------------------------------------------------------- ------------------------------ // ** * Get a day of the date of the past few months * * @Param _DATE * Specified date * @Param _Month * forwarded month * @Return results Date * / public synchronized static dateformat getpreviousmonth (string _date, int _month) {if (! DateFormat.checkdate (_date)) {throw new NullPointerException ("Date is incorrect");} return new dateformat (_date) .GetpreviousMonth (_MONTH);} / ** * Get a day of future days * * @Param _date * specified date * @Param _Month * Rear date * @return results Date * / public synchronized static dateformat getNextMont (String _date, int _month {if (! DateFormat.Checkdate (_date)) {throw new nullpointEREXCEPTION ("Date is incorrect");} Return new dateformat (_date) .getnextMonth (_MONTH);
/ ** * Get a day of the date of the past * * @Param _Date * Specify date * @Param _day * forward date * @return results Date * / public synchronized static dateformat getpreviousDay (String _date, int INT _DAY) {if (! DateFormat.checkdate (_date)) {throw new nullpointerException ("Date is incorrect");} return new dateformat (_date) .GetpreviousDay (_DAY);
/ ** * Get a day of future days * * @Param _date * Specify date * @Param _day * to the date * @return results Date * / public synchronized static dateformat getnextday (String _date, int _DAY) {if (! DateFormat.checkdate (_date)) {throw new nullpointersException ("Date is incorrect");} return new dateformat (_date) .Getnextday (_DAY);
/ / -------------------------------------------------------------------------------------------- ----------------------------- // / ** * Compare two days before and after * * @Param _Date1 * Date 1 * @Param _date2 * Date 2 * @return comparison result, if> 0, _date1> _date2; = 0, two dates; <0, _date1 <_date2 * / public synchronized static int compare (string _date1 String _date2) {if (! Checkdate (_date1)) {throw new nullpointerException ("Date is not correct");} if (! Checkdate (_date2)) {throw new nullpointerexception ("Date is incorrect");} int NDATE1 = Integer.Parseint (_date1); int ndate2 = integer.parseint (_date2); return ndate1 - ndate2;} // -------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------- --- Public string toString () {ix (getHour () == null || getminute () == null || getsecond () == null) {return getdate ();} return getdate () getTime (); }
/ / -------------------------------------------------------------------------------------------- ------------------------------ public boolean equals (Object o) {if (o instanceof DateFormat) {return toString (). Equals (O.ToString ());} else {dateformat obj = new dateformat (o.tostring (), true); return equals (obj);}}
/ / -------------------------------------------------------------------------------------------- ------------------------------ / ** * Compare large small * @Param o * @return * / public int compareto Object o) {if (o instanceof dateformat) {return to {.tostring ());} else {dateformat obj = new dateformat (o.tostring (), true); return compareto (obj);} }
/ / -------------------------------------------------------------------------------------------- ----------------------------- / ** * Return two days from the number of days * * @Param StartDate * Start Date * @param endDate * end date * @return * / public static long getPeriodDayCount (String startDate, String endDate) {date d1 = new DateFormat (startDate) .toDate (); date d2 = new DateFormat (endDate) .toDate (); long PeriodTime = D2.Gettime () - D1.GetTime (); Return PeriodTime / 24/60/60/1000;} // ---------------------- -------------------------------------------------- ------ Public Date Todate () {Calendar C = Calendar.GetInstance (); int = integer.parseint (getYear ()); C.SET (Calendar.Year, Integer.Parseint (GetYear ())) C.SET (Calendar.Month, Integer.Parseint (getMopeN) - 1); C.SET (Calendar.date, Integer.Parseint (GetDay ())); C.SET (Calendar.Hour_Of_Day, Integer.Parseint (gethour ())); C.SET (Calendar.minute, Integer.Parseint (Getminute ()); C.SET (Calendar.Second, Integer.Parseint (GetSecond ()));
Return c.gettime ();} / ** * Return a date is the day of the week * @Param _Date * @Return * @throws exception * / public static string getWeek (Date _date) {SimpleDateFormat DTFORMAT = New SimpleDateFormat ("e") String Week = DTFORMAT.FORMAT (_DATE); RETURN Week;} / ** * Returns a date is the day * @Param _Date * @return * @throws exception * / public static string getWeek (String _date) throws exception {ix (! checkDate (_date)) throw new NullPointerException ( "date is not legal"); SimpleDateFormat dtFormat = new SimpleDateFormat ( "yyyyMMdd"); date date = dtFormat.parse (_date); String week = getWeek (date); return week; } Public static arraylist week () {arraylist alist = new arraylist (); alist.add ("Sunday"); Alist.Add ("Monday"); alist.add ("Tuesday); alist.add (" Wednesday "); Alist.add (" Thursday "); alist.add (" Friday "); alist.add (" Saturday "); return alist;} / ** * Return a time to time * @Param Date _Date * @return time period EG: 2004-11-01 --- 2004-11-06 * / public string getdateweek (date _date) throws exception {simpleDateformat DTFORMAT = New SimpleDateFormat ("YYYYMMDD"); String Date = DTFORMAT.FORMAT (_DATE); return getdateweek (Date);} / ** * Returns a week of time * @Param string _date * @return a week time EG: 2004-11-01 --- 2004-11-06 * / public string getdateweek (String _date) throws exception {arraylist alist = new arraylist (); alist = week (); string aweek = getWeek (_date); int prodt = 0; int nextdt = 0; INT i = 0; while (i Dateformat nextddate = getNextDay (_date, nextdt); return getNextDay.getprintdate () "---" nextddate.getprintdate ();} / ** * After returning _date's post-DAY day is the week * @ param Date _date * @param int _day * @return * @throws Exception * / public static String getNextWeek (Date _date, int _day) throws Exception {SimpleDateFormat dtFormat = new SimpleDateFormat ( "yyyyMMdd"); String date = dtFormat.format (_date Return getNextWeek (Date, _Day);} / ** * After returning a time _ DAY day is the week * @Param string _date * @Param INT _DAY * @Return * @throws exception * / public static String getNextWeek (String _date, int _day) throws Exception {DateFormat dtFormat = new DateFormat (); dtFormat = DateFormat.getNextDay (_date, _day); return getWeek (dtFormat.getYear () dtFormat.getMonth () dtFormat.getDay ( ));} / ** * For a time _ Date's front _Day day is the day * @Param Date _date * @Param INT _DAY * @Return * @throws exception * / public static string getpreviousweek (date _date, int _DAY) THROWS Exception {SimpleDateFormat DTFORMAT = New Simpleda Teformat ("YYYYMMDD"); String Date = DTFORMAT.FORMAT (_DATE); Return getPreviousWeek (Date, _Day);} / ** * Back to a time _ DAY day is the week * @Param string _date * @param int _day * @return * @throws Exception * / public static String getPreviousWeek (String _date, int _day) throws Exception {DateFormat dtFormat = new DateFormat (); dtFormat = DateFormat.getPreviousDay (_date, _day); return getWeek (dtFormat } // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ------------------------------------------------- Public static void main (string [] args) {try {date year = new date (); DATEFORMAT DTFMT = New DateFormat (); string week = dateformat.getpreviousweek (Year, 3); System.out.println (Week); } catch (exception e) {E.PrintStackTrace ();}}}