yujian
2019-08-22 33bfa1b3d03236922cc95b9435d7d10c91e8c4e5
utils/src/main/java/org/yeshi/utils/DateUtil.java
@@ -37,7 +37,7 @@
   public static String dateDiff2(Date startTime, Date endTime) throws Exception {
      String datatime = 0 + "天" + 0 + "小时" + 0 + "分钟";
      String datatime = "0分钟";
      long nm = 1000 * 60;// 一分钟的毫秒数
      long nh = 1000 * 60 * 60;// 一小时的毫秒数
@@ -45,13 +45,19 @@
      // 获得两个时间的毫秒时间差异
      long diff = endTime.getTime() - startTime.getTime();
      long day = diff / nd;// 计算差多少天
      long hour = diff % nd / nh;// 计算差多少小时
      long min = diff % nd % nh / nm;// 计算差多少分钟
      datatime = day + "天" + hour + "小时" + min + "分钟";
      if (day > 0) {
         datatime = day + "天" + hour + "时" + min + "分";
      } else {
         if (hour > 0 ) {
            datatime = hour + "时" + min + "分";
         } else {
            datatime = min + "分";
         }
      }
      return datatime;
   }
@@ -142,6 +148,22 @@
      return format.format(ca.getTime());
   }
   /**
    * 指定日期加上天数后的日期
    *
    * @param num     增加的天数
    * @param nowDate 创建时间
    * @return
    * @throws ParseException
    */
   public static Date plusDayDate(int num, Date currdate) {
      Calendar ca = Calendar.getInstance();
      ca.setTime(currdate);
      ca.add(Calendar.DATE, num);
      return ca.getTime();
   }
   /**
    * 指定日期减去天数后的日期
@@ -192,16 +214,31 @@
    * @return 相差天数
    * @throws ParseException calendar 对日期进行时间操作 getTimeInMillis() 获取日期的毫秒显示形式
    */
   public static int daysBetween(Date smdate, Date bdate) throws ParseException {
   public static int daysBetween(Date minDate, Date maxDate) throws ParseException {
      Calendar cal = Calendar.getInstance();
      cal.setTime(smdate);
      long time1 = cal.getTimeInMillis();
      cal.setTime(bdate);
      long time2 = cal.getTimeInMillis();
      long between_days = (time2 - time1) / (1000 * 3600 * 24);
      cal.setTime(minDate);
      long minTime = cal.getTimeInMillis();
      cal.setTime(maxDate);
      long maxTime = cal.getTimeInMillis();
      long between_days = (maxTime - minTime) / (1000 * 3600 * 24);
      return Integer.parseInt(String.valueOf(between_days));
   }
   /**
    * 字符串日期格式的计算
    *
    * @param smdate
    * @param bdate
    * @return  单位天数
    * @throws ParseException
    */
   public static int daysBetween2(Date minDate, Date maxDate) throws ParseException {
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
      return daysBetween(sdf.format(minDate), sdf.format(maxDate));
   }
   /**
    * 字符串日期格式的计算
    * 
@@ -210,14 +247,14 @@
    * @return
    * @throws ParseException
    */
   public static int daysBetween(String smdate, String bdate) throws ParseException {
   public static int daysBetween(String minDate, String maxDate) throws ParseException {
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
      Calendar cal = Calendar.getInstance();
      cal.setTime(sdf.parse(smdate));
      long time1 = cal.getTimeInMillis();
      cal.setTime(sdf.parse(bdate));
      long time2 = cal.getTimeInMillis();
      long between_days = (time2 - time1) / (1000 * 3600 * 24);
      cal.setTime(sdf.parse(minDate));
      long minTime = cal.getTimeInMillis();
      cal.setTime(sdf.parse(maxDate));
      long maxTime = cal.getTimeInMillis();
      long between_days = (maxTime - minTime) / (1000 * 3600 * 24);
      return Integer.parseInt(String.valueOf(between_days));
   }