获取时间段范围内的每一天
public static void main(String[] args) { try { String s1 = "2022-01-01"; String s2 = "2022-01-03"; // 算两个日期间隔多少天 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); Date date1 = format.parse(s1); Date date2 = format.parse(s2); String dateTimeStart = ""; String dateTimeEnd = ""; String dateStart = ""; String dateEnd = ""; int datelen = (int) ((date2.getTime() - date1.getTime()) / (1000 * 3600 * 24)); Date sDate = format.parse(s1); // logger.info("-------------合并开始" + DateUtils.getNowString()); for (int i = 0; i <= datelen; i++) { dateTimeStart = format.format(sDate) + " 00:00:00"; dateStart = format.format(sDate); // 日期+1 Calendar c = Calendar.getInstance(); c.setTime(sDate); c.add(Calendar.DAY_OF_MONTH, 1); sDate = c.getTime(); dateTimeEnd = format.format(sDate) + " 00:00:00"; dateEnd = format.format(sDate); logger.info(dateTimeStart + "++++++" + dateTimeEnd); } } catch (java.lang.Exception e) { // java.lang.Exception } }
打印结果:
越努力越幸运~ 加油ヾ(◍°∇°◍)ノ゙