Date日期工具类1

/* *
 *类名:UtilDate
 *详细:工具类,可以用作获取系统日期等
 *说明:
 */
public class UtilDate {

  /**
   * 年月日时分秒(无下划线) yyyyMMddHHmmss
   */
  public static final String dtLong = "yyyyMMddHHmmss";

  /**
   * 完整时间 yyyy-MM-dd HH:mm:ss
   */
  public static final String simple = "yyyy-MM-dd HH:mm:ss";

  public static final String simpleX = "yyyy/MM/dd HH:mm:ss";

  /**
   * 年月日(无下划线) yyyyMMdd
   */
  public static final String dtShort = "yyyyMMdd";

  /**
   * 年月(无下划线) yyyyMM
   */
  public static final String yearMonth = "yyyyMM";

  /**
   * 年月日(无下划线) yyyy-MM-dd
   */
  public static final String simplebr = "yyyy-MM-dd";

  /**
   * 年月日(无下划线) yyyy/MM/dd
   */
  public static final String dtShortSlash = "yyyy/MM/dd";


  /**
   * @return 以yyyyMMddHHmmss为格式的当前系统时间
   */
  public static String getOrderNum() {
    Date date = new Date();
    DateFormat df = new SimpleDateFormat(dtLong);
    return df.format(date);
  }

  /**
   * @return 以yyyyMMddHHmmss为格式的指定时间
   */
  public static String getOrderNum(Date date) {
    if (StringUtils.isBlank(String.valueOf(date))) {
      return null;
    }
    DateFormat df = new SimpleDateFormat(dtLong);
    return df.format(date);
  }

  /**
   * 获取系统当前日期(精确到毫秒),格式:yyyy-MM-dd HH:mm:ss
   *
   * @return
   */
  public static String getDateFormatter() {
    Date date = new Date();
    DateFormat df = new SimpleDateFormat(simple);
    return df.format(date);
  }

  /**
   * 获取指定日期(精确到毫秒),格式:yyyy-MM-dd HH:mm:ss
   *
   * @return
   */
  public static String getDateFormatter(Date date) {
    if (StringUtils.isBlank(String.valueOf(date))) {
      return null;
    }
    DateFormat df = new SimpleDateFormat(simple);
    return df.format(date);
  }

  /**
   * 获取系统当期年月日(精确到天),格式:yyyyMMdd
   *
   * @return
   */
  public static String getDate() {
    Date date = new Date();
    DateFormat df = new SimpleDateFormat(dtShort);
    return df.format(date);
  }

  /**
   * 获取指定年月日(精确到天),格式:yyyyMMdd
   *
   * @return
   */
  public static String getDate(Date date) {
    if (StringUtils.isBlank(String.valueOf(date))) {
      return null;
    }
    DateFormat df = new SimpleDateFormat(dtShort);
    return df.format(date);
  }

  /**
   * 获取系统当期年月日(精确到天),格式:yyyy-MM-dd
   *
   * @return
   */
  public static String getDatebr() {
    Date date = new Date();
    DateFormat df = new SimpleDateFormat(simplebr);
    return df.format(date);
  }

  /**
   * 获取指定年月日(精确到天),格式:yyyy-MM-dd
   *
   * @return
   */
  public static String getDatebr(Date date) {
    if (StringUtils.isBlank(String.valueOf(date))) {
      return null;
    }
    DateFormat df = new SimpleDateFormat(simplebr);
    return df.format(date);
  }

  /**
   * 获取系统当期年月日(精确到天),格式:yyyy/MM/dd
   *
   * @return
   */
  public static String dtShortSlash() {
    Date date = new Date();
    DateFormat df = new SimpleDateFormat(dtShort);
    return df.format(date);
  }

  /**
   * 获取指定日期年月日(精确到天),格式:yyyy/MM/dd
   *
   * @return
   */
  public static String dtShortSlash(Date date) {
    if (StringUtils.isBlank(String.valueOf(date))) {
      return null;
    }
    DateFormat df = new SimpleDateFormat(dtShort);
    return df.format(date);
  }

  /**
   * 获取指定日期的指定格式
   *
   * @return
   */
  public static String getFormatDate(String formatname, Date date) {
    if (StringUtils.isBlank(String.valueOf(date))) {
      return null;
    }
    DateFormat df = new SimpleDateFormat(formatname);
    return df.format(date);
  }

  /**
   * 获取指定日期格式,指定日期的Date类型(谁调用谁处理异常问题)
   *
   * @return
   */
  public static Date getDateForString(String formatname, String date) throws Exception {
    if (StringUtils.isBlank(date)) {
      return null;
    }
    DateFormat df = new SimpleDateFormat(formatname);
    return df.parse(date);
  }

  /**
   * 获取指定日期的Date类型(精确到秒,格式:yyyy-MM-dd HH:mm:ss)
   *
   * @return
   */
  public static Date getDateForSimple(String date) throws Exception {
    if (StringUtils.isBlank(date)) {
      return null;
    }
    DateFormat df = new SimpleDateFormat(simple);
    return df.parse(date);
  }

  /**
   * 获取指定日期的Date类型(精确到秒,格式:yyyy/MM/dd HH:mm:ss)
   *
   * @return
   */
  public static Date getDateForSimpleX(String date) throws Exception {
    if (StringUtils.isBlank(date)) {
      return null;
    }
    DateFormat df = new SimpleDateFormat(simpleX);
    return df.parse(date);
  }

  /**
   * 获取指定日期的Date类型(精确到秒,格式:yyyyMMddHHmmss)
   *
   * @return
   */
  public static Date getDateForDtLong(String date) throws Exception {
    if (StringUtils.isBlank(date)) {
      return null;
    }
    DateFormat df = new SimpleDateFormat(dtLong);
    return df.parse(date);
  }

  /**
   * 获取指定日期的Date类型(精确到天,格式:yyyyMMdd)
   *
   * @return
   */
  public static Date getDateForDtShort(String date) throws Exception {
    if (StringUtils.isBlank(date)) {
      return null;
    }
    DateFormat df = new SimpleDateFormat(dtShort);
    return df.parse(date);
  }

  /**
   * 获取指定日期的Date类型(精确到天,格式:yyyy-MM-dd)
   *
   * @return
   */
  public static Date getDateForSimplebr(String date) throws Exception {
    if (StringUtils.isBlank(date)) {
      return null;
    }
    DateFormat df = new SimpleDateFormat(simplebr);
    return df.parse(date);
  }

  /**
   * 获取指定日期的Date类型(精确到天,格式:yyyy/MM/dd)
   *
   * @return
   */
  public static Date getDateForDtShortSlash(String date) throws Exception {
    if (StringUtils.isBlank(date)) {
      return null;
    }
    DateFormat df = new SimpleDateFormat(dtShortSlash);
    return df.parse(date);
  }

  /**
   * 获取年月
   * 如果传值就按当前值
   * 若没传值按当前日期
   * @param time
   * @param originalFormat
   * @return
   */
  public static String getYearMonth(String time, String originalFormat) {
    SimpleDateFormat original = new SimpleDateFormat(originalFormat);
    SimpleDateFormat dateFormat = new SimpleDateFormat(yearMonth);
    Date date = new Date();
    if (StringUtils.isNotBlank(time)) {
      try {
        date = original.parse(time);
      } catch (ParseException e) {
        //e.printStackTrace();
      }
    }
    return dateFormat.format(date);
  }

  /**
   * 获取指定格式当前日期Date类型(谁调用谁处理异常问题)
   *
   * @return
   */
  public Date getFormatDate(String formatname) throws Exception {
    if (StringUtils.isBlank(formatname)) {
      return null;
    }
    DateFormat df = new SimpleDateFormat(formatname);
    return df.parse(df.format(new Date()));
  }

  /**
   * 产生随机的三位数
   *
   * @return
   */
  public static String getThree() {
    Random rad = new Random();
    return rad.nextInt(1000) + "";
  }

  /**
   * 计算两个时间之间相差多少天多少时多少分
   *
   * @return
   */
  public static String getDatePoor(Date startDate, Date endDate) {

    long nd = 1000 * 24 * 60 * 60;//每天毫秒数

    long nh = 1000 * 60 * 60;//每小时毫秒数

    long nm = 1000 * 60;//每分钟毫秒数

    long ns = 1000;//每秒毫秒数

    long diff = endDate.getTime() - startDate.getTime(); // 获得两个时间的毫秒时间差异

    long day = diff / nd;   // 计算差多少天

    long hour = diff % nd / nh; // 计算差多少小时

    long min = diff % nd % nh / nm;  // 计算差多少分钟

    long sec = diff % nd % nh % nm / ns;// 计算差多少秒//输出结果

    return day + "天" + hour + "小时" + min + "分钟" + sec + "秒";

  }

  /**
   * 判读时间差距:月
   */
  public static Long getMonths(Date startDate, Date endDate) {
    Calendar c1 = Calendar.getInstance();
    Calendar c2 = Calendar.getInstance();
    c1.setTime(startDate);
    c2.setTime(endDate);
    if (c1.getTimeInMillis() < c2.getTimeInMillis()) {
      return 0L;
    }
    int year1 = c1.get(Calendar.YEAR);
    int year2 = c2.get(Calendar.YEAR);
    int month1 = c1.get(Calendar.MONTH);
    int month2 = c2.get(Calendar.MONTH);
    int day1 = c1.get(Calendar.DAY_OF_MONTH);
    int day2 = c2.get(Calendar.DAY_OF_MONTH);
    // 获取年的差值 假设 d1 = 2015-8-16  d2 = 2011-9-30
    int yearInterval = year1 - year2;
    // 如果 d1的 月-日 小于 d2的 月-日 那么 yearInterval-- 这样就得到了相差的年数
    if (month1 < month2 || month1 == month2 && day1 < day2) {
      yearInterval--;
    }
    // 获取月数差值
    int monthInterval = (month1 + 12) - month2;
    if (day1 < day2) {
      monthInterval--;
    }
    monthInterval %= 12;
    return (long) (yearInterval * 12 + monthInterval);
  }

  /**
   * 判读时间差距:天
   */
  public static Long getDay(Date startDate, Date endDate) {
    DateFormat dateFormat = new SimpleDateFormat(simple);
    Long days = null;
    long diff = endDate.getTime() - startDate.getTime();
    days = diff / (1000 * 60 * 60 * 24);
    return days;
  }

  /**
   * 判读时间差距:时
   */
  public static Long geHour(Date startDate, Date endDate) {
    DateFormat dateFormat = new SimpleDateFormat(simple);
    Long hour = null;
    long diff = endDate.getTime() - startDate.getTime();
    hour = diff / (1000 * 60 * 60);
    return hour;
  }

  /**
   * 判读时间差距:分
   */
  public static Long geMin(Date startDate, Date endDate) {
    DateFormat dateFormat = new SimpleDateFormat(simple);
    Long min = null;
    long diff = endDate.getTime() - startDate.getTime();
    min = diff / (1000 * 60);
    return min;
  }


  /**
   * 判读时间差距:秒
   */
  public static Long geSec(Date startDate, Date endDate) {
    DateFormat dateFormat = new SimpleDateFormat(simple);
    Long seconds = null;
    long diff = endDate.getTime() - startDate.getTime();
    seconds = diff / (1000);
    return seconds;
  }

  /**
   * 日期相加减
   *
   * @param time 时间字符串 yyyy-MM-dd HH:mm:ss
   * @param num  加的数,-num就是减去
   * @return 减去相应的数量的年的日期
   * @throws ParseException
   */
  public static Date yearAddNum(Date time, Integer num) {
    //SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    //Date date = format.parse(time);

    Calendar calendar = Calendar.getInstance();
    calendar.setTime(time);
    calendar.add(Calendar.YEAR, num);
    Date newTime = calendar.getTime();
    return newTime;
  }

  /**
   * @param time 时间
   * @param num  加的数,-num就是减去
   * @return 减去相应的数量的月份的日期
   * @throws ParseException Date
   */
  public static Date monthAddNum(Date time, Integer num) {
    //SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    //Date date = format.parse(time);

    Calendar calendar = Calendar.getInstance();
    calendar.setTime(time);
    calendar.add(Calendar.MONTH, num);
    Date newTime = calendar.getTime();
    return newTime;
  }

  /**
   * @param time 时间
   * @param num  加的数,-num就是减去
   * @return 减去相应的数量的周的日期
   * @throws ParseException Date
   */
  public static Date weekAddNum(Date time, Integer num) {
    //SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    //Date date = format.parse(time);

    Calendar calendar = Calendar.getInstance();
    calendar.setTime(time);
    calendar.add(Calendar.WEEK_OF_MONTH, num);
    Date newTime = calendar.getTime();
    return newTime;
  }


  /**
   * @param time 时间
   * @param num  加的数,-num就是减去
   * @return 减去相应的数量的天的日期
   * @throws ParseException Date
   */
  public static Date dayAddNum(Date time, Integer num) {
    //SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    //Date date = format.parse(time);

    Calendar calendar = Calendar.getInstance();
    calendar.setTime(time);
    calendar.add(Calendar.DAY_OF_MONTH, num);
    Date newTime = calendar.getTime();
    return newTime;
  }

  /**
   * 获取本月第一天时间
   */
  public static Date getMonthStartDate() {
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.DAY_OF_MONTH, 1);
    return calendar.getTime();
  }

  /**
   * 获取本月最后一天
   */
  public static Date getMonthEndDate() {
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
    return calendar.getTime();
  }

  /**
   * 获取本周的开始时间
   */
  public static Date getBeginWeekDate() {
    Calendar cal = Calendar.getInstance();
    cal.setTime(new Date());
    int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
    //周日是1 ,周一是 2 ,周二是 3
    //所以,当周的第一天 = 当前日期 - 距离周一过了几天(周一过了0天,周二过了1天,   周日过了6天)
    // 2 - 周一的(dayofweek:2 )= 0
    // 2 - 周二的(dayofweek:3 )= -1
    //                      .
    //                      .
    // 2 - 周日的(dayofweek:1) = 1(这个是不符合的需要我们修改)===》2 - 周日的(dayofweek:1 ==》8 ) = -6
    if (dayofweek == 1) {
      dayofweek += 7;
    }
    cal.add(Calendar.DATE, 2 - dayofweek);
    return cal.getTime();
  }

  /**
   * 本周的结束时间
   * 开始时间 + 6天
   */
  public static Date getEndWeekDate() {
    Calendar cal = Calendar.getInstance();
    cal.setTime(new Date());
    int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
    if (dayofweek == 1) {
      dayofweek += 7;
    }
    cal.add(Calendar.DATE, 8 - dayofweek);//2 - dayofweek + 6
    return cal.getTime();
  }

  /**
   * @param
   * @return java.util.Date
   * @Description: 获取当天的开始时间
   * @Add_Date: 2021-10-29 15:49
   * @Modify_Date: 2021-10-29 15:49
   * @MetchName: getStartDate
   */
  public static Date getStartDate() {
    Calendar todayStart = Calendar.getInstance();
    todayStart.set(Calendar.HOUR, 0);//这里是12小时制的
    todayStart.set(Calendar.MINUTE, 0);
    todayStart.set(Calendar.SECOND, 0);
    todayStart.set(Calendar.MILLISECOND, 0);
    return todayStart.getTime();
  }

  public static Date getStartDate1() {
    Calendar todayStart = Calendar.getInstance();
    todayStart.set(Calendar.HOUR_OF_DAY,07);//这里获取的事件是24小时制的
    todayStart.set(Calendar.MINUTE, 0);
    todayStart.set(Calendar.SECOND, 0);
    todayStart.set(Calendar.MILLISECOND, 0);
    return todayStart.getTime();
  }

  /**
   * @param
   * @return java.util.Date
   * @Description: 获取当天的结束时间
   * @Add_Date: 2021-10-29 15:49
   * @Modify_Date: 2021-10-29 15:49
   * @MetchName: getStartDate
   */
  public static Date getEndDate() {
    Calendar todayEnd = Calendar.getInstance();
    todayEnd.set(Calendar.HOUR, 23);
    todayEnd.set(Calendar.MINUTE, 59);
    todayEnd.set(Calendar.SECOND, 59);
    todayEnd.set(Calendar.MILLISECOND, 999);
    return todayEnd.getTime();
  }
}
posted @ 2024-11-04 16:57  哩个啷个波  阅读(1)  评论(0编辑  收藏  举报