【问题标题】:Month is not printed from a date - Java DateFormat月份不是从日期打印的 - Java DateFormat
【发布时间】:2013-10-02 21:09:36
【问题描述】:

如何从java中的日期获取月份:

        DateFormat inputDF  = new SimpleDateFormat("mm/dd/yy");
        Date date1 = inputDF.parse("9/30/11");

        Calendar cal = Calendar.getInstance();
        cal.setTime(date1);

        int month = cal.get(Calendar.MONTH);
        int day = cal.get(Calendar.DAY_OF_MONTH);
        int year = cal.get(Calendar.YEAR);

        System.out.println(month+" - "+day+" - "+year);

此代码返回日期和年份,但不返回月份。

输出:

0 - 30 - 2011

【问题讨论】:

  • 仅供参考:问题中看到的这些麻烦的旧日期时间类现在是遗留的,被 Java 8 及更高版本中内置的 java.time 类所取代。 ZonedDateTime.now().getMonthValue()

标签: java date calendar date-format


【解决方案1】:

是时候有人提供现代答案了。其他答案在 2013 年提出问题时是很好的答案,并且仍然正确。今天,你没有理由与旧的、过时的、同时臭名昭著的麻烦 SimpleDateFormat 类作斗争。 java.time,现代 Java 日期和时间 API,使用起来更方便:

    DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern("M/d/yy");
    LocalDate date1 = LocalDate.parse("9/30/11", inputFormatter);
    System.out.println(date1);

打印出来

2011-09-30

LocalDate 类代表一个没有时间的日期,正是您所需要的,它比旧类 DateCalendar 更精确地满足您的要求。

DateTimeFormatter 使用的格式模式字符串类似于 SimpleDateFormat 中的格式模式字符串,但有一些不同之处。您可以使用大写的 MM 来要求两位数的月份(如 09 表示九月)或单个 M 以允许使用一位或两位数写入月份。同样ddd 表示月份中的某天。 yy 表示两位数的年份,并以 2000 为基数进行解释,即从 2000 到 2099(包括我的生日)。

链接Oracle tutorial Trail: Date Time解释如何使用java.time

【讨论】:

    【解决方案2】:

    mm是分钟,在指定格式时使用MM

    Calendar cal = Calendar.getInstance();
    cal.setTime(date1);
    
    int month = cal.get(Calendar.MONTH);// returns month value index starts from 0
    

    【讨论】:

      【解决方案3】:

      首先,您在日期格式中使用了mmwhich is "minutes" according to the Javadocs。您将分钟设置为9,而不是月份。看起来月份默认为 0(一月)。

      使用MM(大写“M”)解析月份。然后,您将看到8,因为in Calendar months start with 0, not 1。添加1 以取回所需的9

      公历和儒略历中的第一个月是 一月是 0

      // MM is month, mm is minutes
      DateFormat inputDF  = new SimpleDateFormat("MM/dd/yy");  
      

      以后

      int month = cal.get(Calendar.MONTH) + 1; // To shift range from 0-11 to 1-12
      

      【讨论】:

        【解决方案4】:

        尝试使用MM 而不是mm:-

            DateFormat inputDF  = new SimpleDateFormat("MM/dd/yy");
            Date date1 = inputDF.parse("9/30/11");
        
            Calendar cal = Calendar.getInstance();
            cal.setTime(date1);
        
            int month = cal.get(Calendar.MONTH);
            int day = cal.get(Calendar.DAY_OF_MONTH);
            int year = cal.get(Calendar.YEAR);
        
            System.out.println(month+" - "+day+" - "+year);
        

        打印的月份将是 8,因为索引从 0 开始

        或尝试:-

        int month = cal.get(Calendar.MONTH) + 1;
        

        【讨论】:

        • 打印的月份为 8,因为月份的范围是 0-11,而不是 1-12。
        • @rgettman:- 在我的回答中添加了这一点。我认为 OP 必须意识到这一点!
        【解决方案5】:

        月份格式应为MM 而不是mm

         DateFormat inputDF  = new SimpleDateFormat("MM/dd/yy");
        

        【讨论】:

          【解决方案6】:

          这是因为您的格式不正确:您需要 "MM/dd/yy" 代表月份,因为 "mm" 代表分钟:

          DateFormat inputDF  = new SimpleDateFormat("MM/dd/yy");
          Date date1 = inputDF.parse("9/30/11");
          
          Calendar cal = Calendar.getInstance();
          cal.setTime(date1);
          
          int month = cal.get(Calendar.MONTH);
          int day = cal.get(Calendar.DAY_OF_MONTH);
          int year = cal.get(Calendar.YEAR);
          
          System.out.println(month+" - "+day+" - "+year);
          

          打印8 - 30 - 2011(因为月份是从零开始的;demo

          【讨论】:

            【解决方案7】:

            如果您阅读SimpleDateFormat javadoc,您会注意到mm 是分钟。您需要一个月的MM

            DateFormat inputDF  = new SimpleDateFormat("MM/dd/yy");
            

            否则该格式不会读取month 字段并假定值为0

            【讨论】:

              猜你喜欢
              • 2017-04-24
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2011-11-03
              • 1970-01-01
              • 2017-06-09
              相关资源
              最近更新 更多