Java求前一天和后一天、相差天数

1. 日期(年、月、日)

2.+1天        -1天

3.返回当前日期的字符串

package mjh.week.day6;

//约束要求
//1.year只能从1000-3000
//2.month只能从1-12
//3.day每个月都不同
public class Date<month> {
    private int year;
    private int month;
    private int day;

    public Date(int year, int month, int day) {
        if (!checkYear(year)) {
            throw new RuntimeException("year必须是[1000,3000]:year = " + year);
        }
        if (!checkMonth(month)) {
            throw new RuntimeException("month必须是[1,12]: month = " + month);
        }
        if (!checkDay(year, month, day)) {
            throw new RuntimeException("day必须满足正确的范围:day = " + day);
        }
        //这里已经可以保证数据一定是正确的,可以保存在属性之中了
        this.year = year;
        this.month = month;
        this.day = day;
    }

    public String toString() {
        return String.format("%04d-%02d-%02d", year, month, day);
    }

    public void 往前一天() {
        day--;
        if (day > 0) {
            return;
        }
        month--;
        if (month == 0) {
            year--;
            month = 12;
        }
        day = 根据月取边界(year, month);
    }

    public void 往后一天() {
        day++;

        int dayBound = 根据月取边界(year, month);
        if (day <= dayBound) {
            return;
        }
        month++;
        day = 1;

        if (month <= 12) {
            return;
        }
        year++;
        month = 1;
    }

    private int 根据月取边界(int year, int month) {
        int dayBound;
        if (month == 2) {
            return isLeapyear(year) ? 29 : 28;
           /* if (isLeapyear(year)) {
                dayBound = 29;
            } else {
                dayBound = 28;
            }*/
        } else {
            /*int m = month - 1;*/
            /* dayBound = DAY_OF_MONTH[m];*/
            return DAY_OF_MONTH[month - 1];
        }
    }

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

    private static boolean isLeapyear(int year) {
        if (year % 400 == 0) {
            return true;
        }
        return year % 4 == 0 && year % 100 != 0;
    }

    private static boolean checkDay(int year, int month, int day) {
        if (month != 2) {
            int i = month - 1;
            return day >= 1 && day <= DAY_OF_MONTH[i];
        }
        if (isLeapyear(year)) {
            return day >= 1 && day <= 29;
        }
        return day >= 1 && day <= 28;
    }

    private static boolean checkMonth(int month) {
        return month >= 1 && month <= 12;
    }

    private static boolean checkYear(int year) {
        return year >= 1000 && year <= 3000;
    }
}

package mjh.week.day6;

public class UseDate {
    public static void main(String[] args) {
        Date d1 = new Date(2020, 1, 1);
        /*for(int i=0;i<=300;i++){
            d1.往后一天();
            if(i%15==0){
                System.out.println(d1.toString());
            }
        }*/
        /*Date d1=new Date(2001,2,29);
        String s=d1.toString();
        System.out.println(s);*/
        for (int i = 0; i <= 300; i++) {
            d1.往前一天();
            if (i % 15 == 0) {
                System.out.println(d1.toString());
            }
        }
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

穿狼皮的小红帽

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值