Giter Site home page Giter Site logo

xsavikx / jdk-zone-parsing-inconsistency Goto Github PK

View Code? Open in Web Editor NEW
0.0 3.0 0.0 70 KB

A companion repository for showcasing inconsistency in JDK `DateTimeFormatter` parsing behavior. JDK issues are https://bit.ly/JDK-8257667 and https://bit.ly/JDK-8257647.

Home Page: https://bit.ly/JDK-8257647

Kotlin 13.68% Java 86.32%
java jdk bugs java-time

jdk-zone-parsing-inconsistency's Introduction

JDK ZoneId parsing inconsistency

This repository contains a reproducible example of the Java time API usage inconsistency across different JDK versions.

The problem

The UTC time zone specified in a String with date and time is parsed to a different ZoneId in Java11+ rather than in Java8.

Consider the following pattern MM/dd/yyyy hh:mm a z used to create a DateTimeFormatter with the US locale.

When a date string such as e.g. 05/17/2021 09:23 PM UTC is parsed to a ZonedDateTime using the formatter, the ZoneId value of the ZonedDateTime is different when different JDK versions are used.

For JDK 8-10 and 12-14 the value of the ZoneId is UTC. But for JDK 11 and JDK 15 the value is equal to Etc/UTC.

Environments

Windows Server 2019, macOS 10.15, Ubuntu 18.04.

Java 11, Java 15.

Steps to reproduce

The easiest way to reproduce the issue is to clone the repository and run ./gradlew build.

Otherwise, you may follow the steps below.

  1. Create DateTimeFormatter with the MM/dd/yyyy hh:mm a z pattern:

    DateTimeFormatter ZONED_FORMATTER = DateTimeFormatter.ofPattern("MM/dd/yyyy hh:mm a z", Locale.US);
    
  2. Parse date-time string 05/17/2021 09:23 PM UTC to a ZonedDateTime object:

    ZonedDateTime result = ZONED_FORMATTER.parse(dateString, ZonedDateTime::from);
    
  3. Check ZoneId value of the result:

    assert result.getZone().equals(ZoneId.of("UTC"));
    

Here's a full compilable example that fails in Java 11 and Java 15 environments.

final class ZonedParser {

    public static void main(String[] args) {
        DateTimeFormatter ZONED_FORMATTER = DateTimeFormatter.ofPattern("MM/dd/yyyy hh:mm a z", Locale.US);
        ZonedDateTime result = ZONED_FORMATTER.parse("05/17/2021 09:23 PM UTC", ZonedDateTime::from);
        if (!result.getZone().equals(ZoneId.of("UTC"))) {
            throw new IllegalStateException("Zone ID must be equal to `UTC`.");
        }
    }
}

jdk-zone-parsing-inconsistency's People

Contributors

yuri-sergiichuk avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.