IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Java知识库 -> ZonedDateTime、OffsetDateTime、LocalDateTime、Date之间如何相互转化 -> 正文阅读

[Java知识库]ZonedDateTime、OffsetDateTime、LocalDateTime、Date之间如何相互转化

具体理论网上有很多介绍了,本地不再提及;
本文全使用例子;
执行一下对比一下结果在理解是会有很大的收获。

ZonedDateTime、OffsetDateTime、LocalDateTime、instant、Date、timestamp之间如何相互转化

	public static final LocalDateTime LOCAL_DATE_TIME = LocalDateTime.of(2019, 7, 7, 20, 18, 18, 888);
    public static final ZonedDateTime ZONED_DATE_TIME = ZonedDateTime.of(2019, 7, 7, 20, 18, 18, 888, ZoneId.of("Asia/Tokyo"));
    public static final OffsetDateTime OFFSET_DATE_TIME = OffsetDateTime.of(2019, 7, 7, 20, 18, 18, 888, ZoneOffset.ofHours(9));
    protected static final Date DATE = new Date(1562501898000L);


    public void instantBuild() {
        System.out.println(Instant.now());
        System.out.println("----------");

        System.out.println(Instant.ofEpochMilli(1562501898000L));//2019-07-07T12:18:18Z
        System.out.println(Instant.ofEpochMilli(1562501898888L));//2019-07-07T12:18:18.888Z
        System.out.println(Instant.ofEpochSecond(1562501898));//2019-07-07T12:18:18Z
        System.out.println(Instant.ofEpochSecond(1562501898,888));//2019-07-07T12:18:18.000000888Z
        System.out.println("----------");
        System.out.println(ZONED_DATE_TIME.toInstant());//2019-07-07T11:18:18.000000888Z
        System.out.println(OFFSET_DATE_TIME.toInstant());//2019-07-07T11:18:18.000000888Z
        System.out.println(LOCAL_DATE_TIME.toInstant(ZoneOffset.ofHours(8)));//2019-07-07T12:18:18.000000888Z
        System.out.println(LOCAL_DATE_TIME.toInstant(ZoneOffset.UTC));//2019-07-07T20:18:18.000000888Z
        System.out.println("----------");
        System.out.println(DATE.toInstant());//2019-07-07T12:18:18Z
        System.out.println("----------");

        System.out.println(Instant.parse("2019-07-07T20:18:18.000000888Z"));//2019-07-07T20:18:18.000000888Z
        System.out.println(Instant.parse("2019-07-07T20:18:18Z"));//2019-07-07T20:18:18Z
        System.out.println("----------");
    }


    public void dateBuild() throws ParseException {
        System.out.println(new Date(1562501898000L));//Sun Jul 07 20:18:18 CST 2019
        System.out.println(new Date());

        Date date1 = new Timestamp(1562501898000L);//2019-07-07 20:18:18.0
        System.out.println(date1);
        System.out.println(new Date(new Timestamp(1562501898000L).getTime()));//Sun Jul 07 20:18:18 CST 2019

        System.out.println(Calendar.getInstance().getTime());
        System.out.println("----------");

        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("格式化器用的时区是:" + dateFormat.getTimeZone().getID() + ":" + dateFormat.parse("2019-07-07 20:18:18"));
        dateFormat.setTimeZone(TimeZone.getTimeZone("Asia/Tokyo"));
        System.out.println("格式化器用的时区是:" + dateFormat.getTimeZone().getID() + ":" + dateFormat.parse("2019-07-07 20:18:18"));

        System.out.println(DateUtils.parseDate("2019-07-07 20:18:18 +0800","yyyy-MM-dd HH:mm:ss Z"));
        System.out.println(DateUtils.parseDate("2019-07-07 20:18:18.888","yyyy-MM-dd HH:mm:ss.SSS"));
        System.out.println(DateUtils.parseDate("2019-07-07 20:18:18","yyyy-MM-dd HH:mm:ss"));
        System.out.println(DateUtils.parseDate("2019-07-07 20:18","yyyy-MM-dd HH:mm"));
        System.out.println(DateUtils.parseDate("2019/07/07 20:18:18 +0800","yyyy/MM/dd HH:mm:ss Z"));
        System.out.println(DateUtils.parseDate("2019/07/07 20:18:18.888","yyyy/MM/dd HH:mm:ss.SSS"));
        System.out.println(DateUtils.parseDate("2019/07/07 20:18:18","yyyy/MM/dd HH:mm:ss"));
        System.out.println(DateUtils.parseDate("2019/07/07 20:18","yyyy/MM/dd HH:mm"));
        System.out.println(DateUtils.parseDate("20190707201818 +0800","yyyyMMddHHmmss Z"));
        System.out.println(DateUtils.parseDate("20190707201818888","yyyyMMddHHmmsSSS"));
        System.out.println(DateUtils.parseDate("20190707201818","yyyyMMddHHmmss"));
        System.out.println(DateUtils.parseDate("201907072018","yyyyMMddHHmm"));
        System.out.println("----------");

        System.out.println(DateUtils.parseDate("2019-07-32 20:18:18 +0800","yyyy-MM-dd HH:mm:ss Z"));//Thu Aug 01 20:18:18 CST 2019
        System.out.println(DateUtils.parseDateStrictly("2019-07-07 20:18:18 +0800","yyyy-MM-dd HH:mm:ss Z"));
        System.out.println("----------");

        System.out.println(Date.from(Instant.ofEpochMilli(1562501898888L)));//Sun Jul 07 20:18:18 CST 2019
        System.out.println(Date.from(Instant.ofEpochSecond(1562501898,888)));//Sun Jul 07 20:18:18 CST 2019
        System.out.println(Date.from(Instant.ofEpochSecond(1562501898)));//Sun Jul 07 20:18:18 CST 2019
        System.out.println(Date.from(Instant.parse("2019-07-07T20:18:18.000000888Z")));//Mon Jul 08 04:18:18 CST 2019
        System.out.println(Date.from(Instant.parse("2019-07-07T20:18:18Z")));//Mon Jul 08 04:18:18 CST 2019
        System.out.println(Date.from(ZONED_DATE_TIME.toInstant()));//Sun Jul 07 19:18:18 CST 2019
        System.out.println(Date.from(OFFSET_DATE_TIME.toInstant()));//Sun Jul 07 19:18:18 CST 2019
        System.out.println(Date.from(LOCAL_DATE_TIME.toInstant(ZoneOffset.UTC)));//Mon Jul 08 04:18:18 CST 2019
        System.out.println(Date.from(LOCAL_DATE_TIME.toInstant(ZoneOffset.ofHours(8))));//Sun Jul 07 20:18:18 CST 2019
    }


    public void timestampBuild() {
        System.out.println(new Timestamp(1562501898000L));//2019-07-07 20:18:18.0
        System.out.println(new Timestamp(new Date(1562501898000L).getTime()));//2019-07-07 20:18:18.0
        System.out.println("----------");

        System.out.println(Timestamp.from(Instant.ofEpochMilli(1562501898888L)));//2019-07-07 20:18:18.888
        System.out.println(Timestamp.from(Instant.ofEpochSecond(1562501898,888)));//2019-07-07 20:18:18.000000888
        System.out.println(Timestamp.from(Instant.ofEpochSecond(1562501898)));//2019-07-07 20:18:18.0
        System.out.println(Timestamp.from(Instant.parse("2019-07-07T20:18:18.000000888Z")));//2019-07-08 04:18:18.000000888
        System.out.println(Timestamp.from(Instant.parse("2019-07-07T20:18:18Z")));//2019-07-08 04:18:18.0
        System.out.println("----------");

        System.out.println(Timestamp.from(ZONED_DATE_TIME.toInstant()));//2019-07-07 19:18:18.000000888
        System.out.println(Timestamp.from(OFFSET_DATE_TIME.toInstant()));//2019-07-07 19:18:18.000000888
        System.out.println(Timestamp.from(LOCAL_DATE_TIME.toInstant(ZoneOffset.ofHours(8))));//2019-07-07 20:18:18.000000888
        System.out.println(Timestamp.from(LOCAL_DATE_TIME.toInstant(ZoneOffset.UTC)));//2019-07-08 04:18:18.000000888
        System.out.println("----------");

        System.out.println(Timestamp.valueOf(LOCAL_DATE_TIME));//2019-07-07 20:18:18.000000888
        System.out.println(ZONED_DATE_TIME);//2019-07-07T20:18:18.000000888+09:00[Asia/Tokyo]
        System.out.println(ZONED_DATE_TIME.toLocalDateTime());//2019-07-07T20:18:18.000000888
        System.out.println(Timestamp.valueOf(ZONED_DATE_TIME.toLocalDateTime()));//2019-07-07 20:18:18.000000888
    }


    public void localDateBuild() {
        System.out.println(LocalDate.now());//2022-04-03

        System.out.println(LocalDate.of(2019, 7, 7));//2019-07-07
        System.out.println(LocalDate.of(2019, Month.JULY, 7));//2019-07-07

        System.out.println(LOCAL_DATE_TIME.toLocalDate());//2019-07-07
        System.out.println(OFFSET_DATE_TIME.toLocalDate());//2019-07-07
        System.out.println(ZONED_DATE_TIME.toLocalDate());//2019-07-07
    }


    public void localTimeBuild() {
        System.out.println(LocalTime.now());//17:30:32.193733700

        System.out.println(LocalTime.of(20, 18));//20:18
        System.out.println(LocalTime.of(20, 18, 18));//20:18:18
        System.out.println(LocalTime.of(20, 18, 18, 888));//20:18:18.000000888

        System.out.println(LOCAL_DATE_TIME.toLocalTime());//20:18:18.000000888
        System.out.println(OFFSET_DATE_TIME.toLocalTime());//20:18:18.000000888
        System.out.println(ZONED_DATE_TIME.toLocalTime());//20:18:18.000000888
    }


    public void localDateTimeBuild() {
        System.out.println(LocalDateTime.now());//2022-04-03T17:30:32.193733700
        System.out.println("----------1");

        System.out.println(LocalDateTime.of(2019, 7, 7, 20, 18, 18, 888));//2019-07-07T20:18:18.000000888
        System.out.println(LocalDateTime.of(2019, 7, 7, 20, 18, 18));//2019-07-07T20:18:18
        System.out.println(LocalDateTime.of(2019, 7, 7, 20, 18));//2019-07-07T20:18
        System.out.println(LocalDateTime.of(2019, Month.JULY, 7, 20, 18, 18, 888));//2019-07-07T20:18:18.000000888
        System.out.println(LocalDateTime.of(2019, Month.JULY, 7, 20, 18, 18));//2019-07-07T20:18:18
        System.out.println(LocalDateTime.of(2019, Month.JULY, 7, 20, 18));//2019-07-07T20:18
        System.out.println(LocalDateTime.of(LocalDate.of(2019, 7, 7), LocalTime.of(20, 18,18,888)));//2019-07-07T20:18:18.000000888
        System.out.println(LocalDateTime.of(LocalDate.of(2019, 7, 7), LocalTime.of(20, 18,18)));//2019-07-07T20:18:18
        System.out.println(LocalDateTime.of(LocalDate.of(2019, 7, 7), LocalTime.of(20, 18)));//2019-07-07T20:18
        System.out.println("----------2");

        System.out.println(LocalDateTime.ofEpochSecond(1562501898,888888888, ZoneOffset.UTC));//2019-07-07T12:18:18.888888888
        System.out.println(LocalDateTime.ofEpochSecond(1562501898,888888888, ZoneOffset.ofHours(8)));//2019-07-07T20:18:18.888888888
        System.out.println(LocalDateTime.ofInstant(Instant.parse("2019-07-07T20:18:18.000000888Z"), ZoneId.of("UTC")));//2019-07-07T20:18:18.000000888
        System.out.println(LocalDateTime.ofInstant(Instant.parse("2019-07-07T20:18:18.000000888Z"), ZoneId.of("+08:00")));//2019-07-08T04:18:18.000000888

        System.out.println(DATE.toInstant().atZone(ZoneId.of("+09:00")).toLocalDateTime());//2019-07-07T21:18:18
        System.out.println(OFFSET_DATE_TIME.toLocalDateTime());//2019-07-07T20:18:18.000000888
        System.out.println(ZONED_DATE_TIME.toLocalDateTime());//2019-07-07T20:18:18.000000888
        System.out.println("----------3");

        System.out.println(LocalDateTime.parse("2022-04-03T18:00"));//2022-04-03T18:00

        DateTimeFormatter df = DateTimeFormatter.ofPattern("第Q季度 yyyy-MM-dd HH:mm:ss", Locale.JAPAN);
        System.out.println(LocalDateTime.parse("第3季度 2019-07-07 20:18:18",df));//2019-07-07T20:18:18
        DateTimeFormatter df1 = DateTimeFormatter.ofPattern("第Q季度 yyyy-MM-dd HH:mm:ss", Locale.CHINA);
        System.out.println(LocalDateTime.parse("第3季度 2019-07-07 20:18:18",df1));//2019-07-07T20:18:18

        System.out.println(LocalDateTime.parse("2019-07-07T20:18:18.000000888"));//2019-07-07T20:18:18.000000888
        System.out.println(LocalDateTime.parse("2019-07-07T20:18:18"));//2019-07-07T20:18:18
        System.out.println(LocalDateTime.parse("2019-07-07T20:18"));//2019-07-07T20:18
    }


    public void offsetDateTimeBuild() {
        System.out.println(OffsetDateTime.now());// 使用系统时区 2022-04-03T17:30:32.209357700+08:00
        System.out.println(OffsetDateTime.now(ZoneId.of("Asia/Tokyo")));//自己指定时区 2022-04-03T18:30:32.209357700+09:00
        System.out.println(OffsetDateTime.now(Clock.systemUTC()));//自己指定时区 2022-04-03T09:30:32.209357700Z
        System.out.println("----------1");

        System.out.println(OffsetDateTime.of(2019, 7, 7, 20, 18, 18, 888, ZoneOffset.ofHours(9)));//2019-07-07T20:18:18.000000888+09:00
        System.out.println(OffsetDateTime.of(LocalDate.of(2019, 7, 7),LocalTime.of(20, 18, 18, 888),ZoneOffset.ofHours(9)));//2019-07-07T20:18:18.000000888+09:00
        System.out.println(OffsetDateTime.of(LOCAL_DATE_TIME,ZoneOffset.ofHours(9)));//2019-07-07T20:18:18.000000888+09:00
        System.out.println(OffsetDateTime.ofInstant(LOCAL_DATE_TIME.toInstant(ZoneOffset.ofHours(9)), ZoneOffset.ofHours(9)));//2019-07-07T20:18:18.000000888+09:00
        System.out.println("----------2");

        System.out.println(LOCAL_DATE_TIME.atOffset(ZoneOffset.ofHours(9)));//2019-07-07T20:18:18.000000888+09:00
        System.out.println(ZONED_DATE_TIME.toOffsetDateTime());//2019-07-07T20:18:18.000000888+09:00

        System.out.println(OffsetDateTime.parse("2019-07-07T20:18:18.000000888+09:00"));//2019-07-07T20:18:18.000000888+09:00
        System.out.println(OffsetDateTime.parse("2019-07-07T20:18:18+09:00"));//2019-07-07T20:18:18+09:00
        System.out.println(OffsetDateTime.parse("2019-07-07T20:18+09:00"));//2019-07-07T20:18+09:00
    }


    public void zonedDateTimeBuild() {
        System.out.println(ZonedDateTime.now());//使用系统时区 2022-04-03T17:30:32.193733700+08:00[Asia/Shanghai]
        System.out.println(ZonedDateTime.now(ZoneId.of("Asia/Tokyo")));//自己指定时区 2022-04-03T18:30:32.193733700+09:00[Asia/Tokyo]
        System.out.println(ZonedDateTime.now(Clock.systemUTC()));//自己指定时区 2022-04-03T09:30:32.193733700Z
        System.out.println("----------2");

        System.out.println(ZonedDateTime.of(2019, 7, 7, 20, 18, 18, 888, ZoneId.of("Asia/Tokyo")));//2019-07-07T20:18:18.000000888+09:00[Asia/Tokyo]
        System.out.println(ZonedDateTime.of(LocalDate.of(2019, 7, 7),LocalTime.of(20, 18, 18, 888),ZoneId.of("Asia/Tokyo")));//2019-07-07T20:18:18.000000888+09:00[Asia/Tokyo]
        System.out.println(ZonedDateTime.of(LOCAL_DATE_TIME, ZoneId.of("Asia/Tokyo")));//2019-07-07T20:18:18.000000888+09:00[Asia/Tokyo]
        System.out.println("--------3");

        System.out.println(ZonedDateTime.ofInstant(LOCAL_DATE_TIME.toInstant(ZoneOffset.ofHours(9)), ZoneOffset.ofHours(9)));//2019-07-07T20:18:18.000000888+09:00
        System.out.println(ZonedDateTime.ofInstant(LOCAL_DATE_TIME.toInstant(ZoneOffset.ofHours(9)), ZoneId.of("Asia/Tokyo")));//2019-07-07T20:18:18.000000888+09:00[Asia/Tokyo]
        System.out.println(ZonedDateTime.ofInstant(LOCAL_DATE_TIME.toInstant(ZoneOffset.ofHours(9)), ZoneId.of("Asia/Shanghai")));//2019-07-07T19:18:18.000000888+08:00[Asia/Shanghai]
        System.out.println(ZonedDateTime.ofInstant(LOCAL_DATE_TIME, ZoneOffset.ofHours(9), ZoneOffset.ofHours(9)));//2019-07-07T20:18:18.000000888+09:00
        System.out.println(ZonedDateTime.ofInstant(LOCAL_DATE_TIME, ZoneOffset.UTC, ZoneOffset.ofHours(9)));//2019-07-08T05:18:18.000000888+09:00
        System.out.println("--------4");

        System.out.println(OFFSET_DATE_TIME.toZonedDateTime());//2019-07-07T20:18:18.000000888+09:00
        System.out.println(OFFSET_DATE_TIME.atZoneSameInstant(ZoneId.of("Asia/Tokyo")));//2019-07-07T20:18:18.000000888+09:00[Asia/Tokyo]
        System.out.println(OFFSET_DATE_TIME.atZoneSameInstant(ZoneId.of("Asia/Shanghai")));//以确保结果具有相同的Instant 2019-07-07T19:18:18.000000888+08:00[Asia/Shanghai]
        System.out.println(OFFSET_DATE_TIME.atZoneSimilarLocal(ZoneId.of("Asia/Tokyo")));//2019-07-07T20:18:18.000000888+09:00[Asia/Tokyo]
        System.out.println(OFFSET_DATE_TIME.atZoneSimilarLocal(ZoneId.of("Asia/Shanghai")));//以确保结果具有相同的本地时间 2019-07-07T20:18:18.000000888+08:00[Asia/Shanghai]
        System.out.println("--------5");

        System.out.println(LOCAL_DATE_TIME.atZone(ZoneId.of("Asia/Tokyo")));//2019-07-07T20:18:18.000000888+09:00[Asia/Tokyo]
        System.out.println(DATE.toInstant().atZone(ZonedDateTime.now().getZone()));//2019-07-07T20:18:18+08:00[Asia/Shanghai]
        System.out.println(DATE.toInstant().atZone(ZonedDateTime.now().getOffset()));//2019-07-07T20:18:18+08:00
        System.out.println("----------6");

        System.out.println(ZonedDateTime.parse("2019-07-07T20:18:18.000000888+09:00[Asia/Tokyo]"));//2019-07-07T20:18:18.000000888+09:00[Asia/Tokyo]
        System.out.println(ZonedDateTime.parse("2019-07-07T20:18:18+09:00[Asia/Tokyo]"));//2019-07-07T20:18:18+09:00[Asia/Tokyo]
        System.out.println(ZonedDateTime.parse("2019-07-07T20:18+09:00[Asia/Tokyo]"));//2019-07-07T20:18+09:00[Asia/Tokyo]
    }

把日期转换为String格式

    public static final LocalDateTime LOCAL_DATE_TIME = LocalDateTime.of(2019, 7, 7, 20, 18, 18, 888);
    public static final ZonedDateTime ZONED_DATE_TIME = ZonedDateTime.of(2019, 7, 7, 20, 18, 18, 888, ZoneId.of("Asia/Tokyo"));
    public static final OffsetDateTime OFFSET_DATE_TIME = OffsetDateTime.of(2019, 7, 7, 20, 18, 18, 888, ZoneOffset.ofHours(9));
    protected static final Date DATE = new Date(1562501898888L);

 public void timestampToString() {
        Timestamp timestamp = new Timestamp(156250189888L);
        System.out.println(timestamp.toString());

        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(df.format(timestamp));
    }

 public void dateToString() {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("这是北京时间:" + df.format(DATE));
        df.setTimeZone(TimeZone.getTimeZone("Asia/Tokyo"));
        System.out.println("这是东京时间:" + df.format(DATE));
    }


    public void ISO_DATE_TIME() {
        Assert.assertEquals("2019-07-07T20:18:18.000000888+09:00[Asia/Tokyo]", ZONED_DATE_TIME.format(DateTimeFormatter.ISO_DATE_TIME));
        Assert.assertEquals("2019-07-07T20:18:18.000000888+09:00", OFFSET_DATE_TIME.format(DateTimeFormatter.ISO_DATE_TIME));
        Assert.assertEquals("2019-07-07T20:18:18.000000888", LOCAL_DATE_TIME.format(DateTimeFormatter.ISO_DATE_TIME));
    }


    public void ISO_DATE() {
        Assert.assertEquals("2019-07-07+09:00", ZONED_DATE_TIME.format(DateTimeFormatter.ISO_DATE));
        Assert.assertEquals("2019-07-07+09:00", OFFSET_DATE_TIME.format(DateTimeFormatter.ISO_DATE));
        Assert.assertEquals("2019-07-07", LOCAL_DATE_TIME.format(DateTimeFormatter.ISO_DATE));
    }


    public void ISO_TIME() {
        Assert.assertEquals("20:18:18.000000888+09:00", ZONED_DATE_TIME.format(DateTimeFormatter.ISO_TIME));
        Assert.assertEquals("20:18:18.000000888+09:00", OFFSET_DATE_TIME.format(DateTimeFormatter.ISO_TIME));
        Assert.assertEquals("20:18:18.000000888", LOCAL_DATE_TIME.format(DateTimeFormatter.ISO_TIME));
    }


    public void ISO_LOCAL_DATE_TIME() {
        Assert.assertEquals("2019-07-07T20:18:18.000000888", DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(ZONED_DATE_TIME));
        Assert.assertEquals("2019-07-07T20:18:18.000000888", DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(OFFSET_DATE_TIME));
        Assert.assertEquals("2019-07-07T20:18:18.000000888", DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(LOCAL_DATE_TIME));
    }


    public void ISO_LOCAL_DATE() {
        Assert.assertEquals("2019-07-07", DateTimeFormatter.ISO_LOCAL_DATE.format(ZONED_DATE_TIME));
        Assert.assertEquals("2019-07-07", DateTimeFormatter.ISO_LOCAL_DATE.format(OFFSET_DATE_TIME));
        Assert.assertEquals("2019-07-07", DateTimeFormatter.ISO_LOCAL_DATE.format(LOCAL_DATE_TIME));
    }


    public void ISO_LOCAL_TIME() {
        Assert.assertEquals("20:18:18.000000888", DateTimeFormatter.ISO_LOCAL_TIME.format(ZONED_DATE_TIME));
        Assert.assertEquals("20:18:18.000000888", DateTimeFormatter.ISO_LOCAL_TIME.format(OFFSET_DATE_TIME));
        Assert.assertEquals("20:18:18.000000888", DateTimeFormatter.ISO_LOCAL_TIME.format(LOCAL_DATE_TIME));
    }


    public void ISO_OFFSET_DATE_TIME() {
        Assert.assertEquals("2019-07-07T20:18:18.000000888+09:00",DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(ZONED_DATE_TIME));
        Assert.assertEquals("2019-07-07T20:18:18.000000888+09:00",DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(OFFSET_DATE_TIME));
        //DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(LOCAL_DATE_TIME);//java.time.temporal.UnsupportedTemporalTypeException: Unsupported field:
    }


    public void ISO_OFFSET_DATE() {
        Assert.assertEquals("2019-07-07+09:00",DateTimeFormatter.ISO_OFFSET_DATE.format(ZONED_DATE_TIME));
        Assert.assertEquals("2019-07-07+09:00",DateTimeFormatter.ISO_OFFSET_DATE.format(OFFSET_DATE_TIME));
        //DateTimeFormatter.ISO_OFFSET_DATE.format(LOCAL_DATE_TIME);//java.time.temporal.UnsupportedTemporalTypeException: Unsupported field:
    }


    public void ISO_OFFSET_TIME() {
        Assert.assertEquals("20:18:18.000000888+09:00",DateTimeFormatter.ISO_OFFSET_TIME.format(ZONED_DATE_TIME));
        Assert.assertEquals("20:18:18.000000888+09:00",DateTimeFormatter.ISO_OFFSET_TIME.format(OFFSET_DATE_TIME));
        //DateTimeFormatter.ISO_OFFSET_TIME.format(LOCAL_DATE_TIME);//java.time.temporal.UnsupportedTemporalTypeException: Unsupported field:
    }


    public void ISO_ZONED_DATE_TIME() {
        Assert.assertEquals("2019-07-07T20:18:18.000000888+09:00[Asia/Tokyo]", DateTimeFormatter.ISO_ZONED_DATE_TIME.format(ZONED_DATE_TIME));
        Assert.assertEquals("2019-07-07T20:18:18.000000888+09:00", DateTimeFormatter.ISO_ZONED_DATE_TIME.format(OFFSET_DATE_TIME));
        //DateTimeFormatter.ISO_ZONED_DATE_TIME.format(LOCAL_DATE_TIME);//java.time.temporal.UnsupportedTemporalTypeException: Unsupported field:

        //DateTimeFormatter.ISO_ZONED_DATE.format(ZonedDateTime.now());// 不存在ISO_ZONED_DATE
        //DateTimeFormatter.ISO_ZONED_TIME.format(ZonedDateTime.now());// 不存在ISO_ZONED_TIME
    }


    public void ISO_INSTANT() {
        Assert.assertEquals("2019-07-07T11:18:18.000000888Z", ZONED_DATE_TIME.format(DateTimeFormatter.ISO_INSTANT));
        Assert.assertEquals("2019-07-07T11:18:18.000000888Z", OFFSET_DATE_TIME.format(DateTimeFormatter.ISO_INSTANT));
        //LOCAL_DATE_TIME.format(DateTimeFormatter.ISO_INSTANT);//java.time.temporal.UnsupportedTemporalTypeException: Unsupported field:
    }


    public void ISO_ORDINAL_DATE() {
        Assert.assertEquals("2019-188+09:00", ZONED_DATE_TIME.format(DateTimeFormatter.ISO_ORDINAL_DATE));
        Assert.assertEquals("2019-188+09:00", OFFSET_DATE_TIME.format(DateTimeFormatter.ISO_ORDINAL_DATE));
        Assert.assertEquals("2019-188", LOCAL_DATE_TIME.format(DateTimeFormatter.ISO_ORDINAL_DATE));
    }


    public void ISO_WEEK_DATE() {
        Assert.assertEquals("2019-W27-7+09:00",ZONED_DATE_TIME.format(DateTimeFormatter.ISO_WEEK_DATE));
        Assert.assertEquals("2019-W27-7+09:00",OFFSET_DATE_TIME.format(DateTimeFormatter.ISO_WEEK_DATE));
        Assert.assertEquals("2019-W27-7",LOCAL_DATE_TIME.format(DateTimeFormatter.ISO_WEEK_DATE));
    }


    public void ofLocalizedDateTime() {
        Assert.assertEquals("2019年7月7日星期日 日本标准时间 下午8:18:18",DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL).withZone(ZoneId.of("Asia/Tokyo")).format(LOCAL_DATE_TIME));
        Assert.assertEquals("2019年7月7日 JST 下午8:18:18",DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG).withZone(ZoneId.of("Asia/Tokyo")).format(LOCAL_DATE_TIME));
        Assert.assertEquals("2019年7月7日 下午8:18:18",DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM).withZone(ZoneId.of("Asia/Tokyo")).format(LOCAL_DATE_TIME));
        Assert.assertEquals("2019/7/7 下午8:18",DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT).withZone(ZoneId.of("Asia/Tokyo")).format(LOCAL_DATE_TIME));
        Assert.assertEquals("2019年7月7日星期日",DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL).format(LOCAL_DATE_TIME));
        Assert.assertEquals("2019年7月7日",DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG).format(LOCAL_DATE_TIME));
        Assert.assertEquals("2019年7月7日",DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM).format(LOCAL_DATE_TIME));
        Assert.assertEquals("2019/7/7",DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT).format(LOCAL_DATE_TIME));

        Assert.assertEquals("2019年7月7日星期日 日本标准时间 下午8:18:18",DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL).withZone(ZoneId.of("Asia/Tokyo")).format(ZONED_DATE_TIME));
        Assert.assertEquals("2019年7月7日 JST 下午8:18:18",DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG).withZone(ZoneId.of("Asia/Tokyo")).format(ZONED_DATE_TIME));
        Assert.assertEquals("2019年7月7日 下午8:18:18",DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM).withZone(ZoneId.of("Asia/Tokyo")).format(ZONED_DATE_TIME));
        Assert.assertEquals("2019/7/7 下午8:18",DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT).withZone(ZoneId.of("Asia/Tokyo")).format(ZONED_DATE_TIME));
        Assert.assertEquals("2019年7月7日星期日",DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL).format(ZONED_DATE_TIME));
        Assert.assertEquals("2019年7月7日",DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG).format(ZONED_DATE_TIME));
        Assert.assertEquals("2019年7月7日",DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM).format(ZONED_DATE_TIME));
        Assert.assertEquals("2019/7/7",DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT).format(ZONED_DATE_TIME));

        Assert.assertEquals("2019年7月7日星期日 日本标准时间 下午8:18:18",DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL).withZone(ZoneId.of("Asia/Tokyo")).format(OFFSET_DATE_TIME));
        Assert.assertEquals("2019年7月7日 JST 下午8:18:18",DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG).withZone(ZoneId.of("Asia/Tokyo")).format(OFFSET_DATE_TIME));
        Assert.assertEquals("2019年7月7日 下午8:18:18",DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM).withZone(ZoneId.of("Asia/Tokyo")).format(OFFSET_DATE_TIME));
        Assert.assertEquals("2019/7/7 下午8:18",DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT).withZone(ZoneId.of("Asia/Tokyo")).format(OFFSET_DATE_TIME));
        Assert.assertEquals("2019年7月7日星期日",DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL).format(OFFSET_DATE_TIME));
        Assert.assertEquals("2019年7月7日",DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG).format(OFFSET_DATE_TIME));
        Assert.assertEquals("2019年7月7日",DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM).format(OFFSET_DATE_TIME));
        Assert.assertEquals("2019/7/7",DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT).format(OFFSET_DATE_TIME));
    }


    public void dateToCustomString() {
        LocalDateTime localDateTimeExcept = LocalDateTime.of(2019, 7, 7, 20, 18, 18);

        DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("第Q季度 yyyy-MM-dd HH:mm:ss", Locale.CHINA);
        Assert.assertEquals("第3季度 2019-07-07 20:18:18",formatter1.format(LOCAL_DATE_TIME));
        Assert.assertEquals("第3季度 2019-07-07 20:18:18",formatter1.format(ZONED_DATE_TIME));
        Assert.assertEquals("第3季度 2019-07-07 20:18:18",formatter1.format(OFFSET_DATE_TIME));

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("第Q季度 yyyy-MM-dd HH:mm:ss", Locale.JAPAN);
        Assert.assertEquals("第3季度 2019-07-07 20:18:18",formatter.format(LOCAL_DATE_TIME));
        Assert.assertEquals("第3季度 2019-07-07 20:18:18",formatter.format(ZONED_DATE_TIME));
        Assert.assertEquals("第3季度 2019-07-07 20:18:18",formatter.format(OFFSET_DATE_TIME));


        Assert.assertEquals(localDateTimeExcept,LocalDateTime.parse("第3季度 2019-07-07 20:18:18", formatter));
        Assert.assertEquals(localDateTimeExcept,LocalDateTime.parse("第3季度 2019-07-07 20:18:18", formatter1));
        //Assert.assertEquals(localDateTime1,ZonedDateTime.parse("第3季度 2019-07-07 20:18:18", formatter));java.time.format.DateTimeParseException
        //Assert.assertEquals(localDateTime1,ZonedDateTime.parse("第3季度 2019-07-07 20:18:18", formatter1));java.time.format.DateTimeParseException
        //Assert.assertEquals(localDateTime1,OffsetDateTime.parse("第3季度 2019-07-07 20:18:18", formatter));java.time.format.DateTimeParseException
        //Assert.assertEquals(localDateTime1,OffsetDateTime.parse("第3季度 2019-07-07 20:18:18", formatter1));java.time.format.DateTimeParseException
    }

常用应用场景

把当前时区日期转换到零时区日期

方式1

Date date = new Date();

LocalDateTime localDateTime = date.toInstant().atZone(ZoneId.of("UTC")).toLocalDateTime();
Timestamp timestamp = Timestamp.valueOf(localDateTime);
new Date(timestamp.getTime());

方式2

Date date = new Date();

Date.from(localDateTime.toInstant(ZonedDateTime.now().getOffset()));

把零时区日期转换到当前时区日期

方式1

Date date = new Date();

LocalDateTime localDateTime = date.toInstant().atZone(ZonedDateTime.now().getZone()).toLocalDateTime();
ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(localDateTime.toInstant(ZoneOffset.UTC), ZoneId.systemDefault());
// 转为Date
Date.from(zonedDateTime.toLocalDateTime().toInstant(ZoneOffset.ofHours(8)))
// 转为String(具体格式可以参考如上toString)
DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(zonedDateTime)

方式2

Date date = new Date();

LocalDateTime localDateTime2 = date.toInstant().atOffset(ZonedDateTime.now().getOffset()).toLocalDateTime();
OffsetDateTime offsetDateTime = OffsetDateTime.ofInstant(localDateTime.toInstant(ZoneOffset.UTC), ZoneId.systemDefault());
// 转为Date
Date.from(offsetDateTime.toLocalDateTime().toInstant(ZoneOffset.ofHours(8)))
// 转为String(具体格式可以参考如上toString)
DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(offsetDateTime)
  Java知识库 最新文章
计算距离春节还有多长时间
系统开发系列 之WebService(spring框架+ma
springBoot+Cache(自定义有效时间配置)
SpringBoot整合mybatis实现增删改查、分页查
spring教程
SpringBoot+Vue实现美食交流网站的设计与实
虚拟机内存结构以及虚拟机中销毁和新建对象
SpringMVC---原理
小李同学: Java如何按多个字段分组
打印票据--java
上一篇文章      下一篇文章      查看所有文章
加:2022-04-28 11:39:48  更:2022-04-28 11:41:21 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/24 3:11:24-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码