java 课后习题 计算两个日期之间的天数

该程序通过用户输入的两个日期,利用Java实现计算它们之间的天数差。首先使用split方法分割输入的字符串,然后将字符串转换为整型,用于创建YearMonthDay类的实例。YearMonthDay类中包含计算每年总天数和每月天数的方法,最后通过Math.abs计算两者天数差并输出。
摘要由CSDN通过智能技术生成

知识点:
1.绝对值 Math.abs() 的运用
2.str1.splist() 字符串分割方法的运用
3.字符串强转整形Integer.parseInt()的运用

public class Study {

    //练习 计算两个日期之间含有多少天
    public static void main(String[] args) {
        System.out.println("请输入第一个日期 用\"-\"分隔 例:2019-06-02");
        Scanner scan = new Scanner(System.in);
        String str1 = scan.nextLine();

        System.out.println("请输入第二个日期 用\"-\"分隔 例:2019-06-02");
        String str2 = scan.nextLine();

        //put the Str to the Strsp
       String[] strsp1 = str1.split("\\-");
       String[] strsp2 = str2.split("\\-");


       //实例化YearMonthDay

        YearMonthDay ymd = new YearMonthDay(strsp1,strsp2);
        System.out.println("两者相差:"+Math.abs(ymd.allDayOne()-ymd.allDayTwo())+"天");








    }
}

class YearMonthDay{

    int year1;
    int month1;
    int day1;

    int year2;
    int month2;
    int day2;


    int allDayOne;
    int allDayTwo;

   public YearMonthDay(String[] strsp1,String[] strsp2){
        //将分隔后的字符串分别 转换为int;
       this.year1 = Integer.parseInt(strsp1[0]);
       this.month1 = Integer.parseInt(strsp1[1]);
       this.day1= Integer.parseInt(strsp1[2]);

       this.year2 = Integer.parseInt(strsp2[0]);
       this.month2 = Integer.parseInt(strsp2[1]);
       this.day2 = Integer.parseInt(strsp2[2]);


    }

        public int allDayOne(){

            //计算第一次输入的年总天数
            for (int i = 1900;i<year1;i++){
                //判断闰不闰
                if ((year1 % 4 ==0 && year1 % 100 != 0) || year1 % 400 == 0){

                    allDayOne +=366;
                }else{
                    allDayOne +=365;
                }


            }
            //累加月份天数
            for (int m = 1;m<month1;m++){

                switch (m){
                    case 2: if ((year1 % 4 ==0 && year1 % 100 != 0) || year1 % 400 == 0){
                        allDayOne +=29;
                    }else {
                        allDayOne += 28;
                    }
                    break;

                    case 1: case 3: case 5: case 7: case 8: case 10: case 12:allDayOne += 31;break;

                    default:allDayOne +=30;break;



                }
            }

            return allDayOne+day1;
        }

        public int allDayTwo(){

            //计算第二次输入的年总天数
            for (int i = 1900;i<year2;i++){
                //判断闰不闰
                if ((year2 % 4 ==0 && year2 % 100 != 0) || year2 % 400 == 0){

                    allDayTwo +=366;
                }else{
                    allDayTwo +=365;
                }
            }

            for (int m = 1;m<month2;m++){

                switch (m){
                    case 2: if ((year2 % 4 ==0 && year2 % 100 != 0) || year2 % 400 == 0){
                        allDayTwo +=29;
                    }else {
                        allDayTwo += 28;
                    }
                        break;

                    case 1: case 3: case 5: case 7: case 8: case 10: case 12:allDayTwo += 31;break;

                    default:allDayTwo +=30;break;



                }
            }

       return allDayTwo+day2;
        }

    @Override
    public String toString() {
        return "YearMonthDay{" +
                "year1=" + year1 +
                ", month1=" + month1 +
                ", day1=" + day1 +
                ", year2=" + year2 +
                ", month2=" + month2 +
                ", day2=" + day2 +
                '}';
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值