Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8163842

java.time.Duration.toString negative value violates format

XMLWordPrintable

    • generic
    • generic

      FULL PRODUCT VERSION :
      java version "1.8.0_101"
      Java(TM) SE Runtime Environment (build 1.8.0_101-b13)
      Java HotSpot(TM) 64-Bit Server VM (build 25.101-b13, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [Version 6.1.7601]

      A DESCRIPTION OF THE PROBLEM :
      See bug JDK-8145166; this is a different report but related. If a duration is negative, the minus sign is placed in the incorrect place. An example output is "PT-1S" while the proper output should be "-PT1S".

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Create a negative duration, call toString. See source example below.


      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      N/A, however if using JAXB to marshal a Duration to XML, will generate an exception when validating the format.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.time.Duration;
      import org.junit.Assert;
      import org.junit.Test;

      public class XmlTest {

      @Test
      public void test() throws InterruptedException{
      Duration d = Duration.ofSeconds(-1);
      Assert.assertTrue(d.isNegative()); //Passes
      String actual = d.toString();
      String expected = "-PT1S";
      System.out.println(actual);
      Assert.assertEquals(expected, actual); //Fails, java bug
      }

      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      public static String printDuration(Duration value) {
      if (value == null)
      return null;
      String str = value.toString();
      /**
      * Bug in Java duration printing, it' doing negative sign in wrong place, e.g. "PT-0.25S". It should be "-PT0.25S"
      */
      if (value.isNegative()) {
      str = str.replace("-", "");
      str = "-" + str;
      }
      return str;
      }

            psonal Pallavi Sonal (Inactive)
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: