作者:郝春利
转贴请注明出处: http://blog.youkuaiyun.com/froole
提取两个时间点的天数差——看上去一个及其单纯的问题,但是,考虑到不同年份、闰年等因素,想要写一个可以广泛使用的却非常麻烦。
以下是loveapple工程中我写的一个提取不同时间点的天数的代码,如有不足,希望各路大虾指正。
测试代码
转贴请注明出处: http://blog.youkuaiyun.com/froole
提取两个时间点的天数差——看上去一个及其单纯的问题,但是,考虑到不同年份、闰年等因素,想要写一个可以广泛使用的却非常麻烦。
以下是loveapple工程中我写的一个提取不同时间点的天数的代码,如有不足,希望各路大虾指正。
- /*
- * $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) + "]");
- }