Skip to content

Instantly share code, notes, and snippets.

@kingseungil
Last active August 2, 2023 02:35
Show Gist options
  • Save kingseungil/b70eea79adcb73ea2be7faeee962f524 to your computer and use it in GitHub Desktop.
Save kingseungil/b70eea79adcb73ea2be7faeee962f524 to your computer and use it in GitHub Desktop.
Zerobase-miniproject
import java.time.LocalDate;
import java.util.Scanner;
public class Miniproject5 {
public static void main(String[] args) {
System.out.println("[달력 출력 프로그램]");
try (Scanner sc = new Scanner(System.in)) { // try-with-resources 문으로 Scanner 객체 자동 닫기
System.out.print("달력의 년도를 입력해 주세요.(yyyy):");
int year = sc.nextInt();
System.out.print("달력의 월을 입력해 주세요.(mm):");
int month = sc.nextInt();
System.out.println();
System.out.format("[%d년 %02d월]\n", year, month);
System.out.println("일\t월\t화\t수\t목\t금\t토");
GenerateCalendar(year, month);
}
}
private static void GenerateCalendar(int year, int month) {
LocalDate date = LocalDate.of(year, month, 1); // LocalDate 객체 생성
int startDay = date.getDayOfWeek().getValue(); // 일 : 7, 월 : 1, 화 : 2, 수 : 3, 목 : 4, 금 : 5, 토 : 6
startDay = startDay % 7 + 1; // 일 : 1, 월 : 2, 화 : 3, 수 : 4, 목 : 5, 금 : 6, 토 : 7
int lastDay = date.lengthOfMonth(); // 월의 마지막 날짜 구하기
for (int i = 1; i < startDay; i++) {
System.out.print("\t");
}
for (int i = 1; i <= lastDay; i++) {
System.out.format("%02d\t", i);
if (startDay % 7 == 0) {
System.out.println();
}
startDay++;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment