首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

一个依据时间段获取时间的集合

2012-09-01 
一个根据时间段获取时间的集合? ? ?在程序中有时候这样的情况就是获取时间段中的时间集合进行处理,这几天

一个根据时间段获取时间的集合

? ? ?在程序中有时候这样的情况就是获取时间段中的时间集合进行处理,这几天做了一个excel导出,根绝时间段查询生成不同的天数标题数据。

?

? ? ? ?相对来说比较准确。

?

?

?

/** * 获得两个日期(字符串)之间的所有日期 *  * @param start_date *             String 开始日期 EX:“20120101” 强制输入参数 * @param end_date *             String 结束日期 EX:“20120102” 强制输入参数 * @return */public static List<String> getDatesBetween2Date(String start_date, String end_date) {List<String> result = new ArrayList<String>();try {start_date = StringUtils.trim(start_date);end_date = StringUtils.trim(end_date);if (StringUtils.isEmpty(start_date) || StringUtils.isEmpty(end_date)) {return result;}SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");int recAfter = diffDate(sdf.parse(end_date), sdf.parse(start_date));if (recAfter < 0) {String temp = start_date;start_date = end_date;end_date = temp;recAfter = -1 * recAfter;}result.add(start_date);String tempStart_date = new String(start_date.getBytes("UTF-8"), "UTF-8");while (recAfter > 0) {tempStart_date = DateUtil.turnDate(tempStart_date, "yyyyMMdd", 1);result.add(tempStart_date);recAfter--;}}catch (Exception e) {e.printStackTrace();}return result;}public static String turnDate(String showDate, String format, int interDay) {// 日期加指定天数Calendar cal = Calendar.getInstance();Date tempDate_001 = DateUtil.parseDate(showDate, format);if (null == tempDate_001) {return null;}cal.setTime(tempDate_001);cal.add(Calendar.DAY_OF_MONTH, interDay);String next = DateUtil.formatDate(cal.getTime(), format);return next;}

?

?

?

?写个main方法调用:

?

?

System.out.println(DateUtil.getDatesBetween2Date("20120101", "20120104"));

?

?

?

?

返回的是一个 list集合:? 20120101、20120102 、 20120103、20120101这样的集合。

热点排行