【问题标题】:Java date format conversion - getting wrong monthJava日期格式转换 - 得到错误的月份
【发布时间】:2012-02-04 22:38:30
【问题描述】:

我在java中转换日期时遇到问题,不知道我哪里出错了......

    String dateStr = "2011-12-15";
    String fromFormat = "yyyy-mm-dd";
    String toFormat = "dd MMMM yyyy";

    try {
        DateFormat fromFormatter = new SimpleDateFormat(fromFormat);
        Date date = (Date) fromFormatter.parse(dateStr);

        DateFormat toformatter = new SimpleDateFormat(toFormat);
        String result = toformatter.format(date);

    } catch (ParseException e) {
        e.printStackTrace();
    }

输入日期是 2011 年 12 月 15 日,我期望结果为“2011 年 12 月 15 日”,但我得到的结果是“2011 年 1 月 15 日”

我哪里错了?

【问题讨论】:

    标签: java date


    【解决方案1】:

    您的 fromFormat 使用分钟,而它应该使用月份。

    String fromFormat = "yyyy-MM-dd";
    

    【讨论】:

      【解决方案2】:

      我认为 fromFormat 应该是“yyyy-MM-dd”。

      格式如下:

      • m == 一小时分
      • M == 一年中的月份

      更多:http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html

      【讨论】:

        【解决方案3】:

        来自的格式应该是:

        String fromFormat = "yyyy-MM-dd"
        

        【讨论】:

          【解决方案4】:

          查看SimpleDateFormat 的javadoc 并查看m 代表什么。不是你想象的几个月,而是几分钟。

          【讨论】:

            【解决方案5】:
             String fromFormat = "yyyy-MM-dd"; 
            

            【讨论】:

              【解决方案6】:

              SimpleDateFormat 中的m 代表分钟,而M 代表月份。因此,您的第一个格式应该是yyyy-MM-dd

              【讨论】:

              • 我喜欢 SO 的一件事是问题得到如此迅速的回答。输入答案的 1 分钟内有 3 个新答案,太棒了!
              【解决方案7】:

              tl;博士

              LocalDate.parse( "2011-12-15" )                            // Date-only, without time-of-day, without time zone.
              .format(                                                   // Generate `String` representing value of this `LocalDate`. 
                  DateTimeFormatter.ofLocalizedDate( FormatStyle.LONG )  // How long or abbreviated?
                                   .withLocale(                          // Locale used in localizing the string being generated.
                                       new Locale( "en" , "IN" )         // English language, India cultural norms.
                                   )                                     // Returns a `DateTimeFormatter` object.
              )                                                          // Returns a `String` object.
              

              2011 年 12 月 15 日

              java.time

              虽然accepted Answer 是正确的(大写MM 表示月份),但现在有更好的方法。麻烦的旧日期时间类现在是遗留的,被 java.time 类所取代。

              您的输入字符串是标准的ISO 8601 格式。所以不需要为解析指定格式模式。

              LocalDate ld = LocalDate.parse( "2011-12-15" );  // Parses standard ISO 8601 format by default.
              Locale l = new Locale( "en" , "IN" ) ;  // English in India.
              DateTimeFormatter f = DateTimeFormatter.ofLocalizedDate( FormatStyle.LONG )
                                                     .withLocale( l );
              String output = ld.format( f );
              

              转储到控制台。

              System.out.println( "ld.toString(): " + ld );
              System.out.println( "output: " + output );
              

              ld.toString(): 2011-12-15

              输出:2011 年 12 月 15 日

              live code in IdeOne.com


              关于java.time

              java.time 框架内置于 Java 8 及更高版本中。这些类取代了麻烦的旧 legacy 日期时间类,例如 java.util.DateCalendarSimpleDateFormat

              Joda-Time 项目现在位于maintenance mode,建议迁移到java.time 类。

              要了解更多信息,请参阅Oracle Tutorial。并在 Stack Overflow 上搜索许多示例和解释。规格为JSR 310

              您可以直接与您的数据库交换 java.time 对象。使用符合JDBC 4.2 或更高版本的JDBC driver。不需要字符串,不需要java.sql.* 类。

              从哪里获得 java.time 类?

              ThreeTen-Extra 项目通过附加类扩展了 java.time。该项目是未来可能添加到 java.time 的试验场。您可以在这里找到一些有用的类,例如IntervalYearWeekYearQuartermore

              【讨论】:

                【解决方案8】:

                嗯,这可能不是你的情况,但可能会对某人有所帮助。在我的情况下,转换后,月份和月份设置为 1。所以无论日期是什么,转换后我得到 1 月,这是错误的。 经过努力,我发现在日期格式中我使用了YYYY 而不是yyyy。当我将所有大写字母 Y 更改为 y 时,它工作正常。

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 2021-04-09
                  • 2021-07-04
                  • 2015-04-29
                  • 1970-01-01
                  • 1970-01-01
                  • 2016-01-26
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多