zoukankan      html  css  js  c++  java
  • Java8日期时间——LocalDateTime的使用以及相互转换

    日期时间处理

    Java8内每个类含义

    在 Java8 之前操作时间,用的都是 Date 和 Calendar 类,但这两个类,操作起来及其繁琐,且在时间转换、时区转换的时候也很麻烦,因此 JDK 官方在 8 之后,引入了 LocalDateTime 以及相关类,通过新的类来定义和操作时间,也十分的简单清晰,下面我们就来我看一下如何操作。

    1. Instant: 时间戳
    2. Duration: 持续时间, 时间差
    3. LocalDate: 只包含⽇期, ⽐如: 2016-10-20
    4. LocalTime: 只包含时间, ⽐如: 231210
    5. LocalDateTime: 包含⽇期和时间, ⽐如: 2016-10-20 231421
    6. Period: 时间段
    7. ZoneOffset: 时区偏移量, ⽐如: +8:00
    8. ZonedDateTime: 带时区的时间
    9. Clock: 时钟, ⽐如获取⽬前美国纽约的时间

    代码实现

    localdatetime -> 其他类型

     // =================================================================================
    
        public static Date localDateTimeToDate(LocalDateTime localDateTime) {
            Date date = Date.from(localDateTime.toInstant(ZoneOffset.ofHours(8)));
            return date;
        }
    
        public static Date localDateTimeToDate(LocalDateTime localDateTime, ZoneOffset zoneOffset) {
            Date date = Date.from(localDateTime.toInstant(zoneOffset));
            return date;
        }
    
        public static long localDateTimeToTimestamp(LocalDateTime localDateTime) {
            long timestamp = localDateTime.toInstant(ZoneOffset.ofHours(8)).getEpochSecond();
            return timestamp;
        }
    
        public static long localDateTimeToTimestamp(LocalDateTime localDateTime, ZoneOffset zoneOffset) {
            long timestamp = localDateTime.toInstant(zoneOffset).getEpochSecond();
            return timestamp;
        }
    
    
        public static String localDateTimeToString(LocalDateTime localDateTime) {
            DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            String string = dateTimeFormatter.format(localDateTime);
            return string;
        }
    
        public static String localDateTimeToString(LocalDateTime localDateTime, DateTimeFormatter dateTimeFormatter) {
            String string = dateTimeFormatter.format(localDateTime);
            return string;
        }
    
        public static String localDateTimeToString(LocalDateTime localDateTime, String pattern) {
            DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern);
            String string = dateTimeFormatter.format(localDateTime);
            return string;
        }
    

    date -> 其他类型

    // =================================================================================
        public static long dateToTimeStamp(Date date) {
            return date.getTime();
        }
    
        public synchronized static String dateToString(Date date, SimpleDateFormat simpleDateFormat) {
            return simpleDateFormat.format(date);
        }
    
        public static String dateToString(Date date) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return simpleDateFormat.format(date);
        }
    
        public static LocalDateTime dateToLocalDateTime(Date date) {
            LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
            return localDateTime;
        }
    
        public static LocalDateTime dateToLocalDateTime(Date date, ZoneId zoneId) {
            LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), zoneId);
            return localDateTime;
        }
    

    timestamp -> 其他类型

        // =================================================================================
        public static Date timestampToDate(long timestamp) {
            return new Date(timestamp);
        }
    
        public static LocalDateTime timestampToLocalDateTime(long timestamp) {
            LocalDateTime localDateTime = Instant.ofEpochSecond(timestamp).atZone(ZoneOffset.ofHours(8)).toLocalDateTime();
            return localDateTime;
        }
    

    string -> 其他类型

        // =================================================================================
        public static Date stringToDate(String string, SimpleDateFormat simpleDateFormat) throws ParseException {
            return simpleDateFormat.parse(string);
        }
    
        public static Date stringToDate(String string) throws ParseException {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return simpleDateFormat.parse(string);
        }
    
        public static LocalDateTime stringToLocalDateTime(String string) {
            DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            LocalDateTime localDateTime = LocalDateTime.parse(string, dateTimeFormatter);
            return localDateTime;
        }
    
        public static LocalDateTime stringToLocalDateTime(String string, DateTimeFormatter dateTimeFormatter) {
            LocalDateTime localDateTime = LocalDateTime.parse(string, dateTimeFormatter);
            return localDateTime;
        }
    
        public static long stringToTimestamp(String string) {
            DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            LocalDateTime localDateTime = LocalDateTime.parse(string, dateTimeFormatter);
            long timestamp = localDateTime.toInstant(ZoneOffset.ofHours(8)).getEpochSecond();
            return timestamp;
        }
    

    公众号截图




    文章在公众号「iceWang」第一手更新,有兴趣的朋友可以关注公众号,第一时间看到笔者分享的各项知识点,谢谢!笔芯!

  • 相关阅读:
    hadoop(五)scp命令copy文件和配置(完全分布式准备二)|7
    hadoop(四)centos7克隆|静态ip|机器名|映射关系|别名配置(完全分布式准备一)|6
    大数据及hadoop简要概念
    hadoop(三)伪分布模式hdfs文件处理|5
    Hadoop(二) 单节点案例grep和wordcount|4
    centos7 ip/映射/机器名变更/克隆(克隆后配置修改)|2
    centos7 NAT链接配置(静态ip/修改网卡名为eth0)|1
    Hadoop(一) centos7 jdk安装,hadoop安装|3
    hive常用函数五
    hive常用函数四
  • 原文地址:https://www.cnblogs.com/JRookie/p/13753631.html
Copyright © 2011-2022 走看看