Package net. cssystem. util;
Import java. Text. decimalformat;
Import java. Text. parseexception;
Import java. Text. simpledateformat;
Import java. util. arraylist;
Import java. util. date;
Import java. util. List;
/**
* @ Author cssystem
* @ Version 2013-3-27
* Enter the class description here.
*/
Public class dateutil {
Private Static transient int gregoriancutoveryear = 1582;
/** Number of days per month in a leap year */
Private Static final int [] days_p_month_ly = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31 };
/** Number of days per month in a non-leap year */
Private Static final int [] days_p_month_cy = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31 };
/** Indicates the year, month, and day in the array */
Private Static final int y = 0, M = 1, D = 2;
/**
* Splits the string representing the date into an integer array representing the year, month, and day.
* @ Param date
* @ Return
*/
Public static int [] splitymd (string date ){
Date = date. Replace ("-","");
Int [] ymd = {0, 0, 0 };
Ymd [y] = integer. parseint (date. substring (0, 4 ));
Ymd [m] = integer. parseint (date. substring (4, 6 ));
Ymd [d] = integer. parseint (date. substring (6, 8 ));
Return ymd;
}
/**
* Check whether the input parameter represents a leap year.
* @ Param year
* @ Return
*/
Public static Boolean isleapyear (INT year ){
Return year> = gregoriancutoveryear?
(Year % 4 = 0) & (Year % 100! = 0) | (Year % 400 = 0): (Year % 4 = 0 );
}
/**
* Date plus 1 day
* @ Param year
* @ Param month
* @ Param day
* @ Return
*/
Private Static int [] addoneday (INT year, int month, int day ){
If (isleapyear (year )){
Day ++;
If (day> days_p_month_ly [month-1]) {
Month ++;
If (month> 12 ){
Year ++;
Month = 1;
}
Day = 1;
}
} Else {
Day ++;
If (day> days_p_month_cy [month-1]) {
Month ++;
If (month> 12 ){
Year ++;
Month = 1;
}
Day = 1;
}
}
Int [] ymd = {year, month, day };
Return ymd;
}
/**
* Add less than two months or dates to two
* @ Param decimal
* @ Return
*/
Public static string formatmonthday (INT decimal ){
Decimalformat df = new decimalformat ("00 ");
Return DF. Format (decimal );
}
/**
* Set the year with fewer than four digits to four digits.
* @ Param decimal
* @ Return
*/
Public static string formatyear (INT decimal ){
Decimalformat df = new decimalformat ("0000 ");
Return DF. Format (decimal );
}
/**
* Calculate the number of days between two dates
* @ Param begin
* @ Param end
* @ Return
* @ Throws parseexception
*/
Public static long countday (string begin, string end ){
Simpledateformat format = new simpledateformat ("yyyy-mm-dd ");
Date begindate, enddate;
Long day = 0;
Try {
Begindate = format. parse (BEGIN );
Enddate = format. parse (end );
Day = (enddate. gettime ()-begindate. gettime ()/(24*60*60*1000 );
} Catch (parseexception e ){
E. printstacktrace ();
}
Return day;
}
/**
* Calculate the date in a loop
* @ Param begindate enddate
* @ Param days
* @ Return
*/
Public static list <string> geteveryday (string begindate, string enddate ){
Long days = countday (begindate, enddate );
Int [] ymd = splitymd (begindate );
List <string> everydays = new arraylist <string> ();
Everydays. Add (begindate );
For (INT I = 0; I <days; I ++ ){
Ymd = addoneday (ymd [Y], ymd [m], ymd [d]);
Everydays. Add (formatyear (ymd [y]) + "-" + formatmonthday (ymd [m]) + "-" + formatmonthday (ymd [d]);
}
Return everydays;
}
Public static void main (string [] ARGs ){
List <string> List = dateutil. geteveryday ("2013-03-01", "2013-03-28 ");
For (string result: List ){
System. Out. println (result );
}
}
}