yujian
2018-12-29 e55de244fa08ee10b08ee316ae1aa64d2e55efe8
utils/src/main/java/org/yeshi/utils/DateUtil.java
@@ -5,63 +5,59 @@
import java.util.Calendar;
import java.util.Date;
public class DateUtil {
   public static String dateDiff(String startTime, String endTime) {
      String datatime = 0 + "天" + 0 + "小时" + 0 + "分钟" ;
      String datatime = 0 + "天" + 0 + "小时" + 0 + "分钟";
      SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
      try {
         long nm = 1000 * 60;// 一分钟的毫秒数
         long nh = 1000 * 60 * 60;// 一小时的毫秒数
         long nd = 1000 * 24 * 60 * 60;// 一天的毫秒数
         // 获得两个时间的毫秒时间差异
         long diff = sd.parse(endTime).getTime() - sd.parse(startTime).getTime();
         long day = diff / nd;// 计算差多少天
         long hour = diff % nd / nh;// 计算差多少小时
         long min = diff % nd % nh / nm;// 计算差多少分钟
         datatime = day + "天" + hour + "小时" + min + "分钟" ;
         datatime = day + "天" + hour + "小时" + min + "分钟";
      } catch (ParseException e) {
         e.printStackTrace();
      }
      return datatime;
   }
   public static String dateDiff2(Date startTime, Date endTime) throws Exception{
      String datatime = 0 + "天" + 0 + "小时" + 0 + "分钟" ;
   public static String dateDiff2(Date startTime, Date endTime) throws Exception {
      String datatime = 0 + "天" + 0 + "小时" + 0 + "分钟";
      long nm = 1000 * 60;// 一分钟的毫秒数
      long nh = 1000 * 60 * 60;// 一小时的毫秒数
      long nd = 1000 * 24 * 60 * 60;// 一天的毫秒数
      // 获得两个时间的毫秒时间差异
      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 + "分钟" ;
      datatime = day + "天" + hour + "小时" + min + "分钟";
      return datatime;
   }
   public String transferLongToDate(String dateFormat, Long millSec) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(millSec);
        return sdf.format(date);
    }
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      Date date = new Date(millSec);
      return sdf.format(date);
   }
   /**
    * 验证是否属于同一天
@@ -97,4 +93,45 @@
      }
   }
   /**
    * 指定日期加上天数后的日期
    *
    * @param num
    *            增加的天数
    * @param nowDate
    *            创建时间
    * @return
    * @throws ParseException
    */
   public static String plusDay(int num, String nowDate) throws ParseException {
      SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
      Date currdate = format.parse(nowDate);
      Calendar ca = Calendar.getInstance();
      ca.setTime(currdate);
      ca.add(Calendar.DATE, num);
      return format.format(ca.getTime());
   }
   /**
    * 指定日期减去天数后的日期
    *
    * @param num
    *            减去的天数
    * @param nowDate
    *            创建时间
    * @return
    * @throws ParseException
    */
   public static String reduceDay(int num, String nowDate) throws ParseException {
      SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
      Date currdate = format.parse(nowDate);
      Calendar ca = Calendar.getInstance();
      ca.setTime(currdate);
      ca.add(Calendar.DATE, -num); // 日期减 如果不够减会将月变动
      return format.format(ca.getTime());
   }
}