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

java.time.format.DateTimeFormatter error in API doc example

XMLWordPrintable

    • b22
    • x86_64
    • windows_7

        A DESCRIPTION OF THE PROBLEM :
        The description for the DateTimeFormatter provides two Java snippets which have several issues.

        These are those two examples:

        First example:

          String text = date.toString(formatter);
          LocalDate date = LocalDate.parse(text, formatter);

        Second example:

          DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy MM dd");
          String text = date.toString(formatter);
          LocalDate date = LocalDate.parse(text, formatter);

        The Issues:
        [1] There is no need to provide both examples in the description; the second is sufficient.
        [2] Both examples are invalid Java.
        [3] Both examples reference a variable name "date" before it is declared.
        [4] Both examples pass method "toString()" a "DateTimeFormatter" argument, but no such method exists in the JDK. Presumably the calls should be to method format() rather than toString().







        EXPECTED VERSUS ACTUAL BEHAVIOR :
        EXPECTED -
        A clearer and valid example for the DateTimeFormatter documentation might be:

                    LocalDate date1 = LocalDate.of(2014, 10, 25);
                    System.out.println("date1: " + date1.toString());
                    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy MM dd");
                    String text = date1.format(formatter);
                    LocalDate date2 = LocalDate.parse(text, formatter);
                    System.out.println("date2: " + date2.toString());

        ACTUAL -
        The following erroneous text appears in the DateTimeFormatter documentation:

          String text = date.toString(formatter);
          LocalDate date = LocalDate.parse(text, formatter);

        .....

          DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy MM dd");
          String text = date.toString(formatter);
          LocalDate date = LocalDate.parse(text, formatter);


        URL OF FAULTY DOCUMENTATION :
        http://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html

              rriggs Roger Riggs
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

                Created:
                Updated:
                Resolved: