美文网首页
时间处理工具类 基于jdk1.8

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

作者: LegendaryTao | 来源:发表于2019-12-17 15:47 被阅读0次
    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;
            }
    }
    

    相关文章

      网友评论

          本文标题:时间处理工具类 基于jdk1.8

          本文链接:https://www.haomeiwen.com/subject/twnsnctx.html