• 完善3.2例题


    package mydate;

    public class mydate {

    private int year,month,day;
    private static int thisYear;

    static
    {thisYear=2014;}
    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 equals(mydate d)
    { return this==d||d!=null&&this.day==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(2014,12,11);
    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+" "+d1+"的昨天是"+(d2=d1.yestoday()));
    }

    }

  • 相关阅读:
    Java中的transient关键字
    【笔记】html的改变(上)
    《开发板 — 实现看门狗》
    《头文件导致Symbol xxx multiply defined重复定义问题分析和解决》
    《APP读取按键值》
    《补充 — Linux内核device结构体分析(转)》
    《设备树LED模板驱动程序》
    《C库 — 字符串和整型数相互转换函数atoi和itoa》
    《Ubuntu — rsync命令》
    《Ubuntu — 2>/dev/null和>/dev/null 2>&1和2>&1>/dev/null的区别》
  • 原文地址:https://www.cnblogs.com/hqdj970120/p/6831580.html
Copyright © 2020-2023  润新知