DateUtil计算时间差,指定月的天数--封装类

import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
 
import org.apache.commons.lang.time.DateUtils;
 
import com.gzdec.common.log.LoggerUtil;
 
/**
 *     此类用来计算两个日期的相隔时间,相隔时间格式化为需要的形式 
 * @author cxg
 * 
 */
public class DateUtil {
    //一天的毫秒数 86400000 = 24*60*60*1000;
    private static final int millisPerDay = 86400000 ;
    //一小时的毫秒数 3600000 = 24*60*60*1000;
    private static final int millisPerHour = 3600000 ;
    /**
       * 计算时间差 (时间单位,开始时间,结束时间)
       * 调用方法 howLong("h","2007-08-09 10:22:26","2007-08-09 20:21:30") ///9小时56分 返回9小时
       * */
     public static long howLong(String unit,String time1,String time2) throws ParseException
      {
        //时间单位(如:不足1天(24小时) 则返回0),开始时间,结束时间
        Date date1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(time1);
        Date date2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(time2);
        long ltime = date1.getTime()-date2.getTime()<0 ? date2.getTime()-date1.getTime():date1.getTime()-date2.getTime();
        if(unit.equals("s"))
        {
          return ltime/1000;//返回秒
        }
        else if(unit.equals("m"))
        {
          return ltime/60000;//返回分钟
        }
        else if(unit.equals("h"))
        {
          return ltime/millisPerHour;//返回小时
        }
        else if(unit.equals("d"))
        {
          return ltime/millisPerDay;//返回天数
        }
        else
        {
          return 0;
        }
      }
    /** 计算系统日期与目标日期的时间差, 检查传入时间是否早于系统时间, 不符合则返回""
     * @param tar 与系统时间对比的目标日期
     * @return
     * author: cxg 2009-5-24 下午02:06:10
     */
    public static String getIntervalString(Date tar){
        return getIntervalString(new Date(), tar, true);
    }
 
    /** 计算系统日期与目标日期的相隔天数
     * @param tar 与系统时间对比的目标日期
     * @return 相隔天数, 参数无效返回-1
     * author: cxg 2009-5-24 下午02:16:54
     */
    public static int getIntervalDay(Date tar){
        int ret = -1;
        Calendar calNow = Calendar.getInstance();
        if(null != tar
                && tar.before(calNow.getTime())){//参数有效
 
            //获得指定时间的Calendar
            Calendar calTar = Calendar.getInstance();
            calTar.setTime(tar);
 
            long millisNow = calNow.getTimeInMillis();
            long millisTar = tar.getTime();
 
            //指定时间小于系统时间才处理, 否则返回空字符串
            if(millisTar < millisNow){
                //86400000 = 24*60*60*1000;
                ret = (int)((millisNow - millisTar) /(millisPerDay));
            }
        }
        return ret;
    }
 
    /** 
     * @param tar 与系统时间对比的目标日期(字符串格式)
     * @param formats 解析日期的格式
     * @return
     * author: cxg 2009-5-24 下午02:20:11
     */
    public static String getIntervalString(String tar, String[] formats){
        Date dTar = null;
        try {
            dTar = DateUtils.parseDate(tar, formats);
        } catch (ParseException e) {
            LoggerUtil.error(DateUtil.class, "invalid parameters, can't format to Date! ");
            e.printStackTrace();
            return "";
        }
        return getIntervalString(dTar);
    }
 
    /** 获得指定年月的天数
     * @param year
     * @param month
     * @return
     * author: cxg 2009-5-24 下午03:48:17
     */
    private static int getDaysOfMonth(int year, int month) {
        int day = 0;
        switch (month) {
        // 大月
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:
            day = 31;
            break;
        // 小月
        case 4:
        case 6:
        case 9:
        case 11:
            day = 30;
            break;
        case 2:
            if ((0 == year % 400) || (0 == year % 4 && 0 != year % 100)) {//闰年
                day = 29;
            } else {
                day = 28;
            }
            break;
        }
 
//        System.out.println("--nian:" + year + "--月:" + month + "--天数:" + day);
        return day;
    }
 
    /** 计算两个日期的差, 返回如"1年2月14天11.5小时"的字符串, 异常情况返回""
     * @param after 
     * @param before
     * @return
     * author: cxg 2009-5-24 下午03:09:18
     */
    private static String getIntervalString(Date after, Date before) {
        StringBuffer ret = new StringBuffer();
 
        Calendar calAfter = Calendar.getInstance();
        calAfter.setTime(after);
 
        // 获得指定时间的Calendar
        Calendar calBefore = Calendar.getInstance();
        calBefore.setTime(before);
 
        float hour = 0F;
        int day = 0;
        int month = 0;
        int year = 0;
        NumberFormat nf = NumberFormat.getInstance();
        nf.setMaximumFractionDigits(1);//保留1位小数
 
        // 计算小时
        hour = ((calAfter.getTimeInMillis() - calBefore.getTimeInMillis())%millisPerDay)/(float)millisPerHour;
 
        // 计算天
        day = calAfter.get(Calendar.DAY_OF_MONTH) - calBefore.get(Calendar.DAY_OF_MONTH);
        if (day < 0) {
            day = day + getDaysOfMonth(calAfter.get(Calendar.YEAR), calAfter.get(Calendar.MONTH));
            month--;
        }
 
        // 计算月
        month = month + calAfter.get(Calendar.MONTH) - calBefore.get(Calendar.MONTH);
        if (month < 0) {
            month = month + 12;
            year--;
        }
 
        // 计算年
        year = year + calAfter.get(Calendar.YEAR) - calBefore.get(Calendar.YEAR);
 
        if (year > 0) {
            ret.append(year).append("年");
        }
        if (month > 0) {
            ret.append(month).append("月");
        }
        if (day > 0) {
            ret.append(day).append("天");
        }
        if(hour > 0F){
            ret.append(nf.format(hour)).append("小时");
        }
        return ret.toString();
    }
 
    /**
     * 计算两个日期的差, 返回如"1年2月14天"的字符串, 异常情况返回""
     * @param after
     * @param before
     * @param check 是否检查参数 after.before(before), 检查不符合返回""
     * @return author: cxg 2009-5-24 下午03:09:18
     */
    public static String getIntervalString(Date after, Date before, boolean check){
        if(null != after
                && null != before){//参数有效
            if(after.before(before)){//期望tar1日期更大
                if(check){//强制检查则返""
                    return "";
                }else{//调换
                    Date tmpDate = after;
                    after = before;
                    before = tmpDate;
                }
            }
 
            return getIntervalString(after, before);
        }else{
            return "";
        }
    } 
}

你可能感兴趣的:(Java,date,string,calendar,parameters,null,float)