资源简介
1.编写类MyDate.java
2.该类有如下构造方法
2.1 无参数构造方法public MyDate(),以当前的系统时间构造MyDate对象
2.2 public MyDate(int year, int month, int day), 以指定的年月日构造MyDate对象
3.该类有如下属性
3.1 private int year ;//年
3.2 private int month; //月
3.3 private int day; //日
4.该类有如下方法
4.1 public String after(int day); //返回当前对象代表的日期之后day天的日期,比如当前对象是2008-8-8,调用after(5)以后,应该返回2008-8-13,格式可自定义
4.2 public String before(int day); //返回当前对象代表的日期之前day天的日期,比如当前对象是2008-8-8,调用before(5)以后,应该返回2008-8-3,格式可自定义
4.3 public void setYear(int year); //设置年为指定值year
4.4 public void setMonth(int month); //设置月为指定值month
4.5 public void setDay(int day); //设置日为指定值day
4.6 public int getYear(); //返回当前对象的年
4.7 public int getMonth(); //返回当前对象的月
4.8 public int getDay(); //返回当前对象的日
4.9 public void set (int year, int month, int day); //设置年、月、日为指定的值year、month、day
4.10 public String toString();//以字符串形式返回当前对象的年月日,例如2008年08月08日,格式可自定义
4.11 public boolean equals(MyDate mydate);//当前对象与另一个对象比较,如果两个对象的年月日均相等,则返回true,反之返回false
5.编写TestMyDate.java,在main方法中对MyDate类的各个方法进行测试
6.按照编程规范为MyDate.java编写规范的代码
7.按照java doc API的要求,对代码编写规范的注释,然后利用javadoc.exe命令生成MyDate.java的API doc
8.撰写上机报告
代码片段和文件信息
package cn.tx.file;
public class MyDate {
/**
*Class attributes year month day
*/
private int year;
private int month;
private int day;
/**Return to the number of days per month*/
public int dayOfMonth(int month) {
switch (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:
if ((year % 4 == 0 && year % 100 != 0)||(year % 100== 0))
return 29;
else
return 28;
}
System.out.println(“error month!“);
return -1;
}
//The total number of days calculated for a date
public int daysOfDate(MyDate date)
{
int y=date.getYear();
int m=date.getMonth();
int d=date.getDay();
if (y < 1)
System.out.println(“error year!“);
else if (m < 1 || m > 12)
System.out.println(“error month!“);
else if (d < 1 || d > dayOfMonth(m))
System.out.println(“error day!“);
else {
//Four years one leap a hundred years not to leap four hundred years to leap again
int difdays=365*(y-1)+((y-1)/4-(y-1)/100+(y-1)/400);
for(int i=1;i difdays+=dayOfMonth(i);
difdays+=d;
return difdays;
}
return -1;
}
/**The specified MyDate object for the year and the month*/
public MyDate(int y int m int d) {
if (y < 1)
System.out.println(“error year!“);
else if (m < 1 || m > 12)
System.out.println(“error month!“);
else if (d < 1 || d > dayOfMonth(m))
System.out.println(“error day!“);
else {
year = y;
month = m;
day = d;
}
}
/**Constructing MyDate objects with current system time*/
public MyDate() {
int addDay=0;
int leapYearDays=366;
int commonYearDays=365;
int leftDays=0;
int startYear=1970;
int sumMonth=0;
int i=1;
//Get the number of milliseconds from 1970 to date
long totalMilliseconds = System.currentTimeMillis();
//Convert the number of milliseconds into a number of days
int totalDays=(int)((totalMilliseconds)/1000/(3600*24))+1;
//Current date algorithm
while(addDay
if(totalDays-addDay<366){
while(leftDays
leftDays+=dayOfMonth(i);
i++;
sumMonth++;
}
month=sumMonth%12;
day=totalDays-addDay-(leftDays-dayOfMonth(i-1));
}
if((startYear % 4 == 0 && startYear % 100 != 0)||(startYear % 100== 0)){
addDay+=leapYearDays;
sumMonth+=12;
}else{
addDay+=commonYearDays;
sumMonth+=12;
}
startYear++;
year=startYear-1;
}
}
/**Compare two dates*/
public boolean equals(MyDate date)
{
int days1=daysOfDate(this);
int days2=daysOfDate(date);
return (days1==days2)? true:false;
}
/**Set the year as the specified value year*/
public void setYear(int year)
{
if (year < 1)
System.out.println(“error year!“);
else
this.year=year;
}
/**Return to the year of the current ob
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 4630 2018-04-22 20:12 编写类MyDate\MyDate.java
文件 1889 2018-04-22 20:13 编写类MyDate\TestMyDate.java
文件 133 2018-09-03 14:04 编写类MyDate\使用方法.txt
文件 178369 2018-09-03 13:47 编写类MyDate\实验报告.docx
目录 0 2018-09-03 14:02 编写类MyDate\
评论
共有 条评论