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);
}
}
}
'Java > JAVA8' 카테고리의 다른 글
[자바] 스레드를 이용해서 3개의 cpu이용하기 (0) | 2021.07.01 |
---|---|
[자바]earnings()와 toString()메소드 완성시키기 (0) | 2021.07.01 |
[자바] extends를 이용한 자바 문장 (0) | 2021.07.01 |
[자바] 짝수 패리티(even parity) (0) | 2021.07.01 |
[자바]2차 방정식의 실수해 (0) | 2021.07.01 |