Skip to content

Instantly share code, notes, and snippets.

@Exom9434
Last active January 25, 2025 08:25
Show Gist options
  • Select an option

  • Save Exom9434/35bd313d14728f5b38b23d1202905214 to your computer and use it in GitHub Desktop.

Select an option

Save Exom9434/35bd313d14728f5b38b23d1202905214 to your computer and use it in GitHub Desktop.
//노재경
import java.time.LocalDate;
import java.util.InputMismatchException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int year = getInput("달력의 년도를 입력해 주세요.(yyyy): ", 0, Integer.MAX_VALUE);
int month = getInput("달력의 월을 입력해 주세요.(mm): ", 1, 12);
System.out.println("\n[달력 출력 프로그램]");
printCalendar(year, month - 1); // 이전 달
printCalendar(year, month); // 현재 달
printCalendar(year, month + 1); // 다음 달
}
public static int getInput(String prompt, int min, int max) {
Scanner scanner = new Scanner(System.in);
int value = -1;
while (true) {
try {
System.out.print(prompt);
value = scanner.nextInt();
if (value < min || value > max) {
System.out.printf("입력 값은 %d에서 %d 사이의 값이어야 합니다.\n", min, max);
} else {
break;
}
} catch (InputMismatchException e) {
System.out.println("숫자를 입력해 주세요.");
scanner.next(); // 잘못된 입력 제거용
}
}
return value;
}
public static void printCalendar(int year, int month) {
// 월과 연도를 조정 (1월 전후 및 12월 전후 처리)
if (month < 1) {
year -= 1;
month = 12;
} else if (month > 12) {
year += 1;
month = 1;
}
LocalDate firstDate = LocalDate.of(year, month, 1);
int daysInMonth = firstDate.lengthOfMonth();
int startDayOfWeek = firstDate.getDayOfWeek().getValue();
System.out.printf("\n[%d년 %02d월]\n", year, month);
System.out.println("일\t월\t화\t수\t목\t금\t토");
// 첫 주 공백 출력
for (int i = 0; i < startDayOfWeek % 7; i++) {
System.out.print("\t");
}
// 날짜 출력
for (int date = 1; date <= daysInMonth; date++) {
System.out.printf("%02d\t", date);
if ((startDayOfWeek + date - 1) % 7 == 6) { //토
System.out.println();
}
}
System.out.println();
}
}
@Exom9434

Copy link
Copy Markdown
Author

//노재경

import java.time.LocalDate;
import java.util.InputMismatchException;
import java.util.Scanner;

public class Main {

public static void main(String[] args) {
    int year = getInput("달력의 년도를 입력해 주세요.(yyyy): ", 0, Integer.MAX_VALUE);
    int month = getInput("달력의 월을 입력해 주세요.(mm): ", 1, 12);

    System.out.println("\n[달력 출력 프로그램]");
    printCalendar(year, month - 1); // 이전 달
    printCalendar(year, month);     // 현재 달
    printCalendar(year, month + 1); // 다음 달
}


public static int getInput(String prompt, int min, int max) {
    Scanner scanner = new Scanner(System.in);
    int value = -1;
    while (true) {
        try {
            System.out.print(prompt);
            value = scanner.nextInt();
            if (value < min || value > max) {
                System.out.printf("입력 값은 %d에서 %d 사이의 값이어야 합니다.\n", min, max);
            } else {
                break;
            }
        } catch (InputMismatchException e) {
            System.out.println("숫자를 입력해 주세요.");
            scanner.next(); // 잘못된 입력 제거
        }
    }
    return value;
}


public static void printCalendar(int year, int month) {
    // 월과 연도를 조정 (1월 전후 및 12월 전후 처리)
    if (month < 1) {
        year -= 1;
        month = 12;
    } else if (month > 12) {
        year += 1;
        month = 1;
    }

    LocalDate firstDate = LocalDate.of(year, month, 1);
    int daysInMonth = firstDate.lengthOfMonth();
    int startDayOfWeek = firstDate.getDayOfWeek().getValue();

    System.out.printf("\n[%d년 %02d월]\n", year, month);
    System.out.println("일\t월\t화\t수\t목\t금\t토");

    // 첫 주 공백 출력
    for (int i = 0; i < startDayOfWeek % 7; i++) {
        System.out.print("\t");
    }

    // 날짜 출력
    for (int date = 1; date <= daysInMonth; date++) {
        System.out.printf("%02d\t", date);
        if ((startDayOfWeek + date - 1) % 7 == 6) { // 토요일이면 줄바꿈
            System.out.println();
        }
    }
    System.out.println();
}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment