자바 프로그래밍/코딩
DateTest
by 세인트킴
2023. 6. 8.
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 void increase() {
day++;
int[] daysPerMonth = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (day > daysPerMonth[month]) {
day = 1;
month++;
if (month > 12) {
month = 1;
year++;
}
if (month == 2 && day == 29 && (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))) {
day = 29;
}
}
}
public String toString() {
return month + "/" + day + "/" + year;
}
}
public class DateTest {
public static void main(String[] args) {
Date date1 = new Date(3, 1, 2018);
Date date2 = new Date(1, 1, 2020);
System.out.println("Entrance date: " + date1);
System.out.println("date2: " + date2);
for(int i=0; i<700; i++) {
date2.increase();
System.out.println(date2);
}
}
}