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

SimpleDateFormat: 31/12/2018 interpreted as 31/12/2019

XMLWordPrintable

    • x86_64
    • windows_7

      ADDITIONAL SYSTEM INFORMATION :
      Windows 7 64bits, Java \jdk1.8.0_191 (download today)

      A DESCRIPTION OF THE PROBLEM :
      When formatting a 'java.util.Date' containing 31/12/2018 with 'java.text.SimpleDateFormat', it print 31/12/2019. It's look like the bug depend on where I reuse a previous initalized variable for 'java.text.SimpleDateFormat'

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Create a simple Java class 'Bzr' with command line. The display will illustrate the problem.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      31/12/2018
      07/01/2019
      07/01/2019
      31/12/2018
      ACTUAL -
      31/12/2018
      07/01/2019
      07/01/2019
      31/12/2019

      ---------- BEGIN SOURCE ----------
      import java.text.ParseException;
      import java.text.SimpleDateFormat;
      import java.util.Date;

      public class Bzr {

      public static void main(String[] args) throws ParseException {

      String dateString = "31/12/2018";
      String dateString2 = "07/01/2019";
      SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
      Date date = formatter.parse(dateString);
      Date date2 = formatter.parse(dateString2);

      //Work
      System.out.println(formatter.format(date));
      System.out.println(formatter.format(date2));
      System.out.println(new SimpleDateFormat("dd/MM/YYYY").format(date2));

      //Don't work
      System.out.println(new SimpleDateFormat("dd/MM/YYYY").format(date));
      }
      }
      ---------- END SOURCE ----------

      FREQUENCY : always


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

              Created:
              Updated:
              Resolved: