Here you can find the source of countDiffDay(String beginDateBase, String endDateBase)
Parameter | Description |
---|---|
beginDateBase | "20100101" , "25530101" |
endDateBase | "20100110" , "25530110" |
public static int countDiffDay(String beginDateBase, String endDateBase)
//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); } }