Created
May 31, 2024 09:25
-
-
Save Exom9434/a6f54c975aeee7537743a67b9c8ee107 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//노재경 | |
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