码迷,mamicode.com
首页 > 其他好文 > 详细

完善 3.2 data 类

时间:2017-04-27 23:19:24      阅读:230      评论:0      收藏:0      [点我收藏+]

标签:set   r++   switch   get   rgs   bsp   oda   oid   form   

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()));
 }
}

完善 3.2 data 类

标签:set   r++   switch   get   rgs   bsp   oda   oid   form   

原文地址:http://www.cnblogs.com/sdn1229/p/6777576.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!