Java面向对象编程实践-日期类的实现

日期类的实现

描述

实现创建一个某年某月某日的日期类Date

  1. 可以实现加天数的操作
  2. 可以实现减天数的操作
  3. 可以求得两个日期的差

变量

private int year; 	// 年
private int month;	// 月
private int day;    // 日
public static int[] monthDays1 = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };	// 一般年每月日数
public static int[] monthDays2 = { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };	// 闰年每月日数

方法

// SET&GET
public boolean setYear(int y);
public boolean setMonth(int m);
public boolean setDay(int d);
public int getYear();
public int getMonth();
public int getDay();
// 构造方法
Date(int y, int m, int d);
// 功能实例方法
public boolean isLeap();									// 判断闰年
public void add();											// 加一天
public void sub();											// 减一天
public void Increase(int days);								// 加多天
public void Decrease(int days);								// 减多天
public boolean isLarger(int year, int month, int day);		// 判断大于
public boolean isLarger(TMyDate mydate);					// 判断大于(重载)
public boolean isEqual(int y, int m, int d);				// 判断相等
public boolean isEqual(Date t);								// 判断相等(重载)
public int Interval(int year, int month, int day);			// 做天数差
public int Interval(TMyDate mydate);						// 做天数差(重载)

日期类实现

Date类

package project;

public class Date {
    // 实例变量
    private int year;
    private int month;
    private int day;
    public static int[] monthDays1 = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    public static int[] monthDays2 = { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

    // 实例方法
    Date(int y, int m, int d) {
        setYear(y);
        setMonth(m);
        setDay(d);
    }

    // set
    public boolean setYear(int y) {
        if (y >= 0) {
            year = y;
            return true;
        } else {
            System.out.println("year wrong!");
            return false;
        }
    }

    public boolean setMonth(int m) {
        if (m > 0 && m <= 12) {
            month = m;
            return true;
        } else {
            System.out.println("month wrong!");
            return false;
        }
    }

    public boolean setDay(int d) {
        if (d > 0 && ((isLeap() && d <= monthDays2[month]) || (!isLeap() && d <= monthDays1[month]))) {
            day = d;
            return true;
        } else {
            System.out.println("year wrong!");
            return false;
        }
    }

    // get
    public int getYear() {
        return year;
    }

    public int getMonth() {
        return month;
    }

    public int getDay() {
        return day;
    }

    // 判断闰年
    public boolean isLeap() {
        if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
            return true;
        }
        return false;
    }

    // 加一天
    public void add() {
        if ((isLeap() && day + 1 <= monthDays2[month]) || (!isLeap() && day + 1 <= monthDays1[month])) {
            day++;
        } else {
            day = 1;
            if (month + 1 <= 12) {
                month++;
            } else {
                month = 1;
                year++;
            }
        }
    }

    // 减一天
    public void sub() {
        if (day - 1 > 0) {
            day--;
        } else {
            if (month - 1 > 0) {
                month--;
                day = isLeap() ? monthDays2[month] : monthDays1[month];
            } else {
                year--;
                month = 12;
                day = 31;
            }
        }
    }

    // 加天数
    public void Increase(int days) {
        for (int i = 0; i < days; i++) {
            add();
        }
    }

    // 减天数
    public void Decrease(int days) {
        for (int i = 0; i < days; i++) {
            sub();
        }
    }

    // 比大小
    // 大于
    public boolean isLarger(int y, int m, int d) {
        if (year > y) {
            return true;
        } else if (year < y) {
            return false;
        } else {
            if (month > m) {
                return true;
            } else if (month < m) {
                return false;
            } else {
                if (day > d) {
                    return true;
                } else {
                    return false;
                }
            }
        }
    }

    public boolean isLarger(Date t) {
        if (year > t.year) {
            return true;
        } else if (year < t.year) {
            return false;
        } else {
            if (month > t.month) {
                return true;
            } else if (month < t.month) {
                return false;
            } else {
                if (day > t.day) {
                    return true;
                } else {
                    return false;
                }
            }
        }
    }

    // 等于
    public boolean isEqual(int y, int m, int d) {
        if (year == y && month == m && day == d) {
            return true;
        }
        return false;
    }

    public boolean isEqual(Date t) {
        if (year == t.year && month == t.month && day == t.day) {
            return true;
        }
        return false;
    }

    // 算差
    public int Interval(int y, int m, int d) {
        Date t = new Date(y, m, d);
        int res = 0;
        if (isLarger(t)) {
            while (!isEqual(t)) {
                t.add();
                res++;
            }
        } else {
            while (!isEqual(t)) {
                t.sub();
                res++;
            }
        }
        return res;
    }

    public int Interval(Date t_) {
        Date t = new Date(t_.year, t_.month, t_.day);
        int res = 0;
        if (isLarger(t)) {
            while (!isEqual(t)) {
                t.add();
                res++;
            }
        } else {
            while (!isEqual(t)) {
                t.sub();
                res++;
            }
        }
        return res;
    }

    // 打印属性
    public void show() {
        System.out.printf("%d/%d/%d\n", year, month, day);
    }

}

主程序测试

package project;

public class main {
    public static void main(String[] args) {

        Date d1 = new Date(2022, 4, 4);
        Date d2 = new Date(2022, 3, 4);
        d1.show(); // 2022/4/4
        d2.show(); // 2022/3/4
        System.out.println(d1.Interval(d2)); // 31
        d1.Increase(12);
        d1.show(); // 2022/4/16
        d2.Decrease(5);
        d2.show(); // 2022/2/27
        System.out.println(d1.Interval(d2)); // 48

    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值