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;
          }
     

     
     
    参考:
  • 相关阅读:
    CSS弹性盒布局(display:flex)
    CSS中的display属性(none,block,inline,inline-block,inherit)
    【Python全栈-JavaScript】JavaScript的window.onload()与jQuery 的ready()的区别
    【数据可视化-Echarts】Echart基础
    【Python全栈】HTML <!--...--> 注释 、CSS/JS //注释 和 /*.....*/ 注释
    Python内置函数
    【Django】Model操作进阶
    【Django】Form组件-2
    【Django】分页
    【Django】模板语言
  • 原文地址:https://www.cnblogs.com/svennee/p/4082852.html
Copyright © 2011-2022 走看看