一、获取两个日期字符串中间的天:
public static List<String> getDaysBetweenTwoDays(String startDay,String endDay) throws ParseException { List<String> list = new ArrayList<>(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//格式化为年月 Calendar min = Calendar.getInstance(); Calendar max = Calendar.getInstance(); min.setTime(sdf.parse(startDay)); min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), min.get(Calendar.DATE),0,0,0); max.setTime(sdf.parse(endDay)); max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), max.get(Calendar.DATE),23,59,59); Calendar curr = min; while (curr.before(max)) { list.add(sdf.format(curr.getTime())); curr.add(Calendar.DATE, 1); } return list; }
二、获取两个日期字符串之间的月份
/** * @description 获取两个日期之间的月份 * @author tank * @date 2019/8/24 16:47 * * @param minDate * @param maxDate * @return java.util.List<java.lang.String> */ public static List<String> getMonthBetween(String minDate, String maxDate) throws ParseException { ArrayList<String> result = new ArrayList<String>(); SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");//格式化为年月 Calendar min = Calendar.getInstance(); Calendar max = Calendar.getInstance(); min.setTime(sdf.parse(minDate)); min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1); max.setTime(sdf.parse(maxDate)); max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2); Calendar curr = min; while (curr.before(max)) { result.add(sdf.format(curr.getTime())); curr.add(Calendar.MONTH, 1); } return result; }
原文地址:https://www.cnblogs.com/tank073/p/11438924.html
时间: 2024-10-06 22:01:15