Dateutils的工具类

package cn.com.hongyitech.accountsystem.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**

  • @Description: 新特性的时间处理工具类

  • @Author: hl

  • @CreateDate: 2019/1/7 9:33

  • @UpdateUser: hl

  • @UpdateDate: 2019/1/7 9:33

  • @UpdateRemark: 修改内容

  • @Version: 1.0

  • 适用jdk 8及以上版本
    */
    public class DateUtil {

    /**

    • 根据传入的日期获取字符串形式的年月日
    • date—>【yyyy-mm-dd】
    • @return
    • @throws
    • @author hl
    • @date 2019/1/7 9:34
      */
      public static String dateToYMD(Date date) {
      Instant instant = date.toInstant();
      ZoneId zone = ZoneId.systemDefault();
      LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
      LocalDate localDate = localDateTime.toLocalDate();
      return localDate.toString();
      }

    /**

    • 根据传入的日期格式化成需要的样式
    • @return yyyy-MM-dd HH:mm:ss
    • @throws
    • @author hl
    • @date 2019/1/7 9:46
      */
      public static String dateToYMDHMS(Date date) {
      Instant instant = date.toInstant();
      ZoneId zone = ZoneId.systemDefault();
      LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
      DateTimeFormatter dateTimeFormater = DateTimeFormatter.ofPattern(“yyyy-MM-dd HH:mm:ss”);
      return dateTimeFormater.format(localDateTime);
      }

    /**

    • 根据传入的日期格式化成需要的样式
    • @return yyyy年MM月dd日hh时mm分ss秒
    • @throws
    • @author hl
    • @date 2019/1/7 9:49
      */
      public static String dateToYMDhms(Date date) {
      Instant instant = date.toInstant();
      ZoneId zone = ZoneId.systemDefault();
      LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
      DateTimeFormatter dateTimeFormater = DateTimeFormatter.ofPattern(“yyyy年MM月dd日HH时mm分ss秒”);
      return dateTimeFormater.format(localDateTime);
      }

    public static String date2YMDhms(Date date) {
    return date2YMDhmsss(date);
    }

    /**

    • 根据传入的日期转成带毫秒值的字符串格式
    • @return yyyyMMddhhmmssSSS
    • @throws
    • @author hl
    • @date 2019/1/7 9:52
      */
      public static String date2YMDhmsss(Date date) {
      Instant instant = date.toInstant();
      ZoneId zone = ZoneId.systemDefault();
      LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
      DateTimeFormatter dateTimeFormater = DateTimeFormatter.ofPattern(“yyyyMMddHHmmssSSS”);
      return dateTimeFormater.format(localDateTime);
      }

    /**

    • 字符串形式日期转换成标准日期 String --> Date “yyyy-MM-dd”–>yyyy-MM-dd

    • @return

    • @throws

    • @author hl

    • @date 2019/1/7 9:55
      */
      public static LocalDate YMDToDate(String date) {

      return LocalDate.parse(date, DateTimeFormatter.ofPattern(“yyyy-MM-dd”));
      }

    /**

    • 字符串形式日期转换成标准日期 String --> Date “yyyy-MM-dd HH:mm:ss”–>yyyy-MM-dd HH:mm:ss
    • @return
    • @throws
    • @author hl
    • @date 2019/1/7 10:24
      */
      public static LocalDateTime YMDHMSToDate(String date) {
      DateTimeFormatter df = DateTimeFormatter.ofPattern(“yyyy-MM-dd HH:mm:ss”);
      return LocalDateTime.parse(date, df);
      }

    /**

    • Date转LocalDateTime
    • @return
    • @throws
    • @author hl
    • @date 2019/1/7 10:35
      */
      public static LocalDateTime DateToLocalDateTime(Date date) {
      Instant instant = date.toInstant();
      ZoneId zone = ZoneId.systemDefault();
      return LocalDateTime.ofInstant(instant, zone);
      }

    /**

    • LocalDateTime转Date
    • @return
    • @throws
    • @author hl
    • @date 2019/1/7 10:35
      */
      public static Date DateTime2Date(LocalDateTime localDateTime) {
      ZoneId zone = ZoneId.systemDefault();
      Instant instant = localDateTime.atZone(zone).toInstant();
      return Date.from(instant);
      }

    /**

    • LocalDateTime转LocalDate
    • @return
    • @throws
    • @author hl
    • @date 2019/1/7 10:40
      */
      public static LocalDate LocalDateTimeToLocalDate(LocalDateTime localDateTime) {
      return localDateTime.toLocalDate();

    }

    /**

    • 获取当前日期的年
    • @return
    • @throws
    • @author hl
    • @date 2019/1/7 10:48
      */
      public static String getTheYear() {
      return Integer.toString(LocalDate.now().getYear());
      }

    /**

    • 字符串转LocalDateTime
    • 比如yyyy-mm-dd HH:mm:ss 转LocalDateTime
    • @return
    • @throws
    • @author hl
    • @date 2019/1/7 10:55
      */
      public static LocalDateTime localDateTimeParse(String time, String pattern) {
      DateTimeFormatter df = DateTimeFormatter.ofPattern(pattern);
      return LocalDateTime.parse(time, df);
      }

    /**

    • 判断两个时间之间的间隔分钟数是否大于某个值,大于返回true
    • 时间格式必须是yyyy-mm-dd HH:mm:ss
    • @return
    • @throws
    • @author hl
    • @date 2019/1/7 10:50
      */
      public static boolean checkBwintervalNumberMinBegTmAndEndTm(String begTm, String endTm, int intervalNumber) {
      Long minutesBetween = getMinutesBetween(begTm, endTm);
      Long intervalNumberLong = new Long(intervalNumber);
      return minutesBetween.longValue() >= intervalNumberLong.longValue() ? true : false;
      }

    /**

    • 起始时间与结束时间间隔不能少于40分钟
    • @return 开始+40分钟<结束返回true
    • @throws
    • @author hl
    • @date 2019/1/7 11:34
      */
      public static boolean checkBw40MinBegTmAndEndTm(String begTm, String endTm) {
      return checkBwintervalNumberMinBegTmAndEndTm(begTm, endTm, 40);
      }

    /**

    • 判断两个字符串时间之间相隔的分钟数,如果为负数,说明前面的时间比后面的时间要晚
    • 时间格式必须是yyyy-mm-dd HH:mm:ss
    • @return
    • @throws
    • @author hl
    • @date 2019/1/7 11:01
      */
      public static Long getMinutesBetween(String begTm, String endTm) {
      LocalDateTime start = localDateTimeParse(begTm, “yyyy-MM-dd HH:mm:ss”);
      LocalDateTime end = localDateTimeParse(endTm, “yyyy-MM-dd HH:mm:ss”);
      Duration between = Duration.between(start, end);
      return between.toMinutes();//相差分钟数
      }

/**

  • 比较两个时间的大小
  • 时间格式必须是yyyy-mm-dd HH:mm:ss
  • @author hl
  • @return 开始<结束返回 true
    */
    public static boolean checkBegTmAndEndTm(String begTm, String endTm) {
    LocalDateTime start = localDateTimeParse(begTm, “yyyy-MM-dd HH:mm:ss”);
    LocalDateTime end = localDateTimeParse(endTm, “yyyy-MM-dd HH:mm:ss”);
    return start.compareTo(end) < 0 ? true : false;
    }
/**
 * 获取指定日期后一星期的字符串格式日期
 *
 * @return
 * @throws
 * @author hl
 * @date 2019/1/7 12:02
 */
public static String getAfterWeekDateYMDHMS(Date date) {
    LocalDateTime localDateTime = DateToLocalDateTime(date);
    LocalDateTime AfterWeek = localDateTime.plusDays(7);
    return localDateTime2YMDHMS(AfterWeek);
}

/**
 * localDateTime2YMDHMS
 * 返回的格式为 yyyy-MM-dd HH:mm:ss
 *
 * @return
 * @throws
 * @author hl
 * @date 2019/1/7 12:04
 */
public static String localDateTime2YMDHMS(LocalDateTime localDateTime) {
    DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    return df.format(localDateTime);
}

/**
 * 计算两个日期之间的间隔天数,只取整数部分
 * 时间格式必须是yyyy-mm-dd HH:mm:ss
 *
 * @return
 * @throws
 * @author hl
 * @date 2019/1/7 11:40
 */
public static int checkDaysBegTmAndEndTm(String begTm, String endTm) {
    LocalDateTime start = localDateTimeParse(begTm, "yyyy-MM-dd HH:mm:ss");
    LocalDateTime end = localDateTimeParse(endTm, "yyyy-MM-dd HH:mm:ss");
    Duration between = Duration.between(start, end);
    int betweenDays = (int) between.toDays();
    return Math.abs(betweenDays);
}

/**
 * 获取两个时间的时间差(小时)
 *
 * @return
 * @throws
 * @author hl
 * @date 2019/1/7 13:48
 */
public static int getHourFromTwoTime(String begTm, String endTm) {
    LocalDateTime start = localDateTimeParse(begTm, "yyyy-MM-dd HH:mm:ss");
    LocalDateTime end = localDateTimeParse(endTm, "yyyy-MM-dd HH:mm:ss");
    Duration between = Duration.between(start, end);
    int betweenDays = (int) between.toHours();
    return Math.abs(betweenDays);
}

/**
 * 返回指定时间的零时零分零秒的时间
 *
 * @param date
 * @return yyyy-MM-dd 00:00:00
 */
public static String getBeforOneDateYMDHMS(Date date) {
    String str = dateToYMD(date);
    LocalDate localDate = LocalDate.parse(str);
    LocalDateTime localDateTime = localDate.atStartOfDay();
    DateTimeFormatter dateTimeFormater = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    return dateTimeFormater.format(localDateTime);
}

/**
 * 返回指定时间的23点59分59秒
 *
 * @param date
 * @return yyyy-MM-dd 00:00:00
 */
public static String getEndOneDateYMDHMS(Date date) {
    String str = dateToYMD(date);
    LocalDate localDate = LocalDate.parse(str);
    LocalDateTime localDateTime = localDate.atStartOfDay();
    localDateTime = localDateTime.plusHours(23);
    localDateTime = localDateTime.plusMinutes(59);
    localDateTime = localDateTime.plusSeconds(59);
    DateTimeFormatter dateTimeFormater = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    return dateTimeFormater.format(localDateTime);
}

/**
 * 当前时间与begTm的前十分钟的大小
 *
 * @return 两个时间相隔的毫秒数
 * @throws
 * @author hl
 * @date 2019/1/7 12:47
 */
public static long checkDaysBeforeTenMinuteCurrTmAndBegTm(String toDate, String begTm) {
    LocalDateTime localDateTime = localDateTimeParse(begTm, "yyyy-MM-dd HH:mm:ss");
    LocalDateTime toDateLocalDateTime = localDateTimeParse(toDate, "yyyy-MM-dd HH:mm:ss");
    LocalDateTime localDateTimeTenMinuteAfter = localDateTime.plusMinutes(10);
    Duration between = Duration.between(toDateLocalDateTime, localDateTimeTenMinuteAfter);
    return between.toMillis();
}

/**
 * 获取指定日期yyyy-mm-dd HH:mm:ss的前后几个月,前用负数表示2月份前的一个月就是-1
 *
 * @return
 * @throws
 * @author hl
 * @date 2019/1/7 13:36
 */
public static String getBeforSomeMonthFromCurrent(String currentTm, int month) {
    LocalDateTime localDateTime = localDateTimeParse(currentTm, "yyyy-MM-dd HH:mm:ss");
    LocalDateTime localDateTimeBeforSomeMonth = localDateTime.plusMonths(month);
    return localDateTime2YMDHMS(localDateTimeBeforSomeMonth);
}

/**
 * 获取当前时间前一个月的日期yyyy-mm-dd HH:mm:ss格式
 *
 * @return
 * @throws
 * @author hl
 * @date 2019/1/7 13:44
 */
public static String getBeforOneMonthFromCurrent() {
    LocalDateTime localDateTime = LocalDateTime.now();
    LocalDateTime localDateTimeBeforOneMonth = localDateTime.plusMonths(-1);
    return localDateTime2YMDHMS(localDateTimeBeforOneMonth);
}

/**
 * 获取当前时间后一个月的日期yyyy-mm-dd HH:mm:ss格式
 *
 * @return
 * @throws
 * @author hl
 * @date 2019/1/7 13:46
 */
public static String getAfterOneMonthFromCurrent() {
    LocalDateTime localDateTime = LocalDateTime.now();
    LocalDateTime localDateTimeAfterOneMonth = localDateTime.plusMonths(1);
    return localDateTime2YMDHMS(localDateTimeAfterOneMonth);
}

/**
 * 获取当前时间的几个月之前或之后的时间值yyyy-mm-dd HH:mm:ss格式
 * 当前时间前传负数,后传正数
 *
 * @return
 * @throws
 * @author hl
 * @date 2019/1/7 13:58
 */
public static String getBeforeSomeMonthFromCurrent(int month) {
    LocalDateTime localDateTime = LocalDateTime.now();
    LocalDateTime localDateTimeAfterOneMonth = localDateTime.plusMonths(month);
    return localDateTime2YMDHMS(localDateTimeAfterOneMonth);
}
/**
 * 获取当前时间后N个小时的字符串时间
 * yyyy-mm-dd HH:mm:ss
 * @author      hl
 * @return
 * @exception
 * @date       2019/1/7 15:03
 */
public static String getAfterTimeDateYMDHMS(int i) {
    LocalDateTime localDateTime = LocalDateTime.now();
    LocalDateTime localDateTimeAdd = localDateTime.plusHours(i);
    return localDateTime2YMDHMS(localDateTimeAdd);
}

/**
 * @param str     需要校验的时间字符串 2018-09-10 或者2018-09-10 12:23:38
 * @param pattern 时间的格式 yyyy-MM-dd 或者yyyy-MM-dd HH:mm:ss(24小时制)
 * @return
 */
public static boolean isValidDate(String str, String pattern) {
    boolean convertSuccess = true;
    // 指定日期格式为四位年/两位月份/两位日期,注意yyyy/MM/dd区分大小写;
    SimpleDateFormat format = new SimpleDateFormat(pattern);
    try {
        // 设置lenient为false.
        // 否则SimpleDateFormat会比较宽松地验证日期,比如2007/02/29会被接受,并转换成2007/03/01
        format.setLenient(false);
        format.parse(str);
    } catch (ParseException e) {
        // e.printStackTrace();
        // 如果throw java.text.ParseException或者NullPointerException,就说明格式不对
        convertSuccess = false;
    }
    return convertSuccess;
}

public static void main(String[] args) {
    System.out.println("dateToYMD: " + dateToYMD(new Date()));
    System.out.println("dateToYMDHMS: " + dateToYMDHMS(new Date()));
    System.out.println("dateToYMDhms: " + dateToYMDhms(new Date()));
    System.out.println("date2YMDhmsss: " + date2YMDhmsss(new Date()));
    System.out.println("YMDToDate: " + YMDToDate("2019-01-07"));
    System.out.println("YMDHMSToDate: " + YMDHMSToDate("2019-01-07 00:00:09"));
    System.out.println("DateToLocalDateTime: " + DateToLocalDateTime(new Date()));
    System.out.println("DateTime2Date: " + DateTime2Date(LocalDateTime.now()));
    System.out.println("LocalDateTimeToLocalDate: " + LocalDateTimeToLocalDate(LocalDateTime.now()));
    System.out.println("getTheYear: " + getTheYear());
    System.out.println("localDateTimeParse: " + localDateTimeParse("2019-01-07 00:50:00", "yyyy-MM-dd HH:mm:ss"));
    System.out.println("checkBwintervalNumberMinBegTmAndEndTm: " + checkBwintervalNumberMinBegTmAndEndTm("2019-01-07 00:00:00", "2019-01-07 00:41:00", 40));
    System.out.println("checkBegTmAndEndTm: " + checkBegTmAndEndTm("2019-01-07 00:30:00", "2019-01-07 00:41:00"));
    System.out.println("checkDaysBegTmAndEndTm: " + checkDaysBegTmAndEndTm("2019-01-09 00:00:00", "2019-01-07 00:00:50"));
    System.out.println("getAfterWeekDateYMDHMS: " + getAfterWeekDateYMDHMS(new Date()));
    System.out.println("getBeforOneMonthFromCurrent: " + getBeforSomeMonthFromCurrent("2019-01-07 00:00:50", 1));
    System.out.println("getAfterOneMonthFromCurrent: " + getAfterOneMonthFromCurrent());
    System.out.println("getBeforOneMonthFromCurrent: " + getBeforOneMonthFromCurrent());
    System.out.println("getBeforOneDateYMDHMS: " + getBeforOneDateYMDHMS(new Date()));
    System.out.println("getBeforeSomeMonthFromCurrent: " + getBeforeSomeMonthFromCurrent(3));
    System.out.println("getHourFromTwoTime: " + getHourFromTwoTime("2019-01-09 00:00:00", "2019-01-07 00:00:50"));
    System.out.println("checkBw40MinBegTmAndEndTm: " + checkBw40MinBegTmAndEndTm("2019-01-09 00:00:00", "2019-01-07 00:00:50"));
    System.out.println("checkDaysBeforeTenMinuteCurrTmAndBegTm: " + checkDaysBeforeTenMinuteCurrTmAndBegTm("2019-01-07 00:00:50", "2019-01-07 00:00:50"));
    System.out.println("getEndOneDateYMDHMS: " + getEndOneDateYMDHMS(new Date()));
    System.out.println("isValidDate: " + isValidDate("2019-01-07 00:00:50", "yyyy-MM-dd HH:mm:ss"));


}

}

你可能感兴趣的:(util)