• 自备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;
        }
    }
  • 相关阅读:
    php发送邮件
    本地phpstudy集成环境中MYSQL在数据传输时报错- Unknown storage engine 'InnoDB'的解决办法
    一键分享到QQ空间、QQ好友、新浪微博、微信代码
    点分治_学习笔记+题目清单
    AtCoder Beginner Contest168-F (二维离散化)
    Codeforces 1354E(Graph Coloring,二分图+dp)
    Codeforces 832D(Misha, Grisha and Underground,LCA)
    Codeforces Round #643 (Div.2)
    Codeforces 909E(Coprocessor,双队列维护)
    Codeforces 949C(Data Center Maintenance,Tarjan缩点)
  • 原文地址:https://www.cnblogs.com/it-taosir/p/10130510.html
Copyright © 2020-2023  润新知