반응형
자바로 달력을 표시하기 위해서는 1일이 무슨 요일인지 알아야 하며(정확히는 요일에 해당하는 숫자를 알아야 한다.)
해당 월의 마지막 날짜를 알아야 한다. (마지막 날짜는 무조건 31일로 정해진것이 아니기 때문에...)
그리고 달력처럼 칸을 나누어 출력할 경우에는 마지막 날짜가 평일인경우 마지막 날짜로부터 주말이 될때까지 빈칸도 표시해주어야 하므로
해당 월의 주가 몇주가 있는지 알아야 한다. 그래야 for 문을 이용하여 loop 횟수를 구할 수 있다.
아래는 해당 월이 몇주까지 있는지 확인하는 소스이다.
Calendar cal = Calendar.getInstance(); // iYear : current Year, iMonth : current Month (january is 0), 1 : one day cal.set(iYear, iMonth, 1); if(increseOrDecreseMonth != null && !increseOrDecreseMonth .equals("")) { if(increseOrDecreseMonth .equals("DECRESE_MONTH")) cal.add(Calendar.MONTH, -1); if(increseOrDecreseMonth .equals("INCRESE_MONTH")) cal.add(Calendar.MONTH, +1); } // 선택 월의 시작요일을 구한다. int startNum = cal.get(Calendar.DAY_OF_WEEK); // 선택 월의 마지막 날짜를 구한다. (2월인경우 28 또는 29일, 나머지는 30일과 31일) int lastNum = cal.getActualMaximum(Calendar.DATE); // Calendar 객체의 날짜를 마지막 날짜로 변경한다. cal.set(Calendar.DATE, lastNum); // 마지막 날짜가 속한 주가 선택 월의 몇째 주인지 확인한다. 이렇게 하면 선택 월에 주가 몇번 있는지 확인할 수 있다. int weekNum = cal.get(Calendar.WEEK_OF_MONTH);
아래는 위의 방법을 가지고 java의 calendar를 이용하여 달력을 출력하는 소스이다.
package javatest; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class GetWeekNumberInMonth { public static int DECRESE_MONTH = -1; public static int INCRESE_MONTH = +1; public static int NOTHING_MONTH_MODE = 0; public static int COUNT_DAY_NUMBER_IN_WEEK = 7; private int monthChangeType = 0; private int currentYear; private int currentMonth; private int numberOfStartDate; private int numberOfEndDate; private int weekNumberInCurrentMonth; private int increseOrDescreseMonth; private Calendar cal; public static void main(String[] args) { new GetWeekNumberInMonth(); } public GetWeekNumberInMonth() { currentYear = 2016; currentMonth = 0; //monthChangeType = INCRESE_MONTH; // 웹화면같은경우 이전달과 다음달 선택 cal = Calendar.getInstance(); cal.set(currentYear, currentMonth, 1); // iYear : current Year, iMonth : current Month (1월은 0), 1 : one day // 웹화면에서 이전달을 선택한 경우 -1 달을 해준다. if(monthChangeType == GetWeekNumberInMonth.DECRESE_MONTH) cal.add(Calendar.MONTH, GetWeekNumberInMonth.DECRESE_MONTH); // 웹화면에서 다음달을 선택한 경우 -1 달을 해준다. if(monthChangeType == GetWeekNumberInMonth.INCRESE_MONTH) cal.add(Calendar.MONTH, GetWeekNumberInMonth.INCRESE_MONTH); numberOfStartDate = cal.get(Calendar.DAY_OF_WEEK); // 선택 월의 시작날짜의 요일을 구한다. numberOfEndDate = cal.getActualMaximum(Calendar.DATE); // 선택 월의 마지막 날짜를 구한다 cal.set(Calendar.DATE, numberOfEndDate); // Calendar 객체의 날짜를 마지막 날짜로 변경한다. weekNumberInCurrentMonth = cal.get(Calendar.WEEK_OF_MONTH); // 마지막 날짜가 속한 주가 선택 월의 몇째 주인지 확인한다 // 달력을 출력한다. printCalendar(); } public static String getDateByString(Date date) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); return sdf.format(date); } private void printCalendar() { System.out.println("*********************"); System.out.println(currentYear + " - " + (currentMonth +1) + "'s calendar"); System.out.println("*********************"); // 일주일은 7일이므로 7 * 선택월에 속한 주수 를 해주면 loop 횟수를 구할 수 있다. // 이렇게 하는 이유는 마지막날짜가 달력의 마지막 칸이 아닌경우에는 빈칸들도 표시해주어야 하기 때문에 전체 loop 횟수를 구한다. // 만약 마지막날짜까지만 출력하고 loop를 그만두려면 loopCount = numberOfStartDate + numberOfEndDate 해주면 된다. // (1일부터 마지막 날짜까지 찍어주고 1일을 찍기전에 빈칸들을 찍어주기 위해서 numberOfStartDate 를 더해준다.) int loopCount = COUNT_DAY_NUMBER_IN_WEEK * weekNumberInCurrentMonth; for(int i=0, printDay=1; i<loopCount;i++) { // 반복되는 index가 시작일자보다 크거나 같으며 화면에 출력되는 날짜(printDay)가 마지막 날짜보다 작거나 같은경우에는 // 날짜를 출력해준다. 날짜를 출력해준 후 1증가 하여 다음번에 표시되도록 한다. if(i >= numberOfStartDate && printDay <= numberOfEndDate) System.out.format(" %02d", printDay++); // index가 시작일자보다 작거나 마지막 날짜보다 큰경우에는 빈칸을 출력한다. else System.out.print(" "); // 일주일은 7일이므로 index를 7로 나눈 나머지의 값이 0인경우에는 줄을 바꿔 다음주로 넘긴다. if((i % COUNT_DAY_NUMBER_IN_WEEK) == 0) System.out.println(""); } } }
결과는 아래와 같다
반응형
'컴퓨터관련' 카테고리의 다른 글
java.lang.ClassNotFoundException: org.apache.tiles.TilesApplicationContext (0) | 2016.02.03 |
---|---|
Oracle 사용자 테이블과 컬럼 및 코멘트 구하는 쿼리 (0) | 2016.01.27 |
Java를 이용하여 2016년부터 2030년까지 공휴일 데이터 조회해보자 (0) | 2016.01.26 |
Java를 이용하여 음력과 양력 변환하기 (0) | 2016.01.26 |
Oracle timestamp 값을 date 형식으로 변환하기 (0) | 2016.01.21 |