본문 바로가기
자바

특정한 두 날짜 사이에 요일별 날짜 구하기

by 처리2 2019. 12. 26.
반응형

날짜 사용을 위한 객체 생성

Calendar cal = Calendar.getInstance();

 

데이터 형태 지정을 위한 코드

 DateFormat df = new SimpleDateFormat("yyyy-MM-dd");

 

현재날짜

String StartDate = "2019-10-01";

종료날짜
String EndDate = "2019-10-31";

 

Date 타입으로 형변환

Date d1 = df.parse(StartDate );
Date d2 = df.parse(EndDate );

 

Calendar 타입으로 변경

Calendar c1 = Calendar.getInstance();
Calendar c2 = Calendar.getInstance();

c1.setTime(d1);

c2.setTime(d2);

 

시작날짜와 종료날짜 사이에 날짜 하나씩 구하기

while( c1.compareTo(c2) != 1){

            

           타입 지정

            SimpleDateFormat transFormat = new SimpleDateFormat("yyyy-MM-dd");

            String date = transFormat.format(c1.getTime());

          시작날짜 +1일

            Calendar calendar = Calendar.getInstance();

            calendar.setTime(c1.getTime());

   현재 날짜의 요일 구하기(1-일, 2-월, 3-화, 4-수, 5-목, 6-금, 7-토 가 기본입니다.)

    int dayNum = calendar.get(Calendar.DAY_OF_WEEK);

}

반응형

댓글