nextDay、beforeDay以及根据nextDay(beforeDay)求解几天后的日期,几天前的日期和两个日期之间的天数
实现代码:
1 package com.corejava.chap02; 2 3 public class Date { 4 private int year; 5 private int month; 6 private int day; 7 8 public Date(int year, int month, int day) { 9 this.year = year; 10 this.month = month; 11 this.day = day; 12 } 13 14 public void display(){ 15 System.out.println(year + "/" + month + "/" +day); 16 } 17 18 //isLeapYear 19 private boolean isLeapYear(){ 20 21 if(year % 400 == 0) return true; 22 else if(year%4 == 0 && year % 100 != 0) return true; 23 else return false; 24 } 25 //The max days of the month 26 private int MaxofMonth(int month){ 27 int max = 0; 28 switch(month){ 29 case 1: 30 case 3: 31 case 5: 32 case 7: 33 case 8: 34 case 10: 35 case 12: max = 31;break; 36 case 4: 37 case 6: 38 case 9: 39 case 11: max = 30;break; 40 case 2:if(isLeapYear()) max = 29;else max = 28;break; 41 default : System.out.println("Date error");// 42 } 43 return max; 44 } 45 46 public Date nextDay(){ 47 int nday = day +1; 48 int nmonth = month; 49 int nyear = year; 50 if (nday > MaxofMonth(month)){ 51 nmonth = month +1; 52 if (nmonth > 12){ 53 nyear += 1; 54 return new Date(nyear,1,1); 55 } else { 56 return new Date(year,nmonth,1); 57 } 58 } else { 59 return new Date(year,month,nday); 60 } 61 62 } 63 64 public Date afterDay(int n) { 65 /* 66 Date temp = new Date(year,month,day); 67 for (int i=0; i<n; i++){ 68 temp = temp.nextDay(); 69 } 70 return temp; 71 */ 72 73 Date temp = null; 74 75 if (n==0) temp = new Date(year,month,day); 76 else if (n<0) temp = afterDay(n+1).previousDay(); 77 else temp = afterDay(n-1).nextDay(); 78 return temp; 79 } 80 81 public Date previousDay(){ 82 int pday = day - 1; 83 int pmonth = month; 84 int pyear = year; 85 if(pday <= 0) { 86 pmonth = month - 1; 87 if(pmonth < 1){ 88 pyear = year - 1; 89 pmonth = 12; 90 } 91 pday = MaxofMonth(pmonth); 92 } 93 return new Date(pyear,pmonth,pday); 94 } 95 private int compare(Date date) { 96 if(date.year == this.year){ 97 if(date.month == this.month) { 98 if(date.day == this.day) return 0; // more true 99 else if(date.day > this.day) return 1; 100 else return -1; 101 } 102 else if(date.month > this.month) return 1; 103 else return -1; 104 } 105 else if(date.year > this.year) return 1; 106 else return -1; 107 } 108 public int sub(Date date) { 109 Date sdate = date; 110 int count = 0; 111 if(compare(date)==0); 112 else if(compare(date) == 1){ 113 for(;compare(sdate)==1;){ 114 count ++; 115 sdate = sdate.previousDay() ; 116 } 117 } else { 118 for(;compare(sdate)==-1;){ 119 count ++; 120 sdate = sdate.nextDay(); 121 } 122 } 123 return count; 124 } 125 }
验证代码:
1 package com.corejava.chap02; 2 3 public class TestDate{ 4 public static void main(String[] args){ 5 Date date = new Date(2012,12,30); 6 Date nextDate = date.nextDay(); 7 Date nextNextDay = nextDate.nextDay(); 8 9 System.out.println("------first Date------"); 10 date.display(); 11 System.out.println("------Next Day------"); 12 nextDate.display(); 13 System.out.println("------Next Next Day------"); 14 nextNextDay.display(); 15 16 System.out.println("------after 5 days------"); 17 date.afterDay(5).display(); 18 19 System.out.println("------previousday of 2013/1/1------"); 20 Date previous = nextDate.previousDay(); 21 previous.display(); 22 23 System.out.println("------before 5 days------"); 24 date.afterDay(-5).display(); 25 26 Date d = new Date(2012,12,29); 27 System.out.println("------Test sub()------"); 28 System.out.print("compare with :"); 29 d.display(); 30 System.out.println(date.sub(d)); 31 } 32 }