【问题标题】:Error when converting date format to another将日期格式转换为另一种格式时出错
【发布时间】:2014-11-29 12:08:42
【问题描述】:

我有一个格式为“YYYY-MM-dd”的字符串,我想将其转换为“MMM dd, yyyy”格式。

我使用下面的代码来做到这一点; 但是当我转换“2014-11-18”时,输出是这个“Sun Dec 29 00:00:00 IST 2013

我该如何解决这个问题?

DateFormat target=new SimpleDateFormat("MMM dd, yyyy");
String P_date="2014-11-18"
Date test1 = new SimpleDateFormat("YYYY-MM-dd").parse(P_date);

String converted_date=target.format(test1);
Date test=target.parse(converted_date);

【问题讨论】:

  • A Date 是自 epoc UTC 以来毫秒的时间戳。它没有被格式化。您使用格式化程序打印 Date。您正在获取Date,将其格式化为String,然后将其解析回Date。因此,所有格式都丢失了。您可能正在使用System.out.println(test) 打印Date,大概。

标签: java date-format simpledateformat jdatechooser


【解决方案1】:

y(小写 Y)格式表示“年”。 Y(大写 Y)您使用的意思是“WeekYear”。 只需使用y 就可以了:

DateFormat target=new SimpleDateFormat("MMM dd, yyyy");
String P_date="2014-11-18";
Date test1 = new SimpleDateFormat("yyyy-MM-dd").parse(P_date);

String converted_date=target.format(test1);
Date test=target.parse(converted_date);

【讨论】:

    【解决方案2】:

    Y 返回周年,这就是为什么您也看到工作日。用 y 代替。

    Date test1 = new SimpleDateFormat("yyyy-MM-dd").parse(P_date);
    

    【讨论】:

      【解决方案3】:

      你可以这样写

      final JDateChooser startDateChooser = new JDateChooser();
      startDateChooser.setDateFormatString("yyyy-MM-dd");
      Date startDate = startDateChooser.getDate();
      HashMap listMap = new HashMap();
      listMap.put("Start Period is ", ((startDate.getYear() + 1900)+ "-" + (startDate.getMonth() + 1) + "-" +startDate.getDate()));
      

      【讨论】:

        【解决方案4】:

        tl;博士

        LocalDate.parse( "2014-11-18" ).format(    // Parse string as `LocalDate` object, then generate a string in a certain format.
            DateTimeFormatter.ofLocalizedDate( FormatStyle.MEDIUM )
                .withLocale( Locale.US )           // Automatically localize to a locale’s human language and cultural norms.
        )                                          // Returns a String.
        

        详情

        accepted Answer by Mureinik 是正确的,您的格式模式使用的代码不正确。

        另一个问题是您对仅日期值感兴趣,但您使用的是日期与时间类型。

        此外,您正在使用麻烦的旧日期时间类,这些类现在已被 java.time 类所取代。

        java.time

        您的 YYYY-MM-DD 格式符合 ISO 8601 格式。 java.time 类在解析/生成字符串时默认使用这些标准格式。所以不需要指定格式模式。

        LocalDate ld = LocalDate.parse( "2014-11-18" ) ;
        

        要生成其他格式的字符串,请使用DateTimeFormatterDateTimeFormatterBuilder 类。

        您可以指定硬编码格式模式。但最好通过让 java.time 自动本地化来进行软编码。要进行本地化,请指定:

        • FormatStyle 确定字符串的长度或缩写。
        • Locale 确定 (a) 用于翻译日期名称、月份名称等的人类语言,以及 (b) 决定缩写、大写、标点符号、分隔符等问题的文化规范。

        例子:

        Locale l = Locale.US ;  // Or Locale.CANADA_FRENCH, Locale.ITALY, etc. 
        DateTimeFormatter f = DateTimeFormatter.ofLocalizedDate( FormatStyle.MEDIUM ).withLocale( l );
        String output = zdt.format( f );
        

        2014 年 11 月 18 日


        关于java.time

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

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

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

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

        从哪里获得 java.time 类?

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-08-27
          相关资源
          最近更新 更多