计算两个日期相差天数,不允许使用java高级类

问题描述

给定日期a"2019-04-21"和日期b"2018-12-21",计算两个日期之间相差多少天?不允许使用高级封装日期等。

解题思路

先处理字符串,变成输入的年月日
再计算日期差
计算日期差思路:计算当前日期a是相对于公元0年的总第多少天,再计算日期b的总第多少天,做差即可。
计算总第多少天思路:
1. 2019年之前总共有多少天:(2019-1)*365+闰年个数
2. 4月前 总共有多少天:平年4月前多少天 or 闰年4月前多少天
3. 总第天数 = 前两项+日期数 。

题解

/*
 * Date :  2019.
 * Author : Mereder
 */

public class theDateDiff {
    public static class Mydate{
        public int year;
        public int mon;
        public int day;

        public Mydate(int year, int mon, int day) {
            this.year = year;
            this.mon = mon;
            this.day = day;
        }
    }
    public static  final int daysOfMon[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
    public static int beforeMon_com[] = new int[13];
    public static int beforeMon_leap[] = new int[13];

    public static int total(Mydate mydate){
        int total  = beforeYear(mydate) + beforeMon(mydate) + mydate.day;
        return total;
    }
    // 这一年之前一共多少天
    public static int beforeYear(Mydate date){
        int year = date.year;
        // 被四整除的年份  减去 被100 整除的 (其中也减去了 被400 整除的  再加回来)
        int total = (year-1)*365 + (year-1) / 4 - (year-1) / 100 + (year-1) / 400;
        return  total;
    }
    public static int beforeMon(Mydate mydate){
        if (isLeapYear(mydate)){
            return beforeMon_leap[mydate.mon];
        }
        else return beforeMon_com[mydate.mon];
    }
    public static boolean isLeapYear(Mydate mydate){
        int year = mydate.year;
        if ((year%4 == 0 && year%100 != 0) || (year%400 == 0)) return true;
        else return false;
    }
    public static void main(String[] args) {
        // 总体思路: 以2019年4月24日为例
        // 2019年之前总共有多少天:(2019-1)*365+闰年个数
        // 4月前 总共有多少天:平年4月前多少天 or 闰年四月前多少天
        // 前两项+24日 = 总共天数。
        // 输入转换
        String date1 = "2019-04-24";
        String date2 = "2019-04-22";
        int n = 0;
        // 初始化 月前总天数数组  含义:2月1日前总共多少天......12月1日前总共多少天
        for (int i = 1; i < 13; i++) {
            beforeMon_com[i] = n;
            if (i > 2){
                beforeMon_leap[i] = n +1;
            }
            else beforeMon_leap[i] = n;

            n += daysOfMon[i];
        }
        int year1 = Integer.parseInt(date1.split("-")[0]);
        int mon1 = Integer.parseInt(date1.split("-")[1]);
        int day1 = Integer.parseInt(date1.split("-")[2]);
        Mydate first = new Mydate(year1,mon1,day1);
        int year2 = Integer.parseInt(date2.split("-")[0]);
        int mon2 = Integer.parseInt(date2.split("-")[1]);
        int day2 = Integer.parseInt(date2.split("-")[2]);
        Mydate second = new Mydate(year2,mon2,day2);

        System.out.printf("相差 %d 天",(total(first)-total(second)));
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值