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

'SimpleDateFormat' converts Dec 31, 2017 to 31 Dec, 2018

    Details

    • Subcomponent:
    • CPU:
      x86_64
    • OS:
      windows

      Description

      FULL PRODUCT VERSION :
      java version "1.8.0_45"
      Java(TM) SE Runtime Environment (build 1.8.0_45-b15)
      Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Windows ver 8.1

      A DESCRIPTION OF THE PROBLEM :
      Please find the code snippet -

      for(int i=1;i<=5;i++)
      {
      Calendar cal = Calendar.getInstance();
      cal.add(Calendar.DATE, -i);
      System.out.println(cal.getTime());
      SimpleDateFormat format = new SimpleDateFormat("MMM d, YYYY");
      System.out.println(format.format(cal.getTime()));

      }

      Output : -

      Tue Jan 02 15:40:15 IST 2018
      Jan 2, 2018
      Mon Jan 01 15:40:15 IST 2018
      Jan 1, 2018
      Sun Dec 31 15:40:15 IST 2017
      Dec 31, 2018
      Sat Dec 30 15:40:15 IST 2017
      Dec 30, 2017
      Fri Dec 29 15:40:15 IST 2017
      Dec 29, 2017


      As evident, the SimpleDateFormat is converting 31 Dec 2017 to 31 Dec, 2018

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :

      Please find the code snippet -

      for(int i=1;i<=5;i++)
      {
      Calendar cal = Calendar.getInstance();
      cal.add(Calendar.DATE, -i);
      System.out.println(cal.getTime());
      SimpleDateFormat format = new SimpleDateFormat("MMM d, YYYY");
      System.out.println(format.format(cal.getTime()));

      }

      Output : -

      Tue Jan 02 15:40:15 IST 2018
      Jan 2, 2018
      Mon Jan 01 15:40:15 IST 2018
      Jan 1, 2018
      Sun Dec 31 15:40:15 IST 2017
      Dec 31, 2018
      Sat Dec 30 15:40:15 IST 2017
      Dec 30, 2017
      Fri Dec 29 15:40:15 IST 2017
      Dec 29, 2017



      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Dec 31, 2017 after using SimpleDateFormat class
      ACTUAL -
      Dec 31, 2018 ( 2018 instead of 2017 )

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      for(int i=1;i<=5;i++)
      {
      Calendar cal = Calendar.getInstance();
      cal.add(Calendar.DATE, -i);
      System.out.println(cal.getTime());
      SimpleDateFormat format = new SimpleDateFormat("MMM d, YYYY");
      System.out.println(format.format(cal.getTime()));

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

      SUPPORT :
      YES

        Attachments

          Activity

            People

            • Assignee:
              psonal Pallavi Sonal
              Reporter:
              webbuggrp Webbug Group
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: