Last active
January 15, 2024 08:02
-
-
Save Starlight258/7a4d08780241000904f197348705ea54 to your computer and use it in GitHub Desktop.
0114_과제5
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.ArrayList; | |
import java.util.Scanner; | |
public class a5 { | |
public static void main(String[] args) { | |
// 입력받기 | |
System.out.println("[달력 출력 프로그램]"); | |
Scanner sc = new Scanner(System.in); | |
System.out.print("달력의 년도를 입력해 주세요.(yyyy):"); | |
int year = sc.nextInt(); | |
System.out.print("달력의 월을 입력해 주세요.(mm):"); | |
int month = sc.nextInt(); | |
// 요일 배열 | |
ArrayList<String> day = new ArrayList<>(); | |
day.add("일"); | |
day.add("월"); | |
day.add("화"); | |
day.add("수"); | |
day.add("목"); | |
day.add("금"); | |
day.add("토"); | |
// 달 출력 | |
System.out.printf("[%d년 %02d월]", year, month - 1); | |
System.out.print(" "); | |
System.out.printf("[%d년 %02d월]", year, month); | |
System.out.print(" "); | |
System.out.printf("[%d년 %02d월]\n", year, month + 1); | |
// 요일 출력 | |
for (String s : day) { | |
System.out.print(s + "\t"); | |
} | |
System.out.print(" "); | |
for (String s : day) { | |
System.out.print(s + "\t"); | |
} | |
System.out.print(" "); | |
for (String s : day) { | |
System.out.print(s + "\t"); | |
} | |
System.out.println(" "); | |
// 날짜 계산 | |
LocalDate firstDate = LocalDate.of(year, month - 1, 1); | |
int firstDayOfWeekValue = firstDate.getDayOfWeek().getValue(); // 월요일 : 1, 일요일 : 7 | |
int firstMonthLength = firstDate.lengthOfMonth(); // 달의 길이 | |
int firstLastDayOfWeekValue = LocalDate.of(year, month - 1, firstMonthLength).getDayOfWeek().getValue(); | |
LocalDate middleDate = LocalDate.of(year, month, 1); | |
int middleDayOfWeekValue = middleDate.getDayOfWeek().getValue(); // 월요일 : 1, 일요일 : 7 | |
int middleMonthLength = middleDate.lengthOfMonth(); // 달의 길이 | |
int middleLastDayOfWeekValue = LocalDate.of(year, month, middleMonthLength).getDayOfWeek().getValue(); | |
LocalDate lastDate = LocalDate.of(year, month + 1, 1); | |
int lastDayOfWeekValue = lastDate.getDayOfWeek().getValue(); // 월요일 : 1, 일요일 : 7 | |
int lastMonthLength = lastDate.lengthOfMonth(); // 달의 길이 | |
int lastLastDayOfWeekValue = LocalDate.of(year, month + 1, lastMonthLength).getDayOfWeek().getValue(); | |
int firstDayValue = 1, middleDayValue = 1, lastDayValue = 1; | |
// 달력 출력 | |
while (true) { | |
if (lastDayValue > lastMonthLength) break; | |
// 이전 달 출력 | |
if (firstDayValue == 1) { // 첫 날일때 | |
for (int i = 0; i < firstDayOfWeekValue % 7; i++) { | |
System.out.print(" \t"); | |
} | |
} | |
while (firstDayValue <= firstMonthLength) { | |
System.out.printf("%02d\t", firstDayValue); | |
if ((firstDayValue + (firstDayOfWeekValue % 7)) % 7 == 0) { | |
firstDayValue++; | |
break; | |
} | |
firstDayValue++; | |
} | |
// 마지막 날일때 띄어쓰기 처리 | |
if ((firstDayValue - 1 == firstMonthLength) && (firstLastDayOfWeekValue != 6)) { | |
for (int i = 0; i < 6 - (firstLastDayOfWeekValue % 7); i++) { | |
System.out.print(" \t"); | |
} | |
} | |
System.out.print(" "); | |
// 이번 달 출력 | |
if (middleDayValue == 1) { | |
for (int i = 0; i < middleDayOfWeekValue % 7; i++) { | |
System.out.print(" \t"); | |
} | |
} | |
while (middleDayValue <= middleMonthLength) { | |
System.out.printf("%02d\t", middleDayValue); | |
if ((middleDayValue + (middleDayOfWeekValue % 7)) % 7 == 0) { | |
middleDayValue++; | |
break; | |
} | |
middleDayValue++; | |
} | |
// 마지막 날일때 띄어쓰기 처리 | |
if (middleDayValue - 1 == middleMonthLength && middleLastDayOfWeekValue != 6) { | |
for (int i = 0; i < 6 - (middleLastDayOfWeekValue % 7); i++) { | |
System.out.print(" \t"); | |
} | |
} | |
System.out.print(" "); | |
// 마지막 달 출력 | |
if (lastDayValue == 1) { | |
for (int i = 0; i < lastDayOfWeekValue % 7; i++) { | |
System.out.print(" \t"); | |
} | |
} | |
while (lastDayValue <= lastMonthLength) { | |
System.out.printf("%02d\t", lastDayValue); | |
if ((lastDayValue + (lastDayOfWeekValue % 7)) % 7 == 0) { | |
lastDayValue++; | |
break; | |
} | |
lastDayValue++; | |
} | |
if (lastDayValue - 1 == lastMonthLength && lastLastDayOfWeekValue != 6) { | |
for (int i = 0; i < 6 - (lastLastDayOfWeekValue % 7); i++) { | |
System.out.print(" \t"); | |
} | |
} | |
System.out.println(" "); | |
} | |
System.out.println(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment