package day13;
public class MyDate {
private int year,month,day;
private static int thisYear=2016 ;
public MyDate () {
this (0 ,0 ,0 );
}
public MyDate (MyDate d) {
this (d.year,d.day,d.month);
}
public MyDate (int year,int month,int day) {
set(year, month, day);
}
public static int getThisYear (){
return thisYear;
}
public static void setThisYear (int year){
thisYear=year;
}
public void set (int year,int month,int day){
this .setYear(year);
setMonth(month);
setDay(day);
}
public int getYear () {
return year;
}
public void setYear (int year) {
if (thisYear<-5000 ){
this .year = -5000 ;
}
this .year = year;
}
public int getMonth () {
return month;
}
public void setMonth (int month) {
if (month>=1 &&month<=12 ){
this .month = month;
}else {
this .month = -1 ;
}
}
public int getDay () {
return day;
}
public void setDay (int day) {
if (day>=1 &&day<=daysOfMonth()){
this .day = day;
}else {
this .day = -1 ;
}
}
public boolean isLeapYear (){
return isLeapYear(this .year);
}
public static boolean isLeapYear (int year){
return year%400 ==0 ||year%4 ==0 &&year%100 !=0 ;
}
public int daysOfMonth (){
switch (this .month){
case 1 :case 3 :case 5 :case 7 :case 8 :case 10 :case 12 : return 31 ;
case 4 :case 6 :case 9 :case 11 :return 30 ;
case 2 :return isLeapYear()?29 :28 ;
default :return -1 ;
}
}
public void tomorrow (){
int day=daysOfMonth();
this .day++;
if (this .day>day){
this .day=1 ;
this .month++;
if (this .month>12 ){
this .month=1 ;
this .year++;
}
}
}
public MyDate yesterday () {
MyDate d=new MyDate(this );
d.day--;
if (d.day<1 ){
d.month--;
if (d.month<1 ){
d.setYear(--d.year);
d.month=12 ;
}
d.day=d.daysOfMonth();
}
return d;
}
@Override
public String toString () {
return "year=" + year + ",month=" + month + ",day=" + day;
}
}
package day13;
public class MyDateEx {
public static void main (String[] args) {
MyDate d1=new MyDate(2016 ,12 ,31 );
System.out .println(d1.toString());
d1.tomorrow();
System.out .println(d1.toString());
MyDate d=d1.yesterday();
System.out .println(d.toString());
System.out .println(d1.getThisYear());
System.out .println(d.getThisYear());
System.out .println(MyDate.getThisYear());
MyDate.setThisYear(2007 );
System.out .println(MyDate.getThisYear());
}
}