[JAVA] 시작일과 종료일 사이의 날짜 가져오기
|
/** * beginDate 부터 endDate 사이의 날짜 가져오기 * * ex) beginDate == 20200930, endDate == 20201004 일 때, * [20200930, 20201001, 20201002, 20201003, 20201004] 리턴 * @param beginDate * @param endDate * @return * @throws Exception */ private ArrayList<String> getDaysBetweenDates(String beginDate, String endDate) throws Exception { if (beginDate != null && beginDate.length() >= 8) { beginDate = beginDate.substring(0, 8); } else { return null; } if (endDate != null && endDate.length() >= 8) { endDate = endDate.substring(0, 8); } else { return null; } int iBeginDate = Integer.parseInt(beginDate); int iEndDate = Integer.parseInt(endDate); if (iBeginDate > iEndDate) { return null; } // 시작날짜 세팅 int beginYear = Integer.parseInt(beginDate.substring(0, 4)); int beginMonth = Integer.parseInt(beginDate.substring(4, 6)); int beginDay = Integer.parseInt(beginDate.substring(6, 8)); Calendar cal = Calendar.getInstance(); cal.set(beginYear, beginMonth – 1, beginDay); ArrayList<String> resultList = new ArrayList<String>(); SimpleDateFormat sdf = new SimpleDateFormat(“yyyyMMdd”); String oneDay = “”; // beginDate 부터 endDate 사이의 날짜 담기 while (Integer.parseInt(sdf.format(cal.getTime())) <= iEndDate) { oneDay = sdf.format(cal.getTime()); resultList.add(oneDay); // 다음날로 날짜 조정 cal.add(Calendar.DATE, 1); } return resultList; }
|
참고사이트 : https://bugnote.tistory.com/26