Parsing Data in ISO 8601 Format Without Colon in Offset
Problem
Java 8's DateTimeFormatter struggles to parse ISO 8601 strings lacking a colon between hours and minutes in the offset, causing difficulties for users attempting to convert strings like "2018-02-13T10:20:12.120 0000" into Java 8 dates.
Solution
Until Java 8 Bug Fix:
Until the bug in Java 8 is resolved, use the following workaround:
OffsetDateTime.parse( "2018-02-13T10:20:12.120+0000" , DateTimeFormatter.ofPattern( "uuuu-MM-dd'T'HH:mm:ss.SSSX" ) )
After Java 8 Bug Fix:
OffsetDateTime.parse( "2018-02-13T10:20:12.120+0000" )
Detailed Explanation
Wrong Class Usage
Avoid using legacy classes like Date, Calendar, and SimpleDateFormat in favor of modern java.time classes.
Use OffsetDateTime for Offset-Only Values
For values with offsets but no time zone, use OffsetDateTime. ISO 8601 strings typically comply with the standard formats used by java.time classes. However, a bug in Java 8 requires a workaround:
Hacky Workaround
Manipulate the input string to include the missing colon:
String input = "2018-02-13T10:20:12.120+0000".replace( "+0000" , "+00:00" ); OffsetDateTime odt = OffsetDateTime.parse( input );
More Robust Workaround
Define a DateTimeFormatter with an explicit pattern:
String input = "2018-02-13T10:20:12.120+0000" ; DateTimeFormatter f = DateTimeFormatter.ofPattern( "uuuu-MM-dd'T'HH:mm:ss.SSSX" ); OffsetDateTime odt = OffsetDateTime.parse( input , f );
Additional Considerations
- Always use offsets with a colon and both hours and minutes.
- Extract Instant objects to represent values in UTC.
- Apply time zones to view moments through the lens of specific regions' wall-clock time.
About the Java 8 Bug
The Java 8 bug affects the parsing of offsets missing the colon between hours and minutes. This issue affects offsets like 0000 but not 00:00.
Java.time
java.time classes supplant legacy date classes and are built into Java 8 and later. They provide more modern and efficient date-time handling capabilities.
The above is the detailed content of How to Parse ISO 8601 Dates with Missing Colon in Offset in Java 8?. For more information, please follow other related articles on the PHP Chinese website!

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.