Java Day Between countDiffDay(String beginDateBase, String endDateBase)

Here you can find the source of countDiffDay(String beginDateBase, String endDateBase)

Description

count Diff Day

License

Open Source License

Parameter

Parameter Description
beginDateBase "20100101" , "25530101"
endDateBase "20100110" , "25530110"

Return

int 9

Declaration

public static int countDiffDay(String beginDateBase, String endDateBase) 

Method Source Code

//package com.java2s;
/*/*  www .j  a  v a2  s . c o  m*/
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

import java.util.Calendar;

import java.util.Locale;

public class Main {
    /**
     * @param beginDateBase "20100101" , "25530101"
     * @param endDateBase "20100110" , "25530110"
     * @return int 9
     * @example int dateDiff = DateUtils.countDiffDay("20130101", "20130110");
     */
    public static int countDiffDay(String beginDateBase, String endDateBase) {
        Calendar beginDate = Calendar.getInstance(Locale.US);
        Calendar endDate = Calendar.getInstance(Locale.US);

        int year, month, date;

        year = Integer.valueOf(beginDateBase.substring(0, 4)).intValue();
        if (year > 2400) {
            year = year - 543;
        }
        month = Integer.valueOf(beginDateBase.substring(4, 6)).intValue() - 1;
        date = Integer.valueOf(beginDateBase.substring(6, 8)).intValue();

        beginDate.set(year, month, date);

        year = Integer.valueOf(endDateBase.substring(0, 4)).intValue();
        if (year > 2400) {
            year = year - 543;
        }
        month = Integer.valueOf(endDateBase.substring(4, 6)).intValue() - 1;
        date = Integer.valueOf(endDateBase.substring(6, 8)).intValue();

        endDate.set(year, month, date);

        return (countDiffDay(beginDate, endDate));
    }

    public static int countDiffDay(Calendar beginDate, Calendar endDate) {
        int returnInt = 0;

        while (!beginDate.after(endDate)) {
            beginDate.add(Calendar.DAY_OF_MONTH, 1);
            returnInt++;
        }
        if (returnInt > 0) {
            returnInt = returnInt - 1;
        }

        return (returnInt);
    }
}

Related

  1. calculateDays(Date beginDate, Date endDate)
  2. calculateDays(Date dateEarly, Date dateLater)
  3. calculateDifference(Date d1, Date d2)
  4. calculateDifferMonths(Date date1, Date date2, boolean isTruncate)
  5. calculateNumberOfDays(String dateStr)
  6. dayDiff(Date d1, Date d2)
  7. dayDiff(Date start, Date end)
  8. daysDiff(final Date data1, final Date data2)
  9. differentDaysByMillisecond(Date date1, Date date2)