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

java.util.calendar set month to 1 happens error on 2022-06-30

    XMLWordPrintable

Details

    • generic
    • generic

    Description

      ADDITIONAL SYSTEM INFORMATION :
      Os: Windows10 ; Java Runtime Information: java version "1.8.0_91"

      A DESCRIPTION OF THE PROBLEM :
      function code:
      public static String acquireMonth(Integer month) throws Exception{
              SimpleDateFormat monthSdf = new SimpleDateFormat("yyyy-MM");
              SimpleDateFormat dateSdf = new SimpleDateFormat("yyyy-MM-dd");
              Calendar calendar = Calendar.getInstance();
              calendar.set(Calendar.MONTH,month);
              log.error(dateSdf.format(calendar.getTime()));
              return monthSdf.format(calendar.getTime());
          }

      test code:
      public static void main(String[] args) {
              try{
                 String month = acquireMonth(1);
                 System.out.println(month);
              }catch (Exception e){
                  e.printStackTrace();
              }
          }

      I want acquire February DateTime,I user
      Calendar calendar = Calendar.getInstance();
      calendar.set(Calendar.MONTH,1);

      but compiler execution result is : 2022-03
      I observe JDK execution result : calendar.getTime() => 2022-03-02
      I want to know why.


      Attachments

        Activity

          People

            Unassigned Unassigned
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: