日期:2014-05-20  浏览次数:20714 次

急~~~关于把时间分段显示
逻辑很明显,但是不知道用什么方法实现。求大神庇佑~~

以每个月20和21号为分界显示日期段
比如说:2013-6-19至2013-7-23,
       需要分段为2013-6-19至2013-6-20,2013-6-21至2013-7-20,2013-7-21至2013-7-23  三段

不知道怎么写啊
好纠结

日期??分段

------解决方案--------------------
用Calendar对象处理,
先比较起始日期与当月20号,计算出第一段,
然后循环累加一个月,直至超过截止日期。
------解决方案--------------------
大概是这样,然后依次输出就是你要的结果咯,
	public static List<Date> getSplitTimeList(String startTime, String endTime) {
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
List<Date> dateList = new ArrayList<Date>();

Date startDate = sdf.parse(startTime);
Date endDate = sdf.parse(endTime);

dateList.add(startDate);
dateList.add(endDate);

Calendar calendar = Calendar.getInstance();
calendar.setTime(startDate);
calendar.set(Calendar.DAY_OF_MONTH, 20);

Date splitDate1 = null, splitDate2 = null;

while (true) {
splitDate1 = calendar.getTime();
if (splitDate1.after(endDate))
break;
if (!dateList.contains(splitDate1))
dateList.add(splitDate1);

calendar.add(Calendar.DAY_OF_MONTH, 1);
splitDate2 = calendar.getTime();
if (splitDate2.after(endDate))
break;
if (!dateList.contains(splitDate2))
dateList.add(splitDate2);

calendar.add(Calendar.MONTH, 1);
calendar.set(Calendar.DAY_OF_MONTH, 20);
}

if (startDate.getDate() == 20)
dateList.add(startDate);
if (endDate.getDate() == 21)
dateList.add(endDate);

Collections.sort(dateList);

return dateList;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}

供你参考^_^
------解决方案--------------------
好吧,当我比较笨好了,上面那个有点问题
	public static List<Date> getSplitTimeList(String startTime, String endTime) {