手把手地写了一个函数,计算出两个字符串日期之间的所有字符串型日期 集合
//获取两个年月日格式的字符串之间的日期字符串,格式也是年月日,举例:("2013-3-5","2013-3-9")-->({'2013-3-5','2013-3-6','2013-3-7','2013-3-8','2013-3-9'})
private static List<String> getLst(String date1, String date2){
String[] startStr = date1.split("-");
String[] endStr = date2.split("-");
int startYearInt = Integer.valueOf(startStr[0]);
int startMonthInt = Integer.valueOf(startStr[1]);
int startDayInt = Integer.valueOf(startStr[2]);
int endYearInt = Integer.valueOf(endStr[0]);
int endMonthInt = Integer.valueOf(endStr[1]);
int endDayInt = Integer.valueOf(endStr[2]);
List<String> r = new ArrayList<String>();
if(startYearInt==endYearInt){
if(startMonthInt==endMonthInt){
if(startDayInt==endDayInt){
r.add(date1);
}else{
for(int i=startDayInt;i<=endDayInt;i++){
r.add(startYearInt+"-"+startMonthInt+"-"+i);
}
}
}else{
for(int k=startMonthInt;k<=endMonthInt;k++){
if(k==startMonthInt){
int startCnts = getMonthDay(String.valueOf(startYearInt),String.valueOf(startMonthInt));
for(int j=startDayInt;j<=startCnts;j++)r.add(startYearInt+"-"+startMonthInt+"-"+j);
}else if(k==endMonthInt){
for(int j=1;j<=endDayInt;j++)r.add(endYearInt+"-"+endMonthInt+"-"+j);
}else{
int otherCnts = getMonthDay(String.valueOf(startYearInt),String.valueOf(k));
for(int j=endDayInt;j<=otherCnts;j++)r.add(endYearInt+"-"+endMonthInt+"-"+j);
}
}
}
}else{
for(int k= startYearInt;k<=endYearInt;k++){
if(k==startYearInt){
for(int i= startMonthInt;i<=12;i++){
if(i==startMonthInt){
int otherCnts = getMonthDay(String.valueOf(startYearInt),String.valueOf(i));
for(int j=startDayInt;j<=otherCnts;j++)r.add(startYearInt+"-"+i+"-"+j);
}else{
int otherCnts = getMonthDay(String.valueOf(startYearInt),String.valueOf(i));
for(int j=1;j<=otherCnts;j++)r.add(startYearInt+"-"+i+"-"+j);
}
}
}else if(k==endYearInt){
for(int i= 1;i<=endMonthInt;i++){
if(i==endMonthInt){
for(int j=1;j<=endDayInt;j++)r.add(endYearInt+"-"+i+"-"+j);
}else{
int otherCnts = getMonthDay(String.valueOf(endYearInt),String.valueOf(i));
for(int j=1;j<=otherCnts;j++)r.add(endYearInt+"-"+i+"-"+j);
}
}
}else{
for(int i= 1;i<=12;i++){
int otherCnts = getMonthDay(String.valueOf(k),String.valueOf(i));
for(int j=1;j<=otherCnts;j++)r.add(k+"-"+i+"-"+j);
}
}
}
}
return r;
}
// 获取某年某月的天数
public static int getMonthDay(String yearStr, String monthStr) {
int year = Integer.valueOf(yearStr);
int month = Integer.valueOf(monthStr);
int result = 0;
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
result = 31;
break;
case 4:
case 6:
case 9:
case 11:
result = 30;
break;
case 2:
result = 28;
if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {
result = 29;
}
break;
}
return result;
}
// 测试
public static void main(String[] args) {
String date1 = "2012-11-2";
String date2 = "2013-2-8";
List<String> lst = DateUtil.getLst(date1, date2);
System.out.println(lst);
}
//输出结果
[2012-11-2, 2012-11-3, 2012-11-4, 2012-11-5, 2012-11-6, 2012-11-7, 2012-11-8, 2012-11-9, 2012-11-10, 2012-11-11, 2012-11-12, 2012-11-13, 2012-11-14, 2012-11-15, 2012-11-16, 2012-11-17, 2012-11-18, 2012-11-19, 2012-11-20, 2012-11-21, 2012-11-22, 2012-11-23, 2012-11-24, 2012-11-25, 2012-11-26, 2012-11-27, 2012-11-28, 2012-11-29, 2012-11-30, 2012-12-1, 2012-12-2, 2012-12-3, 2012-12-4, 2012-12-5, 2012-12-6, 2012-12-7, 2012-12-8, 2012-12-9, 2012-12-10, 2012-12-11, 2012-12-12, 2012-12-13, 2012-12-14, 2012-12-15, 2012-12-16, 2012-12-17, 2012-12-18, 2012-12-19, 2012-12-20, 2012-12-21, 2012-12-22, 2012-12-23, 2012-12-24, 2012-12-25, 2012-12-26, 2012-12-27, 2012-12-28, 2012-12-29, 2012-12-30, 2012-12-31, 2013-1-1, 2013-1-2, 2013-1-3, 2013-1-4, 2013-1-5, 2013-1-6, 2013-1-7, 2013-1-8, 2013-1-9, 2013-1-10, 2013-1-11, 2013-1-12, 2013-1-13, 2013-1-14, 2013-1-15, 2013-1-16, 2013-1-17, 2013-1-18, 2013-1-19, 2013-1-20, 2013-1-21, 2013-1-22, 2013-1-23, 2013-1-24, 2013-1-25, 2013-1-26, 2013-1-27, 2013-1-28, 2013-1-29, 2013-1-30, 2013-1-31, 2013-2-1, 2013-2-2, 2013-2-3, 2013-2-4, 2013-2-5, 2013-2-6, 2013-2-7, 2013-2-8]
最后想说一下,网上有大把的利用Calendar实例去做,但其实效率也不是特别快,与其用别人的,还不如自己手写一下。学无止境!