完善例题3.2 日期类 MyDate

public class MyDate 
{
  private int year,month,day;
  private static int thisYear;
  static 
  {
   thisYear=2012;
  }
  public MyDate(int year,int month,int day)
  {
   this.set(year,month,day);
  }
  public MyDate()
  {
   this(1970,1,1);
  }
  public MyDate(MyDate d)
  {
   this.set(d);
  }
  public void set(int year,int month,int day)
  {
   this.year=year;
   this.month=(month>=1&&month<=12)?month:1;
   this.day=(day>=1&&day<=31)?day:1;
  }
  public void set(MyDate d)
  {
   set(d.year,d.month,d.day);
  }
  public int getYear()
  {
   return this.year;
  }
  public int getMonth()
  {
   return this.month;
  }
  public int getDay(){
   return this.day;
  }
  public String toString(){
   return year+"年"+String.format("%02d",month)+"月"+String.format("%02d",day)+"日";
  }
  public static int getThisYear()
  {
   return thisYear;
  }
  public static boolean isLeapYear(int year)
  {
   return year%400==0||year%100!=0&&year%4==0;
  }
  public boolean isLeapYear()
  {
   return isLeapYear(this.year);
  }
  public boolean equals(MyDate d)
  {
   return this==d||d!=null&&this.year==d.year&& this.month==d.month &&this.day==d.day;
  }
  public static int daysOfMonth(int year,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: return MyDate.isLeapYear(year)?29:28;
    default: return 0;
   }
  }
  public int daysofMonth()
  {
   return daysOfMonth(this.year,this.month);
  }
  public void tomorrow()
  {
   this.day++;
   if(this.day>this.daysofMonth())
   {
    this.day=1;
    this.month++;
    if(this.month>12)
    {
     this.month=1;
     this.year++;
    }
   }
  }
  public MyDate yestoday()
  {
   MyDate date=new MyDate(this);
   date.day--;
   if(date.day==0)
   {
    date.month--;
    if(date.month==0)
    {
     date.month=12;
     date.year--;
    }
    date.day=daysOfMonth(date.year,date.month);
   }
   return date;
  }
}
class MyDate_ex
{
 public static void main(String args[])
 {
  System.out.println("今年是"+MyDate.getThisYear()+",闰年?"+MyDate.isLeapYear(MyDate.getThisYear()));
  MyDate d1=new MyDate(2012,12,31);
  MyDate d2=new MyDate(d1);
  System.out.println("d1: "+d1+",d2: "+d2+",d1==d2? "+(d1==d2)+", d1.equals(d2)? "+d1.equals(d2));
  System.out.print(d1+"的明天是       ");
  d1.tomorrow();
  System.out.println(d1+"\n"+d1+"的昨天是    "+(d2=d1.yestoday()));
 }
}

转载于:https://www.cnblogs.com/dx20152111104/p/6728964.html

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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.撰写上机报告
好的,我了解了您的问题。让我来回答您的问题。 MyDate 是一个日期类,它可以存储年、月、日的信息,并且能够对日期进行一些操作。下面是完善后的 MyDate 类的代码示: ```java public class MyDate { private int year; private int month; private int day; public MyDate(int year, int month, int day) { this.year = year; this.month = month; this.day = day; } public int getYear() { return year; } public int getMonth() { return month; } public int getDay() { return day; } public void setYear(int year) { this.year = year; } public void setMonth(int month) { this.month = month; } public void setDay(int day) { this.day = day; } public void setDate(int year, int month, int day) { this.year = year; this.month = month; this.day = day; } public String toString() { return String.format("%04d-%02d-%02d", year, month, day); } public boolean equals(Object obj) { if (obj instanceof MyDate) { MyDate date = (MyDate) obj; return year == date.year && month == date.month && day == date.day; } return false; } public int hashCode() { final int prime = 31; int result = 1; result = prime * result + day; result = prime * result + month; result = prime * result + year; return result; } public MyDate nextDay() { int nextDay = day + 1; int nextMonth = month; int nextYear = year; if (nextDay > getDaysInMonth(year, month)) { nextDay = 1; nextMonth++; if (nextMonth > 12) { nextMonth = 1; nextYear++; } } return new MyDate(nextYear, nextMonth, nextDay); } public MyDate nextMonth() { int nextMonth = month + 1; int nextYear = year; if (nextMonth > 12) { nextMonth = 1; nextYear++; } int maxDay = getDaysInMonth(nextYear, nextMonth); int nextDay = Math.min(day, maxDay); return new MyDate(nextYear, nextMonth, nextDay); } public MyDate nextYear() { int nextYear = year + 1; int maxDay = getDaysInMonth(nextYear, month); int nextDay = Math.min(day, maxDay); return new MyDate(nextYear, month, nextDay); } private int getDaysInMonth(int year, 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 (isLeapYear(year)) { return 29; } else { return 28; } default: throw new IllegalArgumentException("Invalid month: " + month); } } private boolean isLeapYear(int year) { if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) { return true; } else { return false; } } } ``` 在这个类中,我们实现了以下方法: - 构造方法 MyDate(int year, int month, int day):用于创建一个日期对象。 - getYear()、getMonth()、getDay():用于获取年、月、日。 - setYear()、setMonth()、setDay():用于设置年、月、日。 - setDate(int year, int month, int day):用于设置年、月、日。 - toString():用于以字符串形式返回当前日期。 - equals(Object obj):用于判断当前日期是否与另一个日期相等。 - hashCode():用于生成当前日期的哈希码。 - nextDay()、nextMonth()、nextYear():用于获取当前日期的下一天、下一个月、下一年。 - getDaysInMonth(int year, int month):用于获取指定年月的天数。 - isLeapYear(int year):用于判断指定年是否为闰年。 这些方法可以帮助我们对日期进行各种操作,比如获取下一天的日期、判断两个日期是否相等等等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值