【问题标题】:Convert Date to String using simpledateformat使用 simpledateformat 将日期转换为字符串
【发布时间】:2013-02-27 11:37:53
【问题描述】:

我在将日期转换为不同格式的字符串时遇到问题。日期:

lastDownloadDate>>Wed Feb 27 16:20:23 IST 2013
lastChangeDate>>Wed Feb 27 15:11:00 IST 2013

我想将此格式转换为另一种格式:yyyy-mm-dd HH:mm:ss

当我尝试转换它时,我得到了不同的结果:

DateFormat outputFormat = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss");


String lastDownloadTimeVal = outputFormat.format(lastDownloadDate);
System.out.println("lastDownloadTimeVal>>"+lastDownloadTimeVal);
String lastChangeTimeVal = outputFormat.format(lastChangeDate);
System.out.println("lastChangeTimeVal>>"+lastChangeTimeVal);

我得到了错误的结果,月份被替换为分钟:

lastDownloadTimeVal>>2013-20-27 16:20:23
lastChangeTimeVal>>2013-11-27 15:11:00

【问题讨论】:

  • 月份模式是 MM 而不是 mm
  • @MiguelPrz 是唯一一个解释为什么 OP 应该使用 M 而不是 m 表示月份的人。 +1。如果它是一个答案,我只给了你一个赞成票。

标签: java date simpledateformat


【解决方案1】:

yyyy-mm-dd HH:mm:ss这是错误的。这应该是yyyy-MM-dd HH:mm:ss。 有关将日期从一种格式转换为另一种格式的更多详细信息,请参阅此http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html

【讨论】:

  • 这是我的错字问题。而不是 MM 我在代码中匆忙使用了 mm。 :(
  • @user2057006 这不是一个大错误。
  • 我们都曾在某个时候犯过同样的错误 :) 顺便说一句,你会考虑将 Apurv 的答案标记为已解决吗?
【解决方案2】:

您使用了错误的月份格式。

yyyy-mm-dd HH:mm:ss ----> yyyy-MM-dd HH:mm:ss
mm--->Minutes. 
MM--->Months

【讨论】:

    【解决方案3】:

    试试

    DateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    

    【讨论】:

      【解决方案4】:

      改变

      DateFormat outputFormat = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss");
      

      DateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      

      【讨论】:

        【解决方案5】:
        DateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        mm for Minutes
        MM for Months
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-04-09
          • 1970-01-01
          • 2017-06-05
          • 2018-11-03
          • 1970-01-01
          • 2023-04-05
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多