• LocalDateTime ZonedDateTime Instant 的相互转换


    LocalDateTime ZonedDateTime Instant 的相互转换

    1. 概念

    类名描述示例
    LocalDateTime本地日期时间2022-06-15T22:06:29.483
    ZonedDateTime带时区的日期时间2022-06-15T22:06:29.483+08:00[Asia/Shanghai]
    Instant时间戳1655301989483

    2. 常用java代码

    package com.ysx.utils.datetime;
    
    import java.time.Instant;
    import java.time.LocalDateTime;
    import java.time.ZoneId;
    import java.time.ZonedDateTime;
    
    /**
     * @author youngbear
     * @email youngbear@aliyun.com
     * @date 2022/6/26 22:22
     * @blog https://blog.csdn.net/next_second
     * @github https://github.com/YoungBear
     * @description LocalDateTime ZonedDateTime Instant 的相互转换
     */
    public class ConvertUtils {
    
        /**
         * 将 ZonedDateTime 转换为 LocalDateTime
         *
         * @param zonedDateTime 带时区的日期时间
         * @return 本地日期时间
         */
        public static LocalDateTime zoned2Local(ZonedDateTime zonedDateTime) {
            return zonedDateTime.toLocalDateTime();
        }
    
        /**
         * 将 LocalDateTime 转换为 ZonedDateTime
         *
         * @param localDateTime 本地日期时间
         * @param zoneId        时区
         * @return 带时区的日期时间
         */
        public static ZonedDateTime local2Zoned(LocalDateTime localDateTime, ZoneId zoneId) {
            return ZonedDateTime.of(localDateTime, zoneId);
        }
    
        /**
         * 将 Instant 转换为 LocalDateTime
         *
         * @param instant 时间戳
         * @param zoneId  时区
         * @return 本地日期时间
         */
        public static LocalDateTime instant2local(Instant instant, ZoneId zoneId) {
            return instant.atZone(zoneId).toLocalDateTime();
        }
    
        /**
         * 将 LocalDateTime 转换为 Instant
         *
         * @param localDateTime 本地日期时间
         * @param zoneId        时区
         * @return 时间戳
         */
        public static Instant local2Instant(LocalDateTime localDateTime, ZoneId zoneId) {
            return localDateTime.atZone(zoneId).toInstant();
        }
    
    
        /**
         * 将 Instant 转换为 ZonedDateTime
         *
         * @param instant 时间戳
         * @param zoneId  时区
         * @return 带时区的日期时间
         */
        public static ZonedDateTime instant2Zoned(Instant instant, ZoneId zoneId) {
            return instant.atZone(zoneId);
        }
    
        /**
         * 将 ZonedDateTime 转换为 Instant
         *
         * @param zonedDateTime 带时区的日期时间
         * @return 时间戳
         */
        public static Instant zoned2Instant(ZonedDateTime zonedDateTime) {
            return zonedDateTime.toInstant();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83

    单元测试:

    package com.ysx.utils.datetime;
    
    import org.junit.Assert;
    import org.junit.Test;
    
    import java.time.Instant;
    import java.time.LocalDateTime;
    import java.time.ZoneId;
    import java.time.ZonedDateTime;
    import java.time.format.DateTimeFormatter;
    
    /**
     * @author youngbear
     * @email youngbear@aliyun.com
     * @date 2022/6/26 22:40
     * @blog https://blog.csdn.net/next_second
     * @github https://github.com/YoungBear
     * @description
     */
    public class ConvertUtilsTest {
    
        @Test
        public void testZonedDateTime() {
            String zonedDateTimeString = "2022-06-15T22:06:29.483+08:00[Asia/Shanghai]";
            DateTimeFormatter formatter = DateTimeFormatter.ISO_ZONED_DATE_TIME;
            ZonedDateTime zonedDateTime = ZonedDateTime.parse(zonedDateTimeString, formatter);
    
            // zoned2Local
            LocalDateTime localDateTime = ConvertUtils.zoned2Local(zonedDateTime);
            String localDateTimeString = "2022-06-15T22:06:29.483";
            Assert.assertEquals(localDateTimeString, localDateTime.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
            // zoned2Instant
            Instant instant = ConvertUtils.zoned2Instant(zonedDateTime);
            long timestamp = 1655301989483L;
            Assert.assertEquals(timestamp, instant.toEpochMilli());
        }
    
        @Test
        public void testLocalDateTime() {
            String localDateTimeString = "2022-06-15T22:06:29.483";
            LocalDateTime localDateTime = LocalDateTime.parse(localDateTimeString, DateTimeFormatter.ISO_LOCAL_DATE_TIME);
            ZoneId zoneId = ZoneId.of("Asia/Shanghai");
    
            // local2Zoned
            ZonedDateTime zonedDateTime = ConvertUtils.local2Zoned(localDateTime, zoneId);
            String zonedDateTimeString = "2022-06-15T22:06:29.483+08:00[Asia/Shanghai]";
            Assert.assertEquals(zonedDateTimeString, zonedDateTime.format(DateTimeFormatter.ISO_ZONED_DATE_TIME));
            // local2Instant
            Instant instant = ConvertUtils.local2Instant(localDateTime, zoneId);
            long timestamp = 1655301989483L;
            Assert.assertEquals(timestamp, instant.toEpochMilli());
        }
    
        @Test
        public void testInstant() {
            long timestamp = 1655301989483L;
            Instant instant = Instant.ofEpochMilli(timestamp);
            ZoneId zoneId = ZoneId.of("Asia/Shanghai");
    
            // instant2local
            LocalDateTime localDateTime = ConvertUtils.instant2local(instant, zoneId);
            String localDateTimeString = "2022-06-15T22:06:29.483";
            Assert.assertEquals(localDateTimeString, localDateTime.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
    
            // instant2Zoned
            ZonedDateTime zonedDateTime = ConvertUtils.instant2Zoned(instant, zoneId);
            String zonedDateTimeString = "2022-06-15T22:06:29.483+08:00[Asia/Shanghai]";
            Assert.assertEquals(zonedDateTimeString, zonedDateTime.format(DateTimeFormatter.ISO_ZONED_DATE_TIME));
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72

    相关文章

    1. LocalDateTime ZonedDateTime Instant 的相互转换

    2. 日期时间格式化与解析

    3. 带时区时间日期 ZonedDateTime

    4. 夏令时

    源代码地址

  • 相关阅读:
    【北亚数据恢复】不认盘的移动硬盘恢复数据案例&解决方案
    Spring事务传播机制
    网络工程实训&eNSP学习记录
    比亚迪越来越像华为?
    Deformable detr源码分析
    vue3源码解析
    常用docker命令 docker_cmd_sheet
    spoof_call的分析与改进
    ISO9001质量管理体系剖析
    基于移动GIS的环保生态管理系统
  • 原文地址:https://blog.csdn.net/Next_Second/article/details/125476198