(代码练习)计算日期差---Java

89 篇文章 0 订阅
23 篇文章 0 订阅

1、年月日之差

/**
 * 构造方法:
 * MyDate(MyDate date);
 * MyDate(int year,int month,int day);
 *
 * 对外方法
 * public void nextDay()
 * public void preDay()
 * public String toString()
 */

public class MyDate {
    private int year;
    private int month;
    private int day;

    public MyDate(MyDate date){
        this.year=date.year;
        this.month=date.month;
        this.day=date.day;
    }

    public MyDate(int year, int month, int day) {
        check(year,month,day);
        this.year = year;
        this.month = month;
        this.day = day;
    }

    private static void check(int year, int month, int day) {
        if (year<0){
            throw new RuntimeException("year参数错误");
        }
        if (month<1||month>12){
            throw new RuntimeException("month参数错误");
        }
        int days=getDayOfMonth(year,month);
        if (day<1||day>days){
            throw new RuntimeException("day参数错误");
        }
    }

    private static final int[] DAYS={31,28,31,30,31,30,31,31,30,31,30,31};

    private static int getDayOfMonth(int year, int month) {
        if (month==2&&isLeapYear(year)){
            return 29;
        }
        return DAYS[month-1];//因为数组元素下标从0开始,即一月份的天数在数组中下标为0
    }

    private static boolean isLeapYear(int year) {
        return (year%400==0||(year%4==0 && year%100!=0));
    }

    @Override
    public String toString() {
        return String.format("%d-%02d-%02d",year,month,day);//返回日期的字符串形式
    }

    //日期往后一天
    public void nextDay(){
        day++;
        if (day<=getDayOfMonth(year,month)){
            return;
        }
        month++;
        day=1;
        if (month<=12){
            return;
        }
        year++;
        month=1;
    }

    //日期往前一天
    public void preDay(){
        day--;
        if (day>0){
            return;
        }
        month--;
        if (month>0){
            day=getDayOfMonth(year,month);//必须先自减再计算
            return;
        }
        year--;
        month=12;
        day=31;
    }
    public int DiffDays(MyDate from){
        if (this.compareTo(from)<=0){
            throw new RuntimeException("from的日期必须在当前日期之前");
        }
        MyDate copyFrom=new MyDate(from);
        int count=0;
        while (copyFrom.compareTo(this)<0){
            System.out.println(copyFrom);
            copyFrom.nextDay();//日期加一天
            count++;
        }
        return count;
    }

    public int compareTo(MyDate from) {
        if (this.year!=from.year){
            return this.year-from.year;
        }
        if (this.month!=from.month){
            return this.month-from.month;
        }
        return this.day-from.day;
    }
}
package date_time;

public class MyDateTest {
    public static void main(String[] args) {
        MyDate from=new MyDate(2021,2,12);
        MyDate to=new MyDate(2021,2,14);

        System.out.printf("从%s到%s经过了%d天",from,to,to.DiffDays(from));
    }
}

2、时分秒之差

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值