直接贴上代码:
<span style="white-space:pre"> </span>/**
* 根据时间获取芒果数据库名(多个库)
*
* @param obj
* @return
*/
public List<String> getDbNames(String starttime, String endtime) {
if(("").equals(starttime) || ("").equals(endtime)){
return null;
}
List<String> dates = new ArrayList<String>();
Date dt_start = new Date(Long.valueOf(starttime));
Date dt_end = new Date(Long.valueOf(endtime));
//结束时间一定大于开始时间
if(dt_end.getTime() >= dt_start.getTime()){
Calendar c_begin = new GregorianCalendar();
Calendar c_end = new GregorianCalendar();
c_begin.setTime(dt_start);
c_end.setTime(dt_end);
while (c_begin.before(c_end)) {
int m = c_begin.get(Calendar.MONTH) + 1;
int y = c_begin.get(Calendar.YEAR);
ates.add(String.valueOf(y)+String.valueOf(m));
c_begin.add(Calendar.MONTH, 1);
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
String[] time = sdf.format(dt_end).split("-");
String dlast = time[0]+time[1];
if(!dates.contains(dlast)){
dates.add(time[0] + time[1]);
}
}
return dates;
}