`
阅读更多
public class ParseUtcDateTimeStringTest {

     @Test
     public void testUtcStringToDate() {
        // "Z" at the end of the date time string is for UTC
        String utcDateStr = "2017-08-28T02:03:41.080Z";
        System.out.println("====== testUtcStringToDate: " + utcDateStr);

        // Parse the utc datetime string to java.time.Instant - defaults formatter to DateTimeFormatter.ISO_INSTANT.
        Instant instant = Instant.parse(utcDateStr);
        System.out.println("Parsed instant in UTC: " + instant);

        // single quote around 'T'
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS");

        // convert to LocalDateTime with default time zone
        ZoneId defaultZone = ZoneOffset.systemDefault();
        System.out.println("defaultZone: " + defaultZone);
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.of(defaultZone.getId()));
        System.out.println("Converted localDateTime=" + localDateTime);

        // convert to ZonedDateTime - you cannot format an Instant, have to convert it to a formattable date time
        ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(instant, ZoneId.of(defaultZone.getId()));
        System.out.println("Formatted zonedDateTime=" + formatter.format(zonedDateTime));

        // get ZoneOffset and then ZoneRules
        ZoneOffset zoneOffset = zonedDateTime.getOffset();
        System.out.println("zoneOffset: " + zonedDateTime.getOffset());
        ZoneRules zoneRules = ZoneRules.of(zoneOffset);
        System.out.println("zoneRuels: " + zoneRules);

        // ZoneRules tells if it's daylight savings time...
        if (zoneRules.isDaylightSavings(instant)) {
            System.out.println("Auckland summer has daylight saving time, which is 13 hours ahead of UTC");
            Assert.assertEquals("2017-08-28T15:03:41.080", formatter.format(zonedDateTime));
        } else {
            System.out.println("Auckland has winter time 12 hours ahead of UTC");
            Assert.assertEquals("2017-08-28T14:03:41.080", formatter.format(zonedDateTime));
        }
    }
}
分享到:
评论

相关推荐

    Mastering Concurrency Programming with Java 8

    About This Book, Implement concurrent applications using the Java 8 Concurrency API and its new componentsImprove the performance of your applications or process more data at the same time,...

    CentOS解决服务器存在大量time_wait的问题

    近期服务器出现大量time_wait的TCP连接造成服务器连接数过多而最终导致tomcat假死状态。连接服务器查看连接数的时候提示如下。 [root@test apache-tomcat-7.0.53]# ...通过java API解决这个问题的路子就堵死了。

    java8集合源码-Java8:Java8

    api(java.time) Lambda 表达式 默认方法 流 可选的 路口类型 Javascript Base64 编码和解码 字符串连接器 类型注释 JDBC 改进 暖身 创建目录 mkdir java8 转到java8目录并应用gradle init命令创建java项目 gradle ...

    java8源码-java8:java8笔记和代码示例

    java8 源码 java8 java8 note and code example java8新特性 Stream Api Lambda 表达式 行为参数化 接口中的默认方法 Date Time Api Optional 类 Stream 和 Collection的区别 Stream 描述对数据的计算 Collection ...

    Java开发技术大全(500个源代码).

    onlyTest.java 对象传值示例 otherClass.java 从类的外部访问对象的成员 showInstVar.java 演示不同的对象拥有不同的成员变量 showMain.java 演示main方法访问本类成员 showMethod.java 演示如何定义一个方法体...

    Java邮件开发Fundamentals of the JavaMail API

    Fundamentals of the JavaMail API Presented by developerWorks, your source for great tutorials ibm.com/developerWorks Table of Contents If you're viewing this document online, you can click ...

    Java 高级特性.doc

    JAVA高级特性 1.静态导入:先举个离例子 。 import java.lang.Integer.parseInt; public class StaticImport { int x = parseInt("123"); System.out.println(x); } 这样的程序如果不在IDE 工具中输入,是很难...

    Selenium Design Patterns and Best Practices 最新 原版

    Save time and money by making your Selenium tests 99% reliable Improve the stability of your test suite and your programing skills by following a step-by-step continuous improvement tutorial Who ...

    selenium framework design data driven testing

    The Selenium WebDriver 3.x Technology is an open source API available to test both Browser and Mobile applications. It is completely platform independent in that tests built for one browser or mobile ...

    Selenium.Testing.Tools.Cookbook.2nd.Edition.178439251

    Set up Selenium Grid for faster and parallel running of tests, increasing test coverage and reducing test execution time for cross-browser testing Build extended Selenium WebDriver tests for ...

    Selenium Testing Tools Cookbook 最新 原版

    Set up Selenium Grid for faster and parallel running of tests, increasing test coverage and reducing test execution time for cross-browser testing Build extended Selenium WebDriver tests for ...

    Packt.Learn.Selenium.rar

    Using Java 8 Features with Selenium Exploring the Features of WebDriver Exploring Advanced Interactions of WebDriver Understanding WebDriver Events Exploring RemoteWebDriver Setting up Selenium ...

    simjava2网络仿真器

    It is an API that augments Java with building blocks for defining and running simulations. The original SimJava was based on HASE++, a C++ simulation library. HASE++ was in turn based on SIM++. The ...

    Building a 3D Game with LibGDX

    With a good plan on improvement while having a full time job as an Android Developer, he also spends a lot of time on the forum java-gaming.org learning and making contacts. He joined forces with a ...

    回头客短信营销系统软件

    $content = "test"; // 发送内容,不建议太长 $encode = "gb2312"; // 字符编码,可选参数 //可传 notify 或 ad , adcard,adfangchan,dxztd 五参数, notify: 表示通知类, ad: 表示106通道广告类,adcard: 表示卡...

    hibernate3.1 jar包

    - Jaxen - universal java xpath engine - runtime, required if you want to deserialize a Configuration to improve startup performance ant-junit-1.6.5.jar (1.6.5) - Ant junit support - buildtime ant-...

    HTTP_Debugger_Pro_V9.10_Crack.zip

    Debug HTTP API calls to a back-end and between back-ends, Easy to use, clean UI, and short ramp-up time. Not a proxy, no network issues! Doesn't change the browser's configuration for the proxy. Can ...

    logstash-input-rest:从 json 中的rest api读取的logstash的输入插件

    它允许您调用 rest API(当前生成 JSON)并在 logstash 事件中发送生成的 JSON。 这个插件背后的想法来自需要读取 springboot 指标端点,而不是配置 jmx 来监控我的 java 应用程序内存 / gc/​​ 等。 它是完全...

    Elasticsearch for Hadoop

    Approach 2 – Aggregate the results at query-time Writing the NetworkLogsMapper job Writing the mapper class Writing Driver Building the job Getting the data into HDFS Running the job Viewing ...

    新版Android开发教程.rar

    的 Android SDK 提供了在 Android 平台上使用 JaVa 语言进行 Android 应用开发必须的工具和 API 接口。 特性 • 应用程序框架 支持组件的重用与替换 • Dalvik Dalvik Dalvik Dalvik 虚拟机 专为移动设备优化 • ...

Global site tag (gtag.js) - Google Analytics