首页 > 代码库 > java计算时间差

java计算时间差


java实现计算时间差
 
 
正式版:

      /**
       * 计算时间差,求出两者相隔的时间
       *
       * @param nowDate
       *            当前时间
       * @param calculateDate
       *            计算的时间
       * @return
       */
      public static long calculateTime(Date nowDate, Date calculateDate) {
            long ret = 0;
            try {
                  long t = nowDate.getTime() - calculateDate.getTime();
                  long day = t / (1000 * 60 * 60 * 24);
                  long hour = t / (1000 * 60 * 60);
                  long minute = t / (1000 * 60);
                  long second = t / 1000;
 
                  // ret = ?
            } catch (Exception e) {
            }
            return ret;
      }
 
       /**
       * 计算时间差,求出时间相隔的天数,值是四舍五入 <br><pre>
       */
       public static long calculateTime4Days(Date nowDate, Date calculateDate) {
             long ret = 0;
             try {
                   long t = nowDate.getTime() - calculateDate.getTime();
//                long day = d / (1000 * 60 * 60 * 24);
                   // 四舍五入
                  Double td = 0.0;
                  td += t;
                   long day = Math.round(td / (1000 * 60 * 60 * 24));
                  
                  ret = day;
            } catch (Exception e) {
            }
             return ret;
      }
 
 

 
 
#2013-6-7补充一个计算月份的,正式版:

       //**************************************************************************
       /**
       * 计算时间差,求出时间相隔的月份 <br><pre>
       * 编写者: sven
       * Email:swnic @isoftstone.com
       * 创建时间:2013 -6- 7 下午02:21:58 </pre>
       * @param boolean isBuc 是否补充:不足一月算一月,true;不足一月不算一月,false;
       * @return long 说明
       * @throws 异常类型 说明
       */
       //**************************************************************************
       public static long calculateTime4Month(Date startDate, Date endDate, boolean isBuc) {
             long monthDiff = 0;
 
             try {
//                String start = "2011-06-12";
//                String end = "2012-06-01";
//                SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
//                Date startDate = fmt.parse(start);
//                Date endDate = fmt.parse(end);
 
                  Calendar starCal = Calendar. getInstance();
                  starCal.setTime(startDate);
 
                   int sYear = starCal.get(Calendar.YEAR);
                   int sMonth = starCal.get(Calendar.MONTH);
                   int sDay = starCal.get(Calendar.DAY_OF_MONTH);
 
                  Calendar endCal = Calendar. getInstance();
                  endCal.setTime(endDate);
                   int eYear = endCal.get(Calendar.YEAR);
                   int eMonth = endCal.get(Calendar.MONTH);
                   int eDay = endCal.get(Calendar.DAY_OF_MONTH);
 
                  monthDiff = ((eYear - sYear) * 12 + (eMonth - sMonth));
 
//                boolean isBuc = true;
                   if (isBuc) {
                         // 不足一月不算一月减1,默认做法,参考公式
                         // 依据天数补充,不足一月算一月加1
                         if (sDay < eDay) {
                              monthDiff = monthDiff + 1;
                        } else if (sDay > eDay) {
                              monthDiff = monthDiff - 1;
                        }
                  }
            } catch (Exception e) {
            }
 
             return monthDiff;
      }
 

 
 
参考:
cnblogs - Java中计算时间差

java计算时间差