코드
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Calendar {
// 연도와 월을 입력받아 달력을 출력하는 프로그램을 구하세용.
// 윤년과 평년 요일도 나타나게 하세요
// 년도 입력:
// 월 입력:
// ================== 2024 년 7 월 ======================
// 일 월 화 수 목 금 토
// 1 2 3 4 5 6 7
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static int year;
static int month;
static int checkYear;
static int totalNumberOfDays = 0;
static int startDays = 0;
static int endDays = 0;
static final String LONG_LINE = "===========================";
static final String LINE = "======= ";
static final String DAY = "Sun Mon Tue Wed Thu Fri Sat";
static final String ASK_YEAR = "년도 입력: ";
static final String ASK_MONTH = "월 입력: ";
static final int LEAP_YEAR = 1;
static final int COMMON_YEAR = 0;
static final int[] DAYS = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
public static void main(String[] args) throws IOException {
System.out.println(LONG_LINE);
System.out.print(ASK_YEAR);
year = Integer.parseInt(br.readLine());
System.out.print(ASK_MONTH);
month = Integer.parseInt(br.readLine());
System.out.println(LONG_LINE);
// 윤년 / 평년 체크
if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0) checkYear = LEAP_YEAR;
else checkYear = COMMON_YEAR;
if (month == 2) {
if (checkYear == LEAP_YEAR) endDays = 29;
else endDays = 28;
} else endDays = DAYS[month - 1];
// 1600 년 부터 현재 연도 이전까지의 총 일 수 구하기
for (int i = 1600; i < year; i++) {
if ((i % 4 == 0 && i % 100 != 0) || (i % 400 == 0)) totalNumberOfDays += 366; // 윤년
else totalNumberOfDays += 365; // 평년
}
// 해당 연도 시작부터 해당 월 이전까지의 일수 구하기
for (int i = 0; i < month - 1; i++) {
totalNumberOfDays += DAYS[i];
}
// 2월이고 윤년일 경우 1일 추가
if (month > 2 && checkYear == LEAP_YEAR) {
totalNumberOfDays++;
}
// 해당 월의 시작요일 구하기 (1600년 1월 1일은 토요일)
startDays = (totalNumberOfDays + 6) % 7;
int nextMonth = month + 1;
if (nextMonth > 12) nextMonth -= 12;
int prevMonth = month - 1;
if (prevMonth == 0) prevMonth = 12;
System.out.println("<< " + (prevMonth) + " 월 " + (nextMonth) + " 월 >>");
System.out.print(LINE);
System.out.print(year + " 년 " + month + " 월 ");
System.out.println(LINE);
System.out.println(DAY);
int current = 1;
for (int i = 0; i < 7; i++) {
if (i < startDays) System.out.print(" ");
else {
System.out.printf("%3d ", current);
current++;
}
}
System.out.println();
while(current < endDays) {
for (int j = 0; j < 7; j++) {
if (current > endDays) break;
System.out.printf("%3d ", current);
current++;
}
System.out.println();
}
System.out.println(LONG_LINE);
}
}
Java
복사
결과
===========================
년도 입력: 2024
월 입력: 8
===========================
<< 7 월 9 월 >>
======= 2024 년 8 월 =======
Sun Mon Tue Wed Thu Fri Sat
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
===========================
Plain Text
복사
===========================
년도 입력: 2022
월 입력: 11
===========================
<< 10 월 12 월 >>
======= 2022 년 11 월 =======
Sun Mon Tue Wed Thu Fri Sat
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
===========================
Plain Text
복사