获取两个日期之间的所有日期



1.遇到了一个要获取在某个两个日期之间的所有日期,比如20150101到20150105之间的所有的日期,就是20150102,20150103,20150104。


截取源码:

/**
* 获取两个日期间的所有日期
* @author lvxinrong
*/
public static ArrayList<String> getTwoDaysTotalDays(String date1, String date2) {
ArrayList<String> L = new ArrayList<String>();
if (date1.equals(date2)) {
System.out.println("两个日期相等!");
return L;
}
String tmp;
if (date1.compareTo(date2) > 0) { // 确保 date1的日期不晚于date2
tmp = date1;
date1 = date2;
date2 = tmp;
}

tmp = format.format(str2Date(date1).getTime() + 3600 * 24 * 1000);

int num = 0;
while (tmp.compareTo(date2) < 0) {
L.add(tmp);
num++;
tmp = format.format(str2Date(tmp).getTime() + 3600 * 24 * 1000);
}


if (num == 0)
System.out.println("两个日期相邻!");
return L;
}
private static Date str2Date(String str) {
if (str == null)
return null;


try {
return format.parse(str);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}


/**
* 判断是否闰年

* @param year
* @return
*/
public static boolean isLeapYear(int year) {
return (year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0));
}


/**
* 一个月有几天

* @param year
* @param month
* @return
*/
public static int dayInMonth(int year, int month) {
boolean yearleap = isLeapYear(year);
int day;
if (yearleap && month == 2) {
day = 29;
} else if (!yearleap && month == 2) {
day = 28;
} else if (month == 4 || month == 6 || month == 9 || month == 11) {
day = 30;
} else {
day = 31;
}
return day;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值