zoukankan      html  css  js  c++  java
  • Java8 常用时间转换工具类

    时间工具类

    import java.time.*;
    import java.time.format.DateTimeFormatter;
    import java.util.Date;
    
    public class TimeUtil {
        /**
         * 北京时间
         */
        private static final ZoneOffset BEIJING_ZONE = ZoneOffset.of("+8");
    
        /**
         * 标准日期时间格式,精确到秒:yyyy-MM-dd HH:mm:ss
         */
        private static final String NORM_DATETIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
        /**
         * 标准日期格式:yyyy-MM-dd
         */
        private static final String NORM_DATE_PATTERN = "yyyy-MM-dd";
    
        /**
         * 转换成时间戳
         *
         * @param localDateTime 当前日期时间
         * @return 时间戳
         */
        public static Long toTimestamp(LocalDateTime localDateTime) {
            if (localDateTime == null) {
                return null;
            }
    
            return localDateTime.toEpochSecond(BEIJING_ZONE);
        }
    
        /**
         * 转换成时间戳
         *
         * @param localDate 当前日期
         * @return 时间戳
         */
        public static Long toTimestamp(LocalDate localDate) {
            if (localDate == null) {
                return null;
            }
    
            return localDate.atStartOfDay(BEIJING_ZONE).toEpochSecond();
        }
    
        /**
         * 转换成Date
         *
         * @param localDateTime 当前日期时间
         * @return Date日期
         */
        public static Date toDate(LocalDateTime localDateTime) {
            if (localDateTime == null) {
                return null;
            }
    
            ZonedDateTime zonedDateTime = localDateTime.atZone(BEIJING_ZONE);
            Instant instant = zonedDateTime.toInstant();
            return Date.from(instant);
        }
    
        /**
         * 转换成Date
         *
         * @param localDate 当前日期
         * @return Date日期
         */
        public static Date toDate(LocalDate localDate) {
            if (localDate == null) {
                return null;
            }
    
            ZonedDateTime zonedDateTime = localDate.atStartOfDay(BEIJING_ZONE);
            Instant instant = zonedDateTime.toInstant();
            return Date.from(instant);
        }
    
        /**
         * 转换成时间字符串
         *
         * @param localDateTime 当前日期时间
         * @return 时间字符串
         */
        public static String toString(LocalDateTime localDateTime) {
            if (localDateTime == null) {
                return null;
            }
    
            return localDateTime.format(DateTimeFormatter.ofPattern(NORM_DATETIME_PATTERN));
        }
    
        /**
         * 转换成时间字符串
         *
         * @param localDate 当前日期
         * @return 时间字符串
         */
        public static String toString(LocalDate localDate) {
            if (localDate == null) {
                return null;
            }
    
            return localDate.format(DateTimeFormatter.ofPattern(NORM_DATE_PATTERN));
        }
    
        /**
         * 转换成日期时间
         *
         * @param text 时间字符串
         * @return 日期时间
         */
        public static LocalDateTime parseLocalDateTime(CharSequence text) {
            if (text == null) {
                return null;
            }
    
            return LocalDateTime.parse(text, DateTimeFormatter.ofPattern(NORM_DATETIME_PATTERN));
        }
    
        /**
         * 转换成日期
         *
         * @param text 时间字符串
         * @return 日期
         */
        public static LocalDate parseLocalDate(CharSequence text) {
            if (text == null) {
                return null;
            }
    
            return LocalDate.parse(text, DateTimeFormatter.ofPattern(NORM_DATE_PATTERN));
        }
    
        public static void main(String[] args) {
            System.out.println(TimeUtil.toTimestamp(LocalDateTime.now()));
            System.out.println(TimeUtil.toTimestamp(LocalDate.now()));
            System.out.println(TimeUtil.toDate(LocalDateTime.now()));
            System.out.println(TimeUtil.toDate(LocalDate.now()));
            System.out.println(TimeUtil.toString(LocalDateTime.now()));
            System.out.println(TimeUtil.toString(LocalDate.now()));
            System.out.println(TimeUtil.parseLocalDateTime("2021-04-12 11:01:02"));
            System.out.println(TimeUtil.parseLocalDate("2021-04-12"));
        }
    
    }
    

    输出结果

    1618196882
    1618156800
    Mon Apr 12 11:08:02 CST 2021
    Mon Apr 12 00:00:00 CST 2021
    2021-04-12 11:08:02
    2021-04-12
    2021-04-12T11:01:02
    2021-04-12
    
  • 相关阅读:
    【Go】windows下搭建go语言编译环境
    【java回调】同步/异步回调机制的原理和使用方法
    【tomcat】tomcat远程调试
    【tomcat】获取访问者真实IP
    【深度学习学习记录】之一:开篇闲扯一些话
    【java】线程安全的整型类AtomicInteger
    【OpenStack】源码级深入了解删除虚拟机操作
    【Maven】maven的常用命令以及搭建maven私人仓库
    素 数 (第三届省赛)
    房间安排(第三届省赛)
  • 原文地址:https://www.cnblogs.com/fengzhentian/p/14647138.html
Copyright © 2011-2022 走看看