时间处理工具类 基于jdk1.8

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * title:    时间处理工具类 基于jdk1.8
 * Author:   hetao
 * Date:     2019/12/17 10:03
 */
public class LocalDateTimeUtil {

  private static DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    
  private static DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

  private static Pattern datePattern = Pattern.compile("^\\d{4}(\\-|\\/|.)\\d{1,2}\\1\\d{1,2}$");
    
  private static Pattern dateTimePattern = Pattern.compile("^(\\d{4})([/-]\\d{1,2}){2}\\s\\d{1,2}(:\\d{1,2}){2}$");

    /**
     * Date转换为LocalDateTime
     * @param date Date 类型
     */
    public static LocalDateTime dateToLocalDateTime(Date date){
        Instant instant = date.toInstant();//An instantaneous point on the time-line.(时间线上的一个瞬时点。)
        ZoneId zoneId = ZoneId.systemDefault();//A time-zone ID, such as {@code Europe/Paris}.(时区)
        LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();
        return localDateTime;
    }

    /**
     * LocalDateTime转换为Date
     * @param localDateTime LocalDateTime
     */
    public static Date localDateTimeToDate(LocalDateTime localDateTime) {
        ZoneId zoneId = ZoneId.systemDefault();
        ZonedDateTime zdt = localDateTime.atZone(zoneId);//Combines this date-time with a time-zone to create a  ZonedDateTime.
        Date date = Date.from(zdt.toInstant());
        return date;
    }

    /**
     * String转换为Date
     * @param dateStr String
     */
    public static Date strToDate(String dateStr) {
        LocalDateTime localDateTime = LocalDateTime.parse(dateStr,dateTimeFormatter);
        ZoneId zoneId = ZoneId.systemDefault();
        ZonedDateTime zdt = localDateTime.atZone(zoneId);//Combines this date-time with a time-zone to create a  ZonedDateTime.
        Date date = Date.from(zdt.toInstant());
        return date;
    }

    /**
     * 获取去年的今天
     * @param yearMonthly
     * @return
     */
    public static String lastOneYear(String yearMonthly) {
        LocalDate currMonthStart = LocalDate.parse(yearMonthly);
        LocalDate localDate = currMonthStart.minusYears(1);
        return localDate.format(dateFormatter);
    }

    /**
     * 获取上一个月份的今天
     * @param yearMonthly
     * @return
     */
    public static String lastOneMonth(String yearMonthly) {
        LocalDate currMonthStart = LocalDate.parse(yearMonthly);
        LocalDate localDate = currMonthStart.minusMonths(1);
        return localDate.format(dateFormatter);
    }

    /**
     * 检测字符串是否是日期格式
     * @param dateStr
     * @return
     */
    public static boolean isDate(String dateStr) {
      Matcher dateMatcher = datePattern.matcher(dateStr);
        Matcher dateTimeMatcher = dateTimePattern.matcher(dateStr);
        if (dateMatcher.matches() || dateTimeMatcher.matches()) {
            return true;
        } else {
            return false;
        }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容