JDK-8054978 : java.time.Duration.parse() fails for negative duration with 0 seconds and nanos
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.time
  • Affected Version: 8u11
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • OS: windows_7
  • CPU: x86_64
  • Submitted: 2014-08-12
  • Updated: 2021-09-15
  • Resolved: 2015-11-13
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 8 JDK 9
8-poolUnresolved 9 b94Fixed
Related Reports
Duplicate :  
Duplicate :  
Duplicate :  
Relates :  
Description
FULL PRODUCT VERSION :
java version "1.8.0_11"
Java(TM) SE Runtime Environment (build 1.8.0_11-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.11-b03, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
Windows 7 64-bit

A DESCRIPTION OF THE PROBLEM :
First of all sorry if this is not in the correct category, I could not find the relevant category for java.time.*

When parsing a negative Duration with 0 seconds and a few nanoseconds, the nanoseconds are wrongly considered positive:

System.out.println(Duration.parse("PT-1.5S")); // PT-1.5S : ok
System.out.println(Duration.parse("PT-0.5S")); // PT0.5S : error



STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
System.out.println(Duration.parse("PT-0.5S"));

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
PT-0.5S
ACTUAL -
PT0.5S

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
package com.company;

import java.time.*;

public class Main {
    public static void main(String[] args) {

        System.out.println(Duration.parse("PT-0.5S"));
    }
}

---------- END SOURCE ----------


Comments
Looks like this was never back-ported to 8, which it should have been.
07-02-2017

URL: http://hg.openjdk.java.net/jdk9/jdk9/jdk/rev/7bc80f556d22 User: lana Date: 2015-11-25 21:18:22 +0000
25-11-2015

URL: http://hg.openjdk.java.net/jdk9/dev/jdk/rev/7bc80f556d22 User: rriggs Date: 2015-11-13 16:53:27 +0000
13-11-2015

While fixing the issue, the following Javadoc should also be corrected in Duration.parse(CharSequence): "P-6H3M" -- parses as "-6 hours and +3 minutes" "-P6H3M" -- parses as "-6 hours and -3 minutes" "-P-6H+3M" -- parses as "+6 hours and -3 minutes" All three examples, should have a "T" after the "P". See also http://stackoverflow.com/questions/28958870/bug-in-java-time-duration
10-03-2015

Patched in threeten backport as shown in the patch. All necessary rights granted to OpenJdk as per OCA.
20-08-2014