Skip to content

Instantly share code, notes, and snippets.

@rorrorome
Last active July 7, 2023 17:04
Show Gist options
  • Select an option

  • Save rorrorome/a1db4ac6d86beafc00bac4b23fcd01cc to your computer and use it in GitHub Desktop.

Select an option

Save rorrorome/a1db4ac6d86beafc00bac4b23fcd01cc to your computer and use it in GitHub Desktop.
달력
/*
* 제로베이스 백엔드 스쿨 15기
* 한새롬
* 미니과제 5. 달력 출력 프로그램
*
* Scanner의 입력함수와 조건문 및 반복문을 통한 달력 계산 로직 작성
* 1. 입력받은 년도와 월을 통한 달력 생성
* 2. 입력값은 년도, 월을 입력
* 3. 날짜는 LocalDate클래스를 이용(Calendar와 Date클래스도 이용 가능)
*/
//Scanner입력함수, LocalDate클래스 이용
import java.util.Scanner;
import java.time.LocalDate;
import java.time.DayOfWeek;
public class MiniProject05 {
public static void main(String[] args) {
// 년,월 입력
Scanner sc = new Scanner(System.in);
System.out.println("[달력 출력 프로그램]");
System.out.print("달력의 년도를 입력해 주세요.(yyyy): ");
int year = sc.nextInt();
System.out.print("달력의 월을 입력해 주세요.(mm): ");
int month = sc.nextInt();
// 해당 월 첫째날의 요일과 마지막 날짜(월의 길이) 구하기
LocalDate date = LocalDate.of(year, month, 01);
DayOfWeek fdw = date.getDayOfWeek();
int fdwn = fdw.getValue();
int ld = date.lengthOfMonth();
System.out.println();
System.out.println();
System.out.printf("[%d년 %02d월]", year, month);
System.out.println();
System.out.printf("일\t월\t화\t수\t목\t금\t토\n");
// 첫째날 맞는 요일에 배치
if (fdwn != 7) {
for (int i = 0; i < fdwn; i++) {
System.out.print("\t");
}
System.out.printf("%02d", 1);
} else {
System.out.printf("%02d", 1);
}
System.out.printf("\t");
// 첫째날이 토요일이면 개행 하고 시작
if (fdwn == 6) {
System.out.println();
}
// 둘째날부터 나열, 토요일마다 개행
for (int j = 2; j <= ld; j++) {
System.out.printf("%02d\t", j);
++fdwn;
if (fdwn % 7 == 6) {
System.out.println();
} else {
continue;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment