zoukankan      html  css  js  c++  java
  • 自备LocalDateTime工具类

    package cn.zytao.taosir.common.utils;
    
    import java.time.Instant;
    import java.time.LocalDate;
    import java.time.LocalDateTime;
    import java.time.LocalTime;
    import java.time.ZoneId;
    import java.time.ZonedDateTime;
    import java.time.format.DateTimeFormatter;
    import java.util.Date;
    
    public class DateTimeUtils {
    
        public static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("HHmmss");
        public static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyyMMdd");
        public static final DateTimeFormatter DATETIME_FORMATTER =  DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        
        /**
         * 获取当前系统时间
         * @return
         */
        public static LocalTime getLocalTime() {
            return LocalTime.now();
        }
        
        /**
         * 获取当前系统日期
         * @return
         */
        public static LocalDate getLocalDate() {
            return LocalDate.now();
        }
        
        /**
         * 获取当前系统日期时间
         * @return
         */
        public static LocalDateTime getLocalDateTime() {
            return LocalDateTime.now();
        }
        
        /**
         * 获取当前系统时间字符串
         * @return
         */
        public static String getLocalTimeString() {
            return LocalTime.now().format(TIME_FORMATTER);
        }
        
        /**
         * 获取当前系统日期字符串
         * @return
         */
        public static String getLocalDateString() {
            return LocalDate.now().format(DATE_FORMATTER);
        }
        
        /**
         * 获取当前系统日期时间字符串
         * @return
         */
        public static String getLocalDateTimeString() {
            return LocalDateTime.now().format(DATETIME_FORMATTER);
        }
        
        /**
         * 字符串转LocalTime
         * @param time
         * @return
         */
        public static LocalTime  string2LocalTime(String time) {
            return LocalTime.parse(time, TIME_FORMATTER);
        }
        
        /**
         * 字符串转LocalDate
         * @param date
         * @return
         */
        public static LocalDate  string2LocalDate(String date) {
            return LocalDate.parse(date, DATE_FORMATTER);
        }
        
        /**
         * 字符串转LocalDateTime
         * @param dateTime
         * @return
         */
        public static LocalDateTime string2LocalDateTime(String dateTime) {
            return LocalDateTime.parse(dateTime, DATETIME_FORMATTER);
        }
        
        /**
         * Date转LocalDateTime
         * @param date
         * @return
         */
        public static LocalDateTime date2LocalDateTime(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;
        }
        
        /**
         * Date转LocalDate
         * @param date
         * @return
         */
        public static LocalDate date2LocalDate(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}.(时区)
            LocalDate localDate = instant.atZone(zoneId).toLocalDate();
            return localDate;
        }
        
        /**
         * Date转LocalDate
         * @param date
         * @return
         */
        public static LocalTime date2LocalTime(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}.(时区)
            LocalTime localTime = instant.atZone(zoneId).toLocalTime();
            return localTime;
        }
        
         /**
         * LocalDateTime转换为Date
         * @param localDateTime
         */
        public static Date localDateTime2Date(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;
        }
    }
  • 相关阅读:
    Activity(二)
    channelartlist标签的使用
    把数据保存到数据库附加表 `dede_addonarticle` 时出错,请把相关信息提交给DedeCms官方。Duplicate entry '2' for key 'PRIMARY'
    TP5.0验证器使用方法
    TP5.0登录验证码实现
    dede列表页限制标题长度
    dede搜索页做法
    表单正则验证简便方法
    解决织梦dedecms文档关键字(自动内链)php5.5以上失效的问题 urf-8版本的
    织梦dede解决“更新数据库archives表时出错"方法
  • 原文地址:https://www.cnblogs.com/it-taosir/p/10130510.html
Copyright © 2011-2022 走看看