package com.changchong.util;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import org.junit.Test;
/**
* 根据一段时间区间,按月份拆分成多个时间段
* @author lxg
*
* 2016年9月5日下午6:18:36
*/
public class SplitDateUtil {
@Test
public void demo(){
List<KeyValueForDate> list = SplitDateUtil.getKeyValueForDate("2015-08-23","2016-06-10");
System.out.println("开始日期--------------结束日期");
for(KeyValueForDate date : list){
System.out.println(date.getStartDate()+"-----"+date.getEndDate());
}
}
/**
* 根据一段时间区间,按月份拆分成多个时间段
* @param startDate 开始日期
* @param endDate 结束日期
* @return
*/
@SuppressWarnings("deprecation")
public static List<KeyValueForDate> getKeyValueForDate(String startDate,String endDate) {
List<KeyValueForDate> list = null;
try {
list = new ArrayList<KeyValueForDate>();
String firstDay = "";
String lastDay = "";
Date d1 = new SimpleDateFormat("yyyy-MM-dd").parse(startDate);// 定义起始日期
Date d2 = new SimpleDateFormat("yyyy-MM-dd").parse(endDate);// 定义结束日期
Calendar dd = Calendar.getInstance();// 定义日期实例
dd.setTime(d1);// 设置日期起始时间
Calendar cale = Calendar.getInstance();
Calendar c = Calendar.getInstance();
c.setTime(d2);
int startDay = d1.getDate();
int endDay = d2.getDate();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
KeyValueForDate keyValueForDate = null;
while (dd.getTime().before(d2)) {// 判断是否到结束日期
keyValueForDate = new KeyValueForDate();
cale.setTime(dd.getTime());
if(dd.getTime().equals(d1)){
cale.set(Calendar.DAY_OF_MONTH, dd
.getActualMaximum(Calendar.DAY_OF_MONTH));
lastDay = sdf.format(cale.getTime());
keyValueForDate.setStartDate(sdf.format(d1));
keyValueForDate.setEndDate(lastDay);
}else if(dd.get(Calendar.MONTH) == d2.getMonth() && dd.get(Calendar.YEAR) == c.get(Calendar.YEAR)){
cale.set(Calendar.DAY_OF_MONTH,1);//取第一天
firstDay = sdf.format(cale.getTime());
keyValueForDate.setStartDate(firstDay);
keyValueForDate.setEndDate(sdf.format(d2));
}else {
cale.set(Calendar.DAY_OF_MONTH,1);//取第一天
firstDay = sdf.format(cale.getTime());
cale.set(Calendar.DAY_OF_MONTH, dd
.getActualMaximum(Calendar.DAY_OF_MONTH));
lastDay = sdf.format(cale.getTime());
keyValueForDate.setStartDate(firstDay);
keyValueForDate.setEndDate(lastDay);
}
list.add(keyValueForDate);
dd.add(Calendar.MONTH, 1);// 进行当前日期月份加1
}
if(endDay<startDay){
keyValueForDate = new KeyValueForDate();
cale.setTime(d2);
cale.set(Calendar.DAY_OF_MONTH,1);//取第一天
firstDay = sdf.format(cale.getTime());
keyValueForDate.setStartDate(firstDay);
keyValueForDate.setEndDate(sdf.format(d2));
list.add(keyValueForDate);
}
} catch (ParseException e) {
return null;
}
return list;
}
}
class KeyValueForDate{
private String startDate;
private String endDate;
public String getStartDate() {
return startDate;
}
public void setStartDate(String startDate) {
this.startDate = startDate;
}
public String getEndDate() {
return endDate;
}
public void setEndDate(String endDate) {
this.endDate = endDate;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133