zoukankan      html  css  js  c++  java
  • 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;
          }
     

     
     
    参考:
  • 相关阅读:
    webpack配置之代码优化
    react组件生命周期
    javascript记住用户名和登录密码
    ajax异步请求原理和过程
    深入理解ajax系列第五篇——进度事件
    ajax多次请求,只执行最后一次的方法
    CentOS6.8下MySQL MHA架构搭建笔记
    HTTP状态码
    什么是 Redis 事务?原理是什么?
    Redis 通讯协议是什么?有什么特点?
  • 原文地址:https://www.cnblogs.com/svennee/p/4082852.html
Copyright © 2011-2022 走看看