-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDate8.java
33 lines (33 loc) · 1.15 KB
/
Date8.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
public class Date8 {
private int month;
private int day;
private int year;
private static final int[] daysPerMonth = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
public Date8(int day, int month, int year){
if (month <= 0 || month > 12)
throw new IllegalArgumentException( "month (" + month + ") must be 1-12");
if (day <= 0 || (day > daysPerMonth[month] && !(month == 2 && day == 29)))
throw new IllegalArgumentException("day (" + day + ") out-of-range for the specified month and year");
if (month == 2 && day == 29 && !(year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)))
throw new IllegalArgumentException("day (" + day + ") out-of-range for the specified month and year");
this.month = month;
this.day = day;
this.year = year;
System.out.printf("Date object constructor for date %s%n", this);
}
public String toString() {
return String.format("%d/%d/%d", day,month, year);
}
public void nextday(){
day +=1;
if (day >daysPerMonth[month]){
day= 1;
month+=1;
if(month>12){
month=1;
year+=1;
}
}
System.out.printf("after incrementation of OF day the new date is %s%n",this);
}
}