JAVA로 구현한 양력 달력
- 역학/달력 & 만세력 개발
- 2020. 6. 14.
JAVA로 구현한 양력 달력
네이버 달력이 1990년 1월부터 시작되는걸 참고.
JAVA 양력 달력소스
import java.util.Scanner;
// 양력 달력
class Cal01{
// 0, 1 , 2, 3 4 5 6
static char[] weeks = {'일','월','화','수','목','금','토'};
static int[][] days = {{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }, // 평년
{ 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }}; // 윤년
// 윤년인지 계산 (윤년 1, 평년 0)
static int isLeap(int year) {
return (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) ? 1 : 0 ;
}
static int firstDayWeek(int year, int month) {
int weeks = 0;
int leapSum = 0;
int yridx = isLeap(year); // 검색 년도 윤년여부에 따라 배열 행 결정
int date = 0;
for (int i = 1900 ; i < year ; i ++){ // 1900년도 기준으로
leapSum += isLeap(i); // 검색년도 이전년도까지 윤년수 총합을 구해서
} weeks = ( year - 1900 + leapSum) % 7 ; // 첫날짜 이동되는 위치1 계산
for (int i = 0; i < month-1; i++){ // 검색년도에서 검색월의 이전월까지
date += days[yridx][i]; // 날짜를 모두 합해서
} weeks += (date +1) % 7; // 첫날짜 이동되는 위치2 계산 + 위치1 합계산
return weeks % 7; // 요일위치 값으로 반환
}
static void Show_Calender(int year, int month) {
int yridx = isLeap(year); // 검색 년도가 윤년여부에 따라 배열 행결정
int lastday = days[yridx][month - 1]; // 해당 월의 마지막 날짜
int Fristweek = firstDayWeek(year,month); // 해당 년월의 첫 요일위치
System.out.printf(
"년도 : %d 월 : %d\t %s\t 첫 요일위치: %d (일:0 월:1 화:2 수:3 목:4 금:5 토:6) \n",
year,month, isLeap(year)==1 ? "<윤년>":"<평년>", Fristweek);
// 상단 요일표시 (일~토)
for (char Value : weeks) {
System.out.printf("%1$c\t", Value);
}System.out.println(); // 다 표기했으면 개행
// (첫날짜 요일위치-1) 만큼 칸 뛰우기
for (int i = 0; i < Fristweek ; i++) {
System.out.printf("\t");}
// 해당월 1일부터 마지막 날까지 숫자쓰기
for (int i = 1; i <= lastday; i++) {
System.out.printf("%d\t", i);
if ((Fristweek + i - 1) % 7 == 6) // 토요일이면
System.out.println(); // 개행
}
}
static void Scan_ERROR(){
System.out.printf("\n 올바른 값을 입력하세요");
System.exit(0);
}
public static void main(String[] args) {
int year = 0;
int month = 0;
Scanner stdin = new Scanner(System.in);
System.out.print("년도를 입력(1900년 이후) :");
year = stdin.nextInt();
if (year < 1900) {Scan_ERROR();}
System.out.print("월을 입력 (1~12월) : ");
month = stdin.nextInt();
if (month< 1 || month > 12) {Scan_ERROR();}
Show_Calender(year, month);
}
}
테스트
마무리
시작은 양력달력부터.
내용이 도움이 되셨거나 JustPost 블로거를 응원하고 싶으신 분은 아래 하트♥공감 버튼을 꾹 눌러주세요!
관심어린 댓글 은 주인장에게 힘이 됩니다. ! 감사합니다. 사랑합니다 ~♥
'역학 > 달력 & 만세력 개발' 카테고리의 다른 글
년도를 60갑자로 변환 - 자바스크립트 (0) | 2020.06.17 |
---|---|
엑셀 VBA로 양력 달력만들기(소스 공유) (0) | 2020.06.16 |