Java在线运行

版本:

所属目录
点击了解高性能代码运行API
运行结果
教程手册
代码仓库
极速运行
终端运行
图形+终端

                        
以下是用户最新保存的代码
# 测试scanner代码的 发布于:2024-07-26 10:13 去除文件名的后缀<br/> .doc<br/> .docx<br/> .xls<br/> 等 发布于:2024-07-24 14:52 去除字符串中的空格 发布于:2024-07-24 11:38 项目使用测试 发布于:2024-07-23 17:31 Java字符串分割循环 发布于:2024-07-20 17:30 关于商品的链表实现 发布于:2024-07-19 15:19 des加密 发布于:2024-07-17 10:16 学生管理系统 发布于:2024-07-16 15:21 for循环嵌套/打印一个菱形 发布于:2024-07-10 21:20 成绩计算代码 发布于:2024-07-04 20:43 计算到当前时间的天数 发布于:2024-07-03 10:57 java代码演示 发布于:2024-07-02 12:05 小雨买文具 发布于:2024-07-01 21:25 大象喝水c 发布于:2024-07-01 08:52 暂存下csv导出 发布于:2024-06-30 20:18 黑红树,未完成 发布于:2024-06-28 20:49 hihi胡 发布于:2024-06-25 13:39 软件测试用例 发布于:2024-06-23 22:19 张明桐 225060130 发布于:2024-06-23 14:33 多态的介绍 发布于:2024-06-23 13:35 方法重写介绍 发布于:2024-06-23 11:26 --- java窗口 发布于:2024-06-21 16:24 FinallShell激活 发布于:2024-06-21 14:24 分别用三种不同的算法时间复杂度找出不超过n的所有素数,编程实现 这三种不同算法,并对比当n为10万时,三种算法所需要的运行时间 发布于:2024-06-21 08:19 用分支限界法求解0-1背包问题 发布于:2024-06-20 21:27 三角形字符 发布于:2024-06-20 21:30 三角形面积 发布于:2024-06-27 18:25 打印菱形放在一个正方形里面 发布于:2024-06-18 19:53 RSA加密 发布于:2024-06-17 11:59 super()使用细节 发布于:2024-06-14 20:31 继承本质例题 发布于:2024-06-14 19:06 用回溯算法求解 n 皇后问题 发布于:2024-06-13 23:47 用回溯算法求解0-1问题 发布于:2024-06-13 23:45 用回溯算法求解装载问题 发布于:2024-06-13 23:39 用贪心算法实现单源最短路径 发布于:2024-06-13 23:36 用贪心算法构造 Huffman 编码。 发布于:2024-06-13 23:34 1111请问大萨达 发布于:2024-06-21 15:58 用 Prime 贪心算法构造最小生成树 发布于:2024-06-13 11:36 用 Kruskal 贪心算法构造最小生成树 发布于:2024-06-13 11:35 用分治法实现选择第 k 小元素 发布于:2024-06-13 11:27 分治法实现二分查找 发布于:2024-06-13 11:24 求最重和最轻的金块 发布于:2024-06-13 11:22 求公鸡母鸡小鸡 发布于:2024-06-13 11:11 用分治法实现快速排序 发布于:2024-06-13 11:25 背包价值计算 发布于:2024-06-13 11:06 计算下一个日期 发布于:2024-06-08 16:06 11.5(课程类) 发布于:2024-06-07 19:05 11.2(P、S、E、F和S类) 发布于:2024-06-07 18:27 11.1(三角形类) 发布于:2024-06-07 18:25 10.5(显示素数因子) 发布于:2024-06-07 18:02 [更多]
显示目录

Java 8 – Convert Date to LocalDate and LocalDateTime



学习嵌入式的绝佳套件,esp8266开源小电视成品,比自己去买开发板+屏幕还要便宜,省去了焊接不当搞坏的风险。 蜂鸣版+触控升级仅36元,更强的硬件、价格全网最低。

点击购买 固件广场

The java.util.Date has no concept of time zone, and only represents the number of seconds passed since the Unix epoch time – 1970-01-01T00:00:00Z (midnight at the start of January 1, 1970 GMT/UTC)

Note
The new Java 8 java.time.Instant is the equivalent class to the classic java.util.Date

1. Date -> java.time

The idea for the date conversion :

Date -> Instant + System default time zone = LocalDate
Date -> Instant + System default time zone = LocalDateTime
Date -> Instant + System default time zone = ZonedDateTime

This example shows you how to convert java.util.Date to the new Java 8 Date APIs – LocalDate, LocalDateTime and ZonedDateTime

DateToJavaTime.java

package com.mkyong.java8;

import java.time.*;
import java.util.Date;

public class DateToJavaTime {

    public static void main(String[] args) {

        //Asia/Kuala_Lumpur +8
        ZoneId defaultZoneId = ZoneId.systemDefault();
        System.out.println("System Default TimeZone : " + defaultZoneId);

        //toString() append +8 automatically.
        Date date = new Date();
        System.out.println("date : " + date);

        //1. Convert Date -> Instant
        Instant instant = date.toInstant();
        System.out.println("instant : " + instant); //Zone : UTC+0

        //2. Instant + system default time zone + toLocalDate() = LocalDate
        LocalDate localDate = instant.atZone(defaultZoneId).toLocalDate();
        System.out.println("localDate : " + localDate);

        //3. Instant + system default time zone + toLocalDateTime() = LocalDateTime
        LocalDateTime localDateTime = instant.atZone(defaultZoneId).toLocalDateTime();
        System.out.println("localDateTime : " + localDateTime);

        //4. Instant + system default time zone = ZonedDateTime
        ZonedDateTime zonedDateTime = instant.atZone(defaultZoneId);
        System.out.println("zonedDateTime : " + zonedDateTime);

    }

}

Output

System Default TimeZone : Asia/Kuala_Lumpur

date : Fri Aug 19 21:46:31 MYT 2016
instant : 2016-08-19T13:46:31.981Z

localDate : 2016-08-19
localDateTime : 2016-08-19T21:46:31.981
zonedDateTime : 2016-08-19T21:46:31.981+08:00[Asia/Kuala_Lumpur]

2. Explanation – Q&A

2.1 Question : If Date has no concept of time zone, why the time zone will be displayed while we print out the Date object? For example :

//Fri Aug 19 11:52:06 MYT 2016
System.out.println(new Date()); //MYT = my system default time zone

Answer : Check the java.uti.Date.toString() source code, if you print out the Date object, the system default time zone will be appended and display together.

java.util.Date

public String toString() {

        //...omitted...

        TimeZone zi = date.getZone();
        if (zi != null) {
            sb.append(zi.getDisplayName(date.isDaylightTime(), TimeZone.SHORT, Locale.US)); // zzz
        } else {
            sb.append("GMT");
        }
        sb.append(' ').append(date.getYear());  // yyyy
        return sb.toString();
}

Note
This behavior is a design flaw since JDK1.1, it makes a lot of confusion. Again, the java.util.Date doesn’t store any time zone info, but if you print it out, the system default time zone will be displayed together.

2.2 Question : For the Date conversion, why we need to add a system default time zone for java.time.instant?
Answer : Refer to the above 2.1 Q&A. Review another example :

1. Date = 19/08/2016T10:00:00
2. System default time zone = +08:00 [Asia/Kuala_Lumpur]
3. Date (Print) = 19/08/2016T10:00:00+08:00 = 19/08/2016T18:00:00

The goal of the conversion is make sure both print Date and print LocalDate will generates the same output.

// Assume 19/08/2016T10:00:00 = 1000
// System default time zone = +8

1. Date (1000) -> Print Date (1000) = 1000+08:00  
// we always see "1000+08:00" (but the Date is still 1000)

2. Date (1000) -> Instant (1000)
// instant has no time zone or zero offset (UTC+0/Z)

3. Instant(1000) -> LocalDate(1000) -> Print LocalDate(1000) = 1000
// The result is "1000", different with print date!

4. LocalDate(1000) + 08:00 -> LocalDate(1000+08:00)
// add default time zone +8

5. Print LocalDate(1000+08:00) = 1000+08:00

3. java.time -> Date

This example shows you how to convert LocalDate, LocalDateTime and ZonedDateTime back to the classic java.util.Date

JavaTimeToDate.java

package com.mkyong.java8;

import java.time.*;
import java.util.Date;

public class JavaTimeToDate {

    public static void main(String[] args) {

        //Asia/Kuala_Lumpur +8
        ZoneId defaultZoneId = ZoneId.systemDefault();
        System.out.println("System Default TimeZone : " + defaultZoneId);

        LocalDate localDate = LocalDate.of(2016, 8, 19);
        Date date = Date.from(localDate.atStartOfDay(defaultZoneId).toInstant());
        System.out.println("\n1. LocalDate -> Date");
        System.out.println("localDate : " + localDate);
        System.out.println("date : " + date);

        LocalDateTime localDateTime = LocalDateTime.of(2016,8,19,21,46,31);
        Date date2 = Date.from(localDateTime.atZone(defaultZoneId).toInstant());
        System.out.println("\n2. LocalDateTime -> Date");
        System.out.println("localDateTime : " + localDateTime);
        System.out.println("date2 : " + date2);

        ZonedDateTime zonedDateTime = localDateTime.atZone(defaultZoneId);
        Date date3 = Date.from(zonedDateTime.toInstant());
        System.out.println("\n3. ZonedDateTime -> Date");
        System.out.println("zonedDateTime : " + zonedDateTime);
        System.out.println("date3 : " + date3);

    }

}

Output

System Default TimeZone : Asia/Kuala_Lumpur

1. LocalDate -> Date
localDate : 2016-08-19
date : Fri Aug 19 00:00:00 MYT 2016

2. LocalDateTime -> Date
localDateTime : 2016-08-19T21:46:31
date2 : Fri Aug 19 21:46:31 MYT 2016

3. ZonedDateTime -> Date
zonedDateTime : 2016-08-19T21:46:31+08:00[Asia/Kuala_Lumpur]
date3 : Fri Aug 19 21:46:31 MYT 2016

References

  1. JSR 310: Date and Time API
  2. Unix time
  3. Instant JavaDoc
  4. Date JavaDoc
  5. Java – Convert date and time between timezone
由JSRUN为你提供的Java在线运行、在线编译工具
        JSRUN提供的Java 在线运行,Java 在线运行工具,基于linux操作系统环境提供线上编译和线上运行,具有运行快速,运行结果与常用开发、生产环境保持一致的特点。
yout