Java/JAVA8

[자바]DateTest 날짜 계산하기(윤년 계산도 포함)

달의요정루나 2021. 7. 1. 20:49

 

package 연습;

class Date{
	private int month;
	private int day;
	private int year;
	
    public Date(int month, int day, int year){
		this.month = checkMonth(month);
		this.year = year;
		this.day = checkDay(day);
	}
	private int checkMonth(int testMonth) {
		if (testMonth >= 1 && testMonth <=12) {
			return testMonth;
		}
		else {
			return 1;
		}
	}
	
    private int checkDay(int testDay) {
		int[] daysPerMonth = {0,31,28,31,30,31,30,31,31,30,31,30,31};
		if (testDay > 0 && testDay <= daysPerMonth[month]) {
			return testDay;
		}
		if (month==2 && testDay==29 && (year%400 == 0 || (year%4==0 && year%100 != 0))) {
			return testDay;
		}
		
        return 1;
	}

	public String toString(){
		return month + "/" + day + "/" + year;
	}
	
    public void increase() {
		day++;
		if (checkDay(day)==1) {
			day=1;
			month++;
			if (month > 12) {
				year++;
				month=1;
			}
		}
	}
}

public class DateTest {
	public static void main(String[] args) {
		Date date2 = new Date( 1, 1, 2020);
		System.out.println("date2: "+date2);
		for (int i = 0; i < 800; i++) {
			date2.increase();
			System.out.println(date2);
		}
	}
}