자바 달력

2013. 8. 8. 14:13WEB/JSP(JAVA)



반응형

자바달력 2월 윤년을 계산해주고 첫번째 날의수 구하고자는 1일이 무슨요일인지 계산해서 뿌려준다. 소스는 아래를 참조

 

 

 

 

 

import java.util.*;

public class ggari_calendar {
	
	static int year = 0;
	static int month = 0;
	static int tempNal = 0;
	static int week = 0;
	static int i = 0;
	static Scanner sc = new Scanner(System.in);
	
	public static void main(String[] args) {
		int monthArray[] = {31,28,31,30,31,30,31,31,30,31,30,31};

		setYear();		//날짜 셋팅

		if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0){
			monthArray[1] = 29;      //윤년이 아님
		}else{
			monthArray[1] = 28;		//윤연 임
		}
		
		// 01년 01월 01일부터 일계산
		tempNal = (year - 1) * 365 + (year - 1) / 4 - (year - 1) / 100	+ (year - 1) / 400;
		
		for (i = 0; i < month - 1; i++){
			tempNal += monthArray[i];
		// 구하는달의 첫번째 날의 수
		tempNal += 1;
		// 구하고자 하는 달이 1일이 무슨요일인지
		week = tempNal % 7;
		
		}
		
		System.out.println("  월  화  수  목  금  토  일\n");
		for (i = 0; i < week; i++) {
			System.out.print("    ");
		}
		for (i = 1; i <= monthArray[month - 1]; i++) {
			System.out.printf("%4d", i);
			week++;
			if (week % 7 == 0){
				System.out.println("\n");
			}
		}
		if (week % 7 != 0){
			System.out.println();
		}
	}

	private static void setYear() {
		System.out.print("연도: ");
		year = sc.nextInt();
		if (year < 1) {
			System.out.println("연도를 재대로 입력해라");
			setYear();
			return;
		}
		setMonth();
	}

	private static void setMonth() {
		System.out.print("월: ");
		month = sc.nextInt();
		if (month < 1 || month > 12) {
			System.out.println("월수를 재대로 입력해라");
			setMonth();
			return;

		}
	}
}

반응형

'WEB > JSP(JAVA)' 카테고리의 다른 글

자바 toString() 오버라이드  (1) 2013.08.27
자바 형변환  (0) 2013.08.14
자바 소수 반올림  (0) 2013.08.07
자바format  (0) 2013.07.26
자바 랜덤함수 (Random)  (0) 2013.07.09