Created
November 7, 2023 16:13
-
-
Save ramyo564/d17facac8992a8b2264f3a6f8fd8921f 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 String makeCalendar(int year, int month, int maxLines) { | |
int date = 1; // 일 | |
int day; // 요일 | |
LocalDate firstDate = LocalDate.of(year, month, date); // 해당월 1일 설정 | |
day = firstDate.getDayOfWeek().getValue(); | |
StringBuilder calendarString = new StringBuilder(); | |
calendarString.append(month >= 10 ? | |
String.format("[%d년 %d월]\t\t\t\t\n", year, month) : String.format("[%d년 %02d월]\t\t\t\t\n", year, month)); | |
calendarString.append("일\t월\t화\t수\t목\t금\t토\t\n"); | |
for (int i = 1; i <= 6; i++) { | |
for (int j = 0; j < 7; j++) { | |
if (i == 1 && j < day) { | |
calendarString.append("\t"); | |
} else if (date <= firstDate.lengthOfMonth()) { | |
calendarString.append(String.format("%02d\t", date++)); | |
} else { | |
calendarString.append("\t"); | |
} | |
} | |
calendarString.append("\n"); | |
} | |
return calendarString.toString(); | |
} | |
public static void main(String[] args) { | |
int year; | |
int month; | |
Scanner scanner = new Scanner(System.in); | |
while (true) { | |
System.out.println("[달력 출력 프로그램]"); | |
System.out.print("달력의 년도를 입력해 주세요. (yyyy) : "); | |
year = scanner.nextInt(); | |
if (year < 0) { | |
System.out.println("년도 입력이 잘못되었습니다. 다시 시작합니다.\n"); | |
continue; | |
} | |
System.out.print("달력의 월을 입력해 주세요. (mm) : "); | |
month = scanner.nextInt(); | |
if (month <= 0 || month > 12) { | |
System.out.println("월 입력이 잘못되었습니다. 다시 시작합니다.\n"); | |
continue; | |
} | |
break; | |
} | |
System.out.println(); | |
int lastMonth = month - 1; | |
int lastYear = year; | |
int nextMonth = month +1 ; | |
int nextYear = year; | |
if (lastMonth == 0) { | |
lastMonth = 12; | |
lastYear--; | |
nextYear = year; | |
nextMonth = month +1; | |
} else if (nextMonth == 13) { | |
nextMonth = 1; | |
nextYear ++; | |
lastMonth = month -1; | |
lastYear = year; | |
} | |
String calendarLast = makeCalendar(lastYear, lastMonth, 6); | |
String calendarCurrent = makeCalendar(year, month, 6); | |
String calendarNext = makeCalendar(nextYear, nextMonth, 6); | |
String[] linesLast = calendarLast.split("\n"); | |
String[] linesCurrent = calendarCurrent.split("\n"); | |
String[] linesNext = calendarNext.split("\n"); | |
int maxLength = Math.max(Math.max(linesLast.length, linesCurrent.length), linesNext.length); | |
for (int i = 0; i < maxLength; i++) { | |
String lastLine = i < linesLast.length ? linesLast[i] : ""; | |
String currentLine = i < linesCurrent.length ? linesCurrent[i] : ""; | |
String nextLine = i < linesNext.length ? linesNext[i] : ""; | |
System.out.println(lastLine + "\t" + currentLine + "\t" + nextLine); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment