본문 바로가기
JAVA

JAVA - 콘솔창에 달력 출력하기

by 스노위13 2022. 5. 19.

콘솔창에 달력을 출력합니다. 

int year과 int month를 변경하면 달력도 변경됩니다. 아래 코드에서는 2022년 10월 달력을 출력하였습니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import java.util.Calendar;
 
public class task01 {
 
    public static void main(String[] args) {
 
        int year = 2022;
        int month = 10;
 
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month - 11);
 
        // 해당 달의 마지막 일자(DATE) 얻기
        int lastDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
 
        // 해당 달의 시작 요일 얻기
        // 1: 일요일 2: 월요일, 3: 화요일 ....
        int startDay = calendar.get(Calendar.DAY_OF_WEEK);
 
        System.out.println(year + "년" + month + "월 \n");
        System.out.println("일\t월\t화\t수\t목\t금\t토");
 
        int currentDay = 1;
 
        for (int i = 0; i <= 42; i++) {
            if (i < startDay) {
                System.out.print("\t");
            } else {
                System.out.printf("%02d\t", currentDay);
                currentDay++;
            }
 
            if (i % 7 == 0) {
                System.out.println();
            }
 
            if (currentDay > lastDay) {
                break;
            }
        }
    }
cs

출력 결과

 

댓글