给个时间区间,返回该时间区间所包含的日期

仅能得到时间垮幅为一个月的,当然可以自己更改(用for循环)。

/**
* 时间区间所包含的日期.
* @param startDate
* @param endDate
*/
public String[] formatDate(String startDate, String endDate) {
String[] date = null;
if (startDate.equals(endDate)) {// 同一天.
date = new String[] { endDate };
} else if (startDate.substring(5, 7).equals(endDate.substring(5, 7))) {// 不同天,同一月.
int a = Integer.parseInt(endDate.substring(8))
- Integer.parseInt(startDate.substring(8));
date = new String[a + 1];
for (int i = 0; i <= a; i++) {
date[i] = startDate.substring(0, 8)
+ (Integer.parseInt(startDate.substring(8)) + i);
}
} else {// 不同天,不同月.
String d[] = endDate.toString().split("-");
int yy = Integer.parseInt(d[0]);
int mm = Integer.parseInt(d[1]);
int dd = Integer.parseInt(d[2]);
mm = mm - 1;
if (mm == 2) {
dd = 28;
} else if (mm == 1 || mm == 3 || mm == 5 || mm == 7 || mm == 8
|| mm == 10 || mm == 12) {
dd = 31;
} else {
dd = 30;
}
// 判断闰年
boolean r = yy % 4 == 0 && yy % 100 != 0 || yy % 400 == 0;
if (r && mm == 2)
dd++;

String temp = String.valueOf(yy) + "-"
+ String.valueOf(mm < 10 ? "0" + mm : mm) + "-"
+ String.valueOf(dd);
int a = Integer.parseInt(temp.substring(8))
- Integer.parseInt(startDate.substring(8));
int b = Integer.parseInt(endDate.substring(8)) - 1;
date = new String[a + b + 2];
for (int i = 0; i <= a; i++) {
date[i] = startDate.substring(0, 8)
+ (Integer.parseInt(startDate.substring(8)) + i);
}
for (int i = 0; i <= b; i++) {
date[a + i + 1] = endDate.substring(0, 8)
+ ((i + 1) < 10 ? "0" + (i + 1) : (i + 1));
}
}
for (int i = 0; i < date.length; i++) {
System.out.println(date[i]);
}
return date;
}



2010-02-25 和 2010-03-02 返回结果:
2010-02-25
2010-02-26
2010-02-27
2010-02-28
2010-03-01
2010-03-02
2010-02-25
2010-02-26
2010-02-27
2010-02-28
2010-03-01
2010-03-02的数组
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值