`

计算两日期之间多少天

阅读更多
/*
* $Header$
* $Author$
* $Revision$
* $Date$
*
* ====================================================================
*
* Copyright (C) 2008 by www.loveapple.cn
*
* All copyright notices regarding loveapple and loveapple CoreLib
* MUST remain intact in the scripts, documents and source code.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*
* Correspondence and Marketing Questions can be sent to:
* info at loveapple
*
* @author: loveapple
*/
package cn.loveapple.common.util;

import java.util.Calendar;


/**
* @author loveapple
* @since 2008/09/16
* @version $Revision:$
*/
public final class DateUtils {
    /**
     * Get from start to end days.
     * @param start
     * @param end
     * @return start-end days
     */
    public final static int diffDayCount(Calendar start, Calendar end){
        int result = 0;
        DayDate startDay = new DayDate(start);
        DayDate endDay = new DayDate(end);
        if(startDay.equals(endDay) || startDay.equals(startDay.getLastDay(endDay))){
            return result;
        }
       
        if(startDay.year == endDay.year){
            return endDay.dayOfY - startDay.dayOfY;
        }else{
            Calendar tmp = Calendar.getInstance();
            result = startDay.calendar.getActualMaximum(Calendar.DAY_OF_YEAR) - startDay.dayOfY;
            for(int i = startDay.year+1; i < endDay.year; i++){
                tmp.set(i, tmp.get(Calendar.MONTH), tmp.get(Calendar.DAY_OF_MONTH));
                result += tmp.getActualMaximum(Calendar.DAY_OF_YEAR);
            }
            result += endDay.dayOfY;
        }
       
        return result;
    }
   
    /**
     * Express a data of day.
     *
     * @author loveapple
     * @since 2008/09/16
     * @version $Revision:$
     */
    private static class DayDate{
        int year;
        int month;
        int dayOfM;
        int dayOfY;
        Calendar calendar;
        public DayDate(Calendar calendar) {
            year = calendar.get(Calendar.YEAR);
            month = calendar.get(Calendar.MONTH);
            dayOfM = calendar.get(Calendar.DAY_OF_MONTH);
            dayOfY = calendar.get(Calendar.DAY_OF_YEAR);
            this.calendar = calendar;
        }
       
        /**
         *
         * {@inheritDoc}
         */
        @Override
        public boolean equals(Object obj){
            if(obj instanceof DayDate){
                DayDate day = (DayDate)obj;
                if(day.year == year && day.dayOfY == dayOfY){
                    return true;
                }
                return false;
            }
            return false;
        }
       
        /**
         *
         * @param day
         * @return
         */
        public DayDate getLastDay(DayDate day){
            if(day.year == year){
                if(day.dayOfY < dayOfY){
                    return this;
                }
                return day;
            }else if(day.year < year){
                return this;
            }
            return day;
        }
    }
}


测试代码

public static void main(String[] args) {
        Calendar today = Calendar.getInstance();
        Calendar startDay1 = Calendar.getInstance();
        startDay1.set(2000, 1, 2);
        Calendar startDay2 = Calendar.getInstance();
        startDay2.set(today.get(Calendar.YEAR), today.get(Calendar.MONTH), 1);
        Calendar startDay3 = Calendar.getInstance();
        startDay3.set(today.get(Calendar.YEAR), 1, 1);
       
        SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
       
        System.out.println(format.format(startDay1.getTime()) + " - " + format.format(today.getTime()) + " [" + DateUtils.diffDayCount(startDay1, today) + "]");
        System.out.println(format.format(startDay2.getTime()) + " - " + format.format(today.getTime()) + " [" + DateUtils.diffDayCount(startDay2, today) + "]");
        System.out.println(format.format(startDay3.getTime()) + " - " + format.format(today.getTime()) + " [" + DateUtils.diffDayCount(startDay3, today) + "]");
    }

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics