Skip to content

Instantly share code, notes, and snippets.

@Exom9434
Created September 1, 2024 04:05
Show Gist options
  • Save Exom9434/b58a20c8f8c887127e51066e5be62b07 to your computer and use it in GitHub Desktop.
Save Exom9434/b58a20c8f8c887127e51066e5be62b07 to your computer and use it in GitHub Desktop.
//노재경
import java.time.LocalDate;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int year = 0;
int month = 0;
Scanner scanner = new Scanner(System.in);
boolean yearCheck = true;
boolean monthCheck = true;
// 사용자로부터 년도와 월을 입력 받음
while(yearCheck) {
System.out.println("[달력 출력 프로그램]");
System.out.print("달력의 년도를 입력해 주세요.(yyyy): ");
year = scanner.nextInt();
if(year < 0) {
System.out.println("연도는 0 이상이어야 합니다");
} else {
yearCheck = false;
}
}
while(monthCheck) {
System.out.print("달력의 월을 입력해 주세요.(mm): ");
month = scanner.nextInt();
if(month < 1 || month > 12) {
System.out.println("1부터 12까지의 정수를 입력해주세요");
} else {
monthCheck = false;
}
}
printCalendar(year, month);
scanner.close();
}
public static void printCalendar(int year, int month) {
// 지난달 달력 출력
int lastYear;
int lastMonth;
int lastDate = 1;
int lastDay;
if(month == 1) {
lastYear = year - 1;
lastMonth = 12;
} else {
lastYear = year;
lastMonth = month - 1;
}
LocalDate lastFirstDate = LocalDate.of(lastYear, lastMonth, lastDate); // 날짜 가져오기
lastDay = lastFirstDate.getDayOfWeek().getValue(); // 요일 가져오기
System.out.printf("\n[%d년\t%02d월]\n", lastYear, lastMonth);
System.out.println("일\t월\t화\t수\t목\t금\t토\t");
for(int i = 0; i < lastDay % 7; i++) {
System.out.print("\t");
}
for(int i = 1; i <= lastFirstDate.lengthOfMonth(); i++) {
System.out.printf("%02d\t", lastDate++);
lastDay++;
if(lastDay % 7 == 0) {
System.out.println();
}
}
// 이번달 달력 출력
int date = 1;
int day;
LocalDate firstDate = LocalDate.of(year, month, date);
day = firstDate.getDayOfWeek().getValue(); // 요일 값
System.out.printf("\n[%d년\t%02d월]\n", year, month);
System.out.println("일\t월\t화\t수\t목\t금\t토\t");
for(int i = 0; i < day % 7; i++) {
System.out.print("\t");
}
for(int i = 1; i <= firstDate.lengthOfMonth(); i++) {
System.out.printf("%02d\t", date++);
day++;
if(day % 7 == 0) {
System.out.println();
}
}
// 다음달 달력 출력
int nextYear;
int nextMonth;
int nextDate = 1;
int nextDay;
if(month == 12) {
nextYear = year + 1;
nextMonth = 1;
} else {
nextYear = year;
nextMonth = month + 1;
}
LocalDate nextFirstDate = LocalDate.of(nextYear, nextMonth, nextDate);
nextDay = nextFirstDate.getDayOfWeek().getValue();
System.out.printf("\n[%d년\t%02d월]\n", nextYear, nextMonth);
System.out.println("일\t월\t화\t수\t목\t금\t토\t");
for(int i = 0; i < nextDay % 7; i++) {
System.out.print("\t");
}
for(int i = 1; i <= nextFirstDate.lengthOfMonth(); i++) {
System.out.printf("%02d\t", nextDate++);
nextDay++;
if(nextDay % 7 == 0) {
System.out.println();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment