提取两个时间点的天数差

这个Java代码实现了一个DateUtils类,用于计算两个日期之间的天数差。它使用Calendar对象作为输入,通过比较年份和月份计算日期差。代码中还包括一个内部类DayDate,用于表达日期并进行相等比较。
作者:郝春利
转贴请注明出处: http://blog.youkuaiyun.com/froole

提取两个时间点的天数差——看上去一个及其单纯的问题,但是,考虑到不同年份、闰年等因素,想要写一个可以广泛使用的却非常麻烦。
以下是loveapple工程中我写的一个提取不同时间点的天数的代码,如有不足,希望各路大虾指正。


  1. /*
  2.  * $Header$
  3.  * $Author$
  4.  * $Revision$
  5.  * $Date$
  6.  *
  7.  * ====================================================================
  8.  *
  9.  * Copyright (C) 2008 by www.loveapple.cn
  10.  *
  11.  * All copyright notices regarding loveapple and loveapple CoreLib
  12.  * MUST remain intact in the scripts, documents and source code.
  13.  *
  14.  * This library is free software; you can redistribute it and/or
  15.  * modify it under the terms of the GNU Lesser General Public
  16.  * License as published by the Free Software Foundation; either
  17.  * version 3 of the License, or (at your option) any later version.
  18.  *
  19.  * This library is distributed in the hope that it will be useful,
  20.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  22.  * Lesser General Public License for more details.
  23.  *
  24.  * You should have received a copy of the GNU Lesser General Public
  25.  * License along with this library; if not, write to the Free Software
  26.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  27.  *
  28.  * Correspondence and Marketing Questions can be sent to:
  29.  * info at loveapple
  30.  *
  31.  * @author: loveapple
  32.  */
  33. package cn.loveapple.common.util;
  34. import java.util.Calendar;
  35. /**
  36.  * @author loveapple
  37.  * @since 2008/09/16
  38.  * @version $Revision:$
  39.  */
  40. public final class DateUtils {
  41.     /**
  42.      * Get from start to end days.
  43.      * @param start
  44.      * @param end
  45.      * @return start-end days
  46.      */
  47.     public final static int diffDayCount(Calendar start, Calendar end){
  48.         int result = 0;
  49.         DayDate startDay = new DayDate(start);
  50.         DayDate endDay = new DayDate(end);
  51.         if(startDay.equals(endDay) || startDay.equals(startDay.getLastDay(endDay))){
  52.             return result;
  53.         }
  54.         
  55.         if(startDay.year == endDay.year){
  56.             return endDay.dayOfY - startDay.dayOfY;
  57.         }else{
  58.             Calendar tmp = Calendar.getInstance();
  59.             result = startDay.calendar.getActualMaximum(Calendar.DAY_OF_YEAR) - startDay.dayOfY;
  60.             for(int i = startDay.year+1; i < endDay.year; i++){
  61.                 tmp.set(i, tmp.get(Calendar.MONTH), tmp.get(Calendar.DAY_OF_MONTH));
  62.                 result += tmp.getActualMaximum(Calendar.DAY_OF_YEAR);
  63.             }
  64.             result += endDay.dayOfY;
  65.         }
  66.         
  67.         return result;
  68.     }
  69.     
  70.     /**
  71.      * Express a data of day.
  72.      * 
  73.      * @author loveapple
  74.      * @since 2008/09/16
  75.      * @version $Revision:$
  76.      */
  77.     private static class DayDate{
  78.         int year;
  79.         int month;
  80.         int dayOfM;
  81.         int dayOfY;
  82.         Calendar calendar;
  83.         public DayDate(Calendar calendar) {
  84.             year = calendar.get(Calendar.YEAR);
  85.             month = calendar.get(Calendar.MONTH);
  86.             dayOfM = calendar.get(Calendar.DAY_OF_MONTH);
  87.             dayOfY = calendar.get(Calendar.DAY_OF_YEAR);
  88.             this.calendar = calendar;
  89.         }
  90.         
  91.         /**
  92.          * 
  93.          * {@inheritDoc}
  94.          */
  95.         @Override
  96.         public boolean equals(Object obj){
  97.             if(obj instanceof DayDate){
  98.                 DayDate day = (DayDate)obj;
  99.                 if(day.year == year && day.dayOfY == dayOfY){
  100.                     return true;
  101.                 }
  102.                 return false;
  103.             }
  104.             return false;
  105.         }
  106.         
  107.         /**
  108.          * 
  109.          * @param day
  110.          * @return
  111.          */
  112.         public DayDate getLastDay(DayDate day){
  113.             if(day.year == year){
  114.                 if(day.dayOfY < dayOfY){
  115.                     return this;
  116.                 }
  117.                 return day;
  118.             }else if(day.year < year){
  119.                 return this;
  120.             }
  121.             return day;
  122.         }
  123.     }
  124. }

测试代码
  1. public static void main(String[] args) {
  2.         Calendar today = Calendar.getInstance();
  3.         Calendar startDay1 = Calendar.getInstance();
  4.         startDay1.set(200012);
  5.         Calendar startDay2 = Calendar.getInstance();
  6.         startDay2.set(today.get(Calendar.YEAR), today.get(Calendar.MONTH), 1);
  7.         Calendar startDay3 = Calendar.getInstance();
  8.         startDay3.set(today.get(Calendar.YEAR), 11);
  9.         
  10.         SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
  11.         
  12.         System.out.println(format.format(startDay1.getTime()) + " - " + format.format(today.getTime()) + " [" + DateUtils.diffDayCount(startDay1, today) + "]");
  13.         System.out.println(format.format(startDay2.getTime()) + " - " + format.format(today.getTime()) + " [" + DateUtils.diffDayCount(startDay2, today) + "]");
  14.         System.out.println(format.format(startDay3.getTime()) + " - " + format.format(today.getTime()) + " [" + DateUtils.diffDayCount(startDay3, today) + "]");
  15.     }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值