java 文件夹 年月日,java获取两个日期之间的日月(yyyy-MM)和年月日(yyyy-MM-dd)...

java获取两个日期之间的年月(yyyy-MM)和年月日(yyyy-MM-dd)

public List processToYYYYMMDD(String date1,String date2){

List timeList=new ArrayList();

if(date1.equals(date2)){

timeList.add(date1);

return timeList;

}

String tmp;

timeList.add(date1);

if(date1.compareTo(date2) > 0){//确保 date1的日期不晚于date2

tmp = date1; date1 = date2; date2 = tmp;

}

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

timeList.add(tmp);

int num = 0;

while(tmp.compareTo(date2) < 0){

num++;

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

timeList.add(tmp);

}

// if(num == 0)

// System.out.println("两个日期相邻!");

return timeList;

}

private Date str2Date(String str) {

if (str == null) return null;

try {

return format.parse(str);

} catch (Exception e) {

e.printStackTrace();

}

return null;

}

public String[] getAllMonths(String start, String end){

String splitSign="-";

String regex="\\d{4}"+splitSign+"(([0][1-9])|([1][012]))"; //判断YYYY-MM时间格式的正则表达式

if(!start.matches(regex) || !end.matches(regex)) return new String[0];

List list=new ArrayList();

if(start.compareTo(end)==0){

String[] result=new String[1];

result[0]=start;

return result;

}

if(start.compareTo(end)>0){

//start大于end日期时,互换

String temp=start;

start=end;

end=temp;

}

String temp=start; //从最小月份开始

while(temp.compareTo(start)>=0 && temp.compareTo(end)<=0){

list.add(temp); //首先加上最小月份,接着计算下一个月份

String[] arr=temp.split(splitSign);

int year=Integer.valueOf(arr[0]);

int month=Integer.valueOf(arr[1])+1;

if(month>12){

month=1;

year++;

}

if(month<10){//补0操作

temp=year+splitSign+"0"+month;

}else{

temp=year+splitSign+month;

}

}

int size=list.size();

String[] result=new String[size];

for(int i=0;i

result[i]=list.get(i);

}

return result;

}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值