这次我们要做一个日期类Date类

主要目的是    1.  熟悉-->构造不同参数的函数  2.善于利用已有的函数!!

题目要求:

Date类要求

可设定年月日

可转换为字符串,并可指定分隔符,默认为’-’

可获取若干天以前/后的日期

可计算与某个日期相隔的天数,日期的形式可能是日期对象,也可能是代表年月日的三个整数

可与某个日期比较大小,日期的形式同上

可计算星期几

解题思路

1.设置类的变量,并创建构造函数。

2.创建 日期转换字符串函数,  分别创建带参数,和不带参数的函数

3.计算若干天以前/后的日期,先把日期转换为天数,进行加减,在吧天数转换为日期

4.计算某个提起相隔的天数,可以把日期转换为天数,加减判断。  日期形式两种,这里就需要,多个构造函数(根据要求设置参数)

5.比较日期大小,  转化为天数比较

6.计算星期几,  天数%7的余数,就是星期数 。我们发现周日为0,这个时候就需要(天数-1)%7 +1  ,这样就避免了上述情况

 

java自身有Date函数,不过这次我们要自己创建。JAVA中的月份,星期几的表示方式有产别 (正常表示-1,因为是从0开始)

 

程序代码

  1 public class Date {
  2     public   int year;
  3     public   int month;
  4     public   int day;
  5     public   static String splitChar = "_";
  6 
  7 
  8     /**
  9      * 比较两个日期大小
 10      * @param date 拿来比较的日期
 11      * @return   0-等于  1-大于  -1-小于
 12      */
 13     public byte compare(Date date) {
 14         int n = off(date);
 15         // 第一种判断方法
 16         /*if (0==n){
 17             return 0;
 18         }
 19         if(n > 0){
 20             return  1;
 21         }
 22         return  -1;*/
 23 
 24         //第二种判断方法
 25         return (byte)(0 == n?0:(n>0?1:-1));
 26     }
 27 
 28     /**
 29      * 计算与某个日期相隔的天数
 30      * @param year  年  20xx形式
 31      * @param month 月
 32      * @param day   日
 33      * @return  相隔的天数
 34      */
 35     public int off(int year, int month, int day) {
 36         Date date = new Date(year,month,day);
 37         return off(date);
 38     }
 39 
 40     /**
 41      * 计算与某个日期相隔的天数
 42      * @param date 相隔的日期 20xx-xx-xx形式
 43      * @return 相隔的天数
 44      */
 45     public int off(Date date) {
 46         return this.getDays()-date.getDays();
 47     }
 48 
 49     /**
 50      * 获取若干天以前的日期
 51      * @param days 天数
 52      * @return 改变后的日期
 53      */
 54     public Date before(int days) {
 55         int n = this.getDays() - days;
 56 
 57         return new Date(n);
 58     }
 59 
 60     /**
 61      * 获取若干天以后的日期
 62      * @param days 天数
 63      * @return 改变后的日期
 64      */
 65     public Date after(int days) {
 66         int n = this.getDays() + days;
 67 
 68         return new Date(n);
 69     }
 70 
 71     /**
 72      * 计算当前日期为周几?
 73      * @return 星期几
 74      */
 75     public int weekday() {
 76         int days = getDays();
 77         return (byte)((days-1)%7+1);
 78     }
 79 
 80     /**
 81      * 将日期转换为字符
 82      * @return 日期字符串形式 2014-12-08
 83      */
 84     public String toStr() {
 85         return toStr("-");
 86     }
 87 
 88     /**
 89      * 将日期转换为字符串
 90      * @param splitChar 分割字符
 91      * @return 日期字符串
 92      */
 93     public String toStr(String splitChar) {
 94         return String.format("%d%s%d%s%d",this.year,splitChar,this.month,splitChar,this.day);
 95     }
 96 
 97     /**
 98      * 构造函数
 99      * @param year    年
100      * @param month   月
101      * @param day     日
102      */
103     public Date(int year, int month, int day) {
104         this.year = year;
105         this.month = month;
106         this.day = day;
107     }
108 
109     /**
110      * 计算当前日期是历史上的第几天
111      * @return 天数
112      */
113     public int getDays(){
114         int sum = day;
115         int monthday;
116         switch (month - 1 ){
117             case 12 :
118                 monthday = 30;
119                 sum += monthday;
120             case  11:
121                 monthday = 30;
122                 sum += monthday;
123             case  10:
124                 monthday = 31;
125                 sum += monthday;
126             case  9:
127                 monthday = 30;
128                 sum += monthday;
129             case  8:
130                 monthday = 31;
131                 sum += monthday;
132             case  7:
133                 monthday = 31;
134                 sum += monthday;
135             case  6:
136                 monthday = 30;
137                 sum += monthday;
138             case  5:
139                 monthday = 31;
140                 sum += monthday;
141             case  4:
142                 monthday = 30;
143                 sum += monthday;
144             case  3:
145                 monthday = 31;
146                 sum += monthday;
147             case  2:
148                 monthday = isLeap(year)?29:28;
149                 sum += monthday;
150             case  1:
151                 monthday = 31;
152                 sum += monthday;
153         }
154 
155         for (int year = this.year-1;year>=1;year-- ){
156             sum += isLeap(year)?366:365;
157         }
158         return sum;
159     }
160 
161     /**
162      * 构造函数
163      * @param days 天数
164      */
165     public Date(int days){
166         /*
167         思路:
168         1.确定年份,逐年减去该年的天数,直到不够减
169         2.确定月份,逐月减去该月的天数,直到不够减
170         3.剩下的天数即为日
171          */
172         year = 1;
173         while (true){
174             int n = isLeap(year)?366:365; //该年的天数
175             if(days < n){
176                 break;
177             }
178             days -= n;
179             year++;
180         }
181 
182         int []mon = {31,28,31,30,31,30,31,31,30,31,30,31};
183         for (month=1;month<=12;month++){
184             // 该月的天数
185             int monthDay = mon[month-1];
186             if (2==month&&isLeap(year)){
187                 monthDay ++;
188             }
189             if(days<monthDay){
190                 break;
191             }
192             days -= monthDay;
193         }
194         day = days;
195     }
196 
197     /**
198      * 判断闰年
199      * @param year
200      * @return true-闰年   false-平年
201      */
202     public boolean isLeap(int year){
203         return year%400==0||(year%4==0&&year%100!=0);
204     }
205 
206 }

 

测试类Test

 1 public class Test {
 2     public static void main(String[] args) {
 3 
 4         Date date1 = new Date(2016,11,2);     //设置日期1   2016-11-2
 5         Date date2 = new Date(2017, 3,12);    // 设置日期2  2017-3-12
 6 
 7         System.out.printf("%s是星期%d\n",date1.toStr(),date1.weekday());    // 计算日期1是星期几
 8  9         System.out.printf("%s是星期%d\n",date2.toStr(Date.splitChar),date2.weekday());  //计算日期2是星期几
10 11         //转换为字符串,指定分隔符
12 
13         Date date3 = date1.after(50);         // 日期3为,  日期1后50天
14         Date date4 = date1.before(50);     // 日期4为, 日期2前50天
15 
16         int off1 = date3.off(date4);          // 计算日期3 和 日期4相差多少天
17         System.out.printf("%s和%s相隔%d天\n",date3.toStr(),date4.toStr(),off1);
18 19 
20         int off2 = date3.off(2017,2,21);      // 计算日期3 和 2017-2-21 相差多少天
21         System.out.printf("%s和2017-02-21相隔%d天\n",date3.toStr(),off2);
22 23 
24         byte result = date3.compare(date4);   // 比较日期3 和 日期4 的大小关系
25         String relation =  "";
26         switch (result){
27             case  0: relation = "等于" ; break;
28             case  1: relation = "大于" ; break;
29             case -1: relation = "小于" ; break;
30         }
31         System.out.printf("%s%s%s\n",date3.toStr(),relation,date4.toStr());
32 
33         Date d = new Date(800);                 计算公元第800天,是什么日期
34         System.out.println("今天是:"+d.toStr());
35 
36         System.out.printf("今天是公元的第%d天\n",date3.getDays());  //输出日期3是公元的第几天
37 38     }
39 }

运行结果

心得体会

我们要善于运用 带参函数,这样同样功能的函数带不同参数时,解决不同的问题。

比如说计算两个日期多少天  1你可以参数设置为日期,直接与日期进行比较  2.你可以设置为具体的年月日,进行比较。

使用时很方便。