日期算法的相关操作

第一次写项目。用到的一些关于日期的操作。发现网上居然没有日期减天数的到日期的算法,所以就自己写了一个:

    
    //将数字转化成字符串	
    public static String ToStringNum2(int num) {
        String stringNum = null;
        if (String.valueOf(num).length() == 1) {
            stringNum = "0" + String.valueOf(num);
            return stringNum;
        }
        if (String.valueOf(num).length() == 2) {
            stringNum = String.valueOf(num);
            return stringNum;

        }

        System.out.println("转化数字出错");
        return null;
    }

    //返回当前日期
    public static String NowTime() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");// 设置日期格式
        String strDate = df.format(new Date());
        return strDate;// new Date()为获取当前系统时间
    }
    
    // 判断年份是否为闰年
    private static boolean isPrime(int year) {
        boolean leadyear;
        if (year % 4 == 0 && year % 100 != 0) {
            leadyear = true;
        } else if (year % 400 == 0) {
            leadyear = true;
        } else {
            leadyear = false;
        }
        return leadyear;
    }

    // 计算间隔天数
    public static int calInterval(String sDate, String eDate) {
        // 时间间隔,初始为0
        int interval = 0;
        boolean flag = true;
        int[] primeMonth = new int[]{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        int[] notPrimeMonth = new int[]{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        /* 将两个日期赋给日历实例,并获取年、月、日相关字段值 */
        int sYears = Integer.parseInt(sDate.substring(0, 4));// 2016-10-01
        int sMonths = Integer.parseInt(sDate.substring(5, 7));
        int sDays = Integer.parseInt(sDate.substring(8, 10));

        int eYears = Integer.parseInt(eDate.substring(0, 4));// 2016-10-01
        int eMonths = Integer.parseInt(eDate.substring(5, 7));
        int eDays = Integer.parseInt(eDate.substring(8, 10));
        // 从sYears开始累加到eYears
        for (int i = sYears; i < eYears; i++) {
            interval += isPrime(i) ? 366 : 365;
        }
        // 如果eYears是闰年,则flag=true,后面调用primeMonth[12]
        flag = isPrime(eYears);
        // 加上eMonths到1月的天数
        for (int i = 1; i < eMonths; i++) {
            if (flag) {
                interval += primeMonth[i - 1];
            } else {
                interval += notPrimeMonth[i - 1];
            }
        }
        // 如果sYears是闰年,则flag=true,后面调用primeMonth[12]

        flag = isPrime(sYears);
        // 减去sMonths到1月的天数
        for (int i = 1; i < sMonths; i++) {
            if (flag) {
                interval -= primeMonth[i - 1];
            } else {
                interval -= notPrimeMonth[i - 1];
            }
        }
        interval = interval + eDays - sDays;
        return interval;
    }
    //计算多少天之后的日期
    public static String addDate(String initDate, int diffDays) {
        // 时间间隔,初始为0
        int interval = 0;
        boolean flag = true;
        int[] normalMonthDays = new int[]{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

        /* 将两个日期赋给日历实例,并获取年、月、日相关字段值 */
        int initDateYears = Integer.parseInt(initDate.substring(0, 4));
        int initDateMonths = Integer.parseInt(initDate.substring(5, 7));
        int initDateDays = Integer.parseInt(initDate.substring(8, 10));

        int reDateYears = initDateYears;
        int reDateMonths = initDateMonths;
        int reDateDays = initDateDays;
        //1.get years (days >=366 or 365)  
        int daysAyear = 365;
        if (isPrime(reDateYears) && (reDateMonths < 3)) {//if leap year 
            daysAyear = 366;
        }
        while ((diffDays / daysAyear) != 0) {
            diffDays = diffDays - daysAyear;
            reDateYears++;
            if (isPrime(reDateYears)) {
                daysAyear = 366;
            } else {
                daysAyear = 365;
            }
        }
        //2.get months (days < 366 or 365) 
        if (isPrime(reDateYears)) {//if leap year 
            normalMonthDays[2] = 29;
        } else {
            normalMonthDays[2] = 28;
        }
        while ((diffDays / normalMonthDays[reDateMonths]) != 0) {
            diffDays = diffDays - normalMonthDays[reDateMonths];
            reDateMonths++;
            if (reDateMonths >= 13) {
                reDateYears++;
                if (isPrime(reDateYears)) {
                    normalMonthDays[2] = 29;
                } else {
                    normalMonthDays[2] = 28;
                }
                reDateMonths = reDateMonths % 12;
            }
        }
        //3.get days 
        if (isPrime(reDateYears)) {
            normalMonthDays[2] = 29;
        } else {
            normalMonthDays[2] = 28;
        }
        if (diffDays + reDateDays <= normalMonthDays[reDateMonths]) {
            reDateDays = diffDays + reDateDays;
        } else {
            reDateDays = diffDays + reDateDays - normalMonthDays[reDateMonths];
            reDateMonths++;
            if (reDateMonths > 12) {
                reDateYears++;
                reDateMonths = reDateMonths % 12;
            }
        }
        String reDate = String.valueOf(reDateYears) + "-" + ToStringNum2(reDateMonths) + "-" + ToStringNum2(reDateDays);
        return reDate;
    }
    //计算多少天之前的日期
    public static String minusDate(String initDate, int diffDays) {
        // 时间间隔,初始为0
        int interval = 0;
        boolean flag = true;
        int[] normalMonthDays = new int[]{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

        /* 将两个日期赋给日历实例,并获取年、月、日相关字段值 */
        int initDateYears = Integer.parseInt(initDate.substring(0, 4));
        int initDateMonths = Integer.parseInt(initDate.substring(5, 7));
        int initDateDays = Integer.parseInt(initDate.substring(8, 10));

        int reDateYears = initDateYears;
        int reDateMonths = initDateMonths;
        int reDateDays = initDateDays;

        if (reDateDays > diffDays) {
            reDateDays -= diffDays;

        } else if (reDateDays == diffDays) {
            reDateMonths--;
            if (reDateMonths < 1) {
                reDateYears--;
                reDateMonths = 12;
            }
            if (isPrime(reDateYears)) {
                normalMonthDays[2] = 29;
            } else {
                normalMonthDays[2] = 28;
            }
            reDateDays = normalMonthDays[reDateMonths];

        } else {
            diffDays = diffDays - reDateDays;

            reDateMonths--;
            if (reDateMonths < 1) {
                reDateYears--;
                reDateMonths = 12;
            }
            if (isPrime(reDateYears)) {
                normalMonthDays[2] = 29;
            } else {
                normalMonthDays[2] = 28;
            }
            reDateDays = normalMonthDays[reDateMonths];
            ///;
            int daysAyear = 365;
            if (isPrime(reDateYears) && (reDateMonths > 2)) {//if leap year 
                daysAyear = 366;
            } else if ((reDateMonths == 2) && (reDateDays == 29)) {
                daysAyear = 366;
            }
            while ((diffDays / daysAyear) != 0) {
                diffDays = diffDays - daysAyear;
                reDateYears--;
                if (isPrime(reDateYears)) {
                    daysAyear = 366;
                } else {
                    daysAyear = 365;
                }
            }
            //2.get months (days < 366 or 365) 
            if (isPrime(reDateYears)) {//if leap year 
                normalMonthDays[2] = 29;
            } else {
                normalMonthDays[2] = 28;
            }
            while ((diffDays / normalMonthDays[reDateMonths]) != 0) {
                diffDays = diffDays - normalMonthDays[reDateMonths];
                reDateMonths--;
                if (reDateMonths < 1) {
                    reDateYears--;
                    if (isPrime(reDateYears)) {
                        normalMonthDays[2] = 29;
                    } else {
                        normalMonthDays[2] = 28;
                    }
                    reDateMonths = 12;
                }
            }
            //3.get days 
            if (isPrime(reDateYears)) {
                normalMonthDays[2] = 29;
            } else {
                normalMonthDays[2] = 28;
            }
            reDateDays = normalMonthDays[reDateMonths];
            reDateDays -= diffDays;

        }

        String reDate = String.valueOf(reDateYears) + "-" + ToStringNum2(reDateMonths) + "-" + ToStringNum2(reDateDays);
        return reDate;
    }

 
  

你可能感兴趣的:(个人算法整理)