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 – ZonedDateTime examples



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

点击购买 固件广场

paris-time-zone paris-time-zone

Few java.time.ZonedDateTime examples to show you how to convert a time zone between different countries.

1. Malaysia (KUL) -> Japan (HND)

Review a flight information from Malaysia Kuala Lumpur (UTC+08:00) to Japan Tokyo Haneda (UTC+09:00)

---Flight Detail---
Kuala Lumpur (KUL) -> Tokyo Haneda (HND)
Flight Duration : 7 hours

(KUL-Depart) 1430, 22 Aug 2016 ->  2230, 22 Aug 2016 (HND-Arrive)

P.S Japan Tokyo is one hour faster than Malaysia Kuala lumpur

DifferentTimeZoneExample1.java

package com.mkyong.timezone;

import java.time.LocalDateTime;
import java.time.Month;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

public class DifferentTimeZoneExample1 {

    public static void main(String[] args) {

        DateTimeFormatter format = DateTimeFormatter.ofPattern("HHmm, dd MMM yyyy");

        LocalDateTime ldt = LocalDateTime.of(2016, Month.AUGUST, 22, 14, 30);
        System.out.println("LocalDateTime : " + format.format(ldt));

        //UTC+8
        ZonedDateTime klDateTime = ldt.atZone(ZoneId.of("Asia/Kuala_Lumpur"));
        System.out.println("Depart : " + format.format(klDateTime));

        //UTC+9 and flight duration = 7 hours
        ZonedDateTime japanDateTime = klDateTime.withZoneSameInstant(ZoneId.of("Asia/Tokyo")).plusHours(7);
        System.out.println("Arrive : " + format.format(japanDateTime));

        System.out.println("\n---Detail---");
        System.out.println("Depart : " + klDateTime);
        System.out.println("Arrive : " + japanDateTime);

    }

}

Output

LocalDateTime : 1430, 22 Aug 2016
Depart : 1430, 22 Aug 2016
Arrive : 2230, 22 Aug 2016

---Detail---
Depart : 2016-08-22T14:30+08:00[Asia/Kuala_Lumpur]
Arrive : 2016-08-22T22:30+09:00[Asia/Tokyo]

2. France, Paris -> -05:00

Another time zone example from France, Paris (UTC+02:00, DST) to a hard coded (UTC-05:00) time zone (e.g New York)

---Flight Detail---
France, Paris -> UTC-05:00
Flight Duration : 8 hours 10 minutes

(Depart) 1430, 22 Aug 2016 ->  1540, 22 Aug 2016 (Arrive)

DifferentTimeZoneExample2.java

package com.mkyong.timezone;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

public class DifferentTimeZoneExample2 {

    public static void main(String[] args) {

        DateTimeFormatter format = DateTimeFormatter.ofPattern("HHmm, dd MMM yyyy");

        //Convert String to LocalDateTime
        String date = "2016-08-22 14:30";
        LocalDateTime ldt = LocalDateTime.parse(date, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"));
        System.out.println("LocalDateTime : " + format.format(ldt));

        //Paris, 2016 Apr-Oct = DST, UTC+2, other months UTC+1
        //UTC+2
        ZonedDateTime parisDateTime = ldt.atZone(ZoneId.of("Europe/Paris"));
        System.out.println("Depart : " + format.format(parisDateTime));

        //hard code a zoneoffset like this, UTC-5
        ZoneOffset nyOffSet = ZoneOffset.of("-05:00");
        ZonedDateTime nyDateTime = parisDateTime.withZoneSameInstant(nyOffSet).plusHours(8).plusMinutes(10);
        System.out.println("Arrive : " + format.format(nyDateTime));

        System.out.println("\n---Detail---");
        System.out.println("Depart : " + parisDateTime);
        System.out.println("Arrive : " + nyDateTime);

    }

}

Output

LocalDateTime : 1430, 22 Aug 2016
Depart : 1430, 22 Aug 2016
Arrive : 1540, 22 Aug 2016

---Detail---
Depart : 2016-08-22T14:30+02:00[Europe/Paris]
Arrive : 2016-08-22T15:40-05:00

Daylight Saving Time (DST)
Paris, normally UTC+1 has DST (add one hour = UTC+2) from 27/mar to 30/Oct, 2016. Review the above output, the java.time is able to calculate and handle the DST correctly.

References

  1. Wikipedia – Daylight saving time
  2. DateTimeFormatter JavaDoc
  3. ZoneddateTime JavaDoc
  4. Time Zone in Paris
  5. Airasia – Flight information
  6. Java 8 – Convert Instant to ZonedDateTime
  7. Java – Convert date and time between timezone
  8. Java 8 – How to convert String to LocalDate
由JSRUN为你提供的Java在线运行、在线编译工具
        JSRUN提供的Java 在线运行,Java 在线运行工具,基于linux操作系统环境提供线上编译和线上运行,具有运行快速,运行结果与常用开发、生产环境保持一致的特点。
yout