반응형
날짜(일별, 월별, 연도별) FOR문
//일별 For문 예시
private void dateRepeat(String stDate, String edDate) {
String startDate = '20221201'; //예시로 stDate 대신 일자 설정
String endDate = '20230201'; //예시로 edDate 대신 일자 설정
SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMdd");
Date startDateTime = fmt.parse(startDate);
Date endDateTime = fmt.parse(endDate);
Calendar start = Calendar.getInstance();
Calendar end = Calendar.getInstance();
start.setTime(startDateTime);
end.setTime(endDateTime);
//시작일부터 종료일까지 1일씩 증가 for문(일수 증가시 for문의 add매서드 1을 원하는 일수로 변경)
for(Date date = start.getTime(); !start.after(end); start.add(Calendar.DATE,1); date = start.getTime()){
//일별 작업(ex: Batch) 처리
}
}
//월별 For문 예시
private void monthRepeat(String stDate, String edDate) {
String startDate = '20221201'; //예시로 stDate 대신 일자 설정
String endDate = '20230201'; //예시로 edDate 대신 일자 설정
Calendar cal = Calendar.getInstance();
cal.set(endDate.substring(0,4), endDate.substring(4,6), endDate.substring(6,8));
//for문 계산시 일별 계산되기 때문에 해당월 마지막일로 설정
endDate = endDate.substring(0,6) + cal.getActualMaximum(Calendar.DAY_OF_MONTH));
SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMdd");
Date startDateTime = fmt.parse(startDate);
Date endDateTime = fmt.parse(endDate);
Calendar start = Calendar.getInstance();
Calendar end = Calendar.getInstance();
start.setTime(startDateTime);
end.setTime(endDateTime);
//시작일부터 종료일까지 1달씩 증가 for문
for(Date date = start.getTime(); !start.after(end); start.add(Calendar.MONTH,1); date = start.getTime()){
//일별 작업(ex: Batch) 처리
}
}
// 연도별 For문 처리
private void yearRepeat(String stDate, String edDate) { //20221201, 20230201
String startDate = stDate;
String endDate = edDate.substring(0,4)+"1231"; //해당 연도의 가장마지막 일자로 설정
SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMdd");
Date startDateTime = fmt.parse(startDate);
Date endDateTime = fmt.parse(endDate);
Calendar start = Calendar.getInstance();
Calendar end = Calendar.getInstance();
start.setTime(startDateTime);
end.setTime(endDateTime);
//시작일부터 종료일까지 1년씩 증가 for문
for(Date date = start.getTime(); !start.after(end); start.add(Calendar.YEAR,1); date = start.getTime()){
//일별 작업(ex: Batch) 처리
}
}
반응형
'JAVA' 카테고리의 다른 글
[JAVA] HTML5 이하 특수문자('포함) JAVA에서 치환 (0) | 2022.02.07 |
---|---|
[JAVA] user agent 브라우저/버전 정보 확인 (0) | 2019.11.20 |
[JAVA] iBatis / MyBatis 차이 (변경사항) (0) | 2019.11.20 |
[JAVA] ModelAndView 기본형식 (0) | 2019.11.13 |
[JAVA] MVC패턴 redirect 전달방법 (0) | 2019.11.13 |