Test 方法测试
@Test
public void dateList() throws IOException, ParseException {
String beginDay = "20191101";
String endDay = "20191209";
List<String> dateList = new ArrayList<>();
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMdd");
Date beginDate = sdf1.parse(beginDay);
Date endDate = sdf1.parse(endDay);
Calendar beginCalendar = Calendar.getInstance();
Calendar endCalendar = Calendar.getInstance();
beginCalendar.setTime(beginDate);
endCalendar.setTime(endDate);
int maxDate = 0;
if (endCalendar.get(Calendar.YEAR) == beginCalendar.get(Calendar.YEAR)) {
maxDate = endCalendar.get(Calendar.DAY_OF_YEAR) - beginCalendar.get(Calendar.DAY_OF_YEAR);
} else if (endCalendar.get(Calendar.YEAR) > beginCalendar.get(Calendar.YEAR)) {
int endNumber = endCalendar.get(Calendar.DAY_OF_YEAR);
int beginNumber = DateUtils.getYearDays(Integer.valueOf(beginDay.substring(0, 4)))
- beginCalendar.get(Calendar.DAY_OF_YEAR);
maxDate = endNumber + beginNumber;
}
for (int i = 0; i <= maxDate; i++) {
dateList.add(sdf1.format(beginCalendar.getTime()));
beginCalendar.add(Calendar.DATE, 1);
}
for (String strDate : dateList) {
System.out.println(strDate);
}
}
测试结果展示
20191101
20191102
20191103
20191104
20191105
20191106
20191107
20191108
20191109
20191110
20191111
20191112
20191113
20191114
20191115
20191116
20191117
20191118
20191119
20191120
20191121
20191122
20191123
20191124
20191125
20191126
20191127
20191128
20191129
20191130
20191201
20191202
20191203
20191204
20191205
20191206
20191207
20191208
20191209
封装好的方法
/**
* 获取日期范围内每天的日期
* @param beginDay 格式“yyyyMMdd”
* @param endDay 格式“yyyyMMdd”
* @return
*/
public List<String> getStrDateList(String beginDay,String endDay){
List<String> dateList = new ArrayList<>();
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMdd");
Date beginDate;
try {
beginDate = sdf1.parse(beginDay);
Date endDate = sdf1.parse(endDay);
Calendar beginCalendar = Calendar.getInstance();
Calendar endCalendar = Calendar.getInstance();
beginCalendar.setTime(beginDate);
endCalendar.setTime(endDate);
int maxDate = 0;
if (endCalendar.get(Calendar.YEAR) == beginCalendar.get(Calendar.YEAR)) {
maxDate = endCalendar.get(Calendar.DAY_OF_YEAR) - beginCalendar.get(Calendar.DAY_OF_YEAR);
} else if (endCalendar.get(Calendar.YEAR) > beginCalendar.get(Calendar.YEAR)) {
int endNumber = endCalendar.get(Calendar.DAY_OF_YEAR);
int beginNumber = DateUtils.getYearDays(Integer.valueOf(beginDay.substring(0, 4)))
- beginCalendar.get(Calendar.DAY_OF_YEAR);
maxDate = endNumber + beginNumber;
}
for (int i = 0; i <= maxDate; i++) {
dateList.add(sdf1.format(beginCalendar.getTime()));
beginCalendar.add(Calendar.DATE, 1);
}
} catch (ParseException e) {
e.printStackTrace();
}
return dateList;
}
获取时间范围之间的天数
/**
* @param date1
* @param date2
* @return
* @throws ParseException
*/
public static int betweenDays(String beginDay, String endDay) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
Date date1 = sdf.parse(beginDay);
Date date2 = sdf.parse(endDay);
System.out.println(date1);
Calendar begin = DateUtil.toCalendar(date1);
Calendar end = DateUtil.toCalendar(date2);
long msBegin = begin.getTimeInMillis();
long msEnd = end.getTimeInMillis();
long between_days = (msEnd - msBegin) / (1000 * 3600 * 24);
between_days = Math.abs(between_days);
return Integer.parseInt(String.valueOf(between_days));
}