使用simpledateformat将Date转换为String
将日期转换为不同格式的字符串时遇到问题。 日期:
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
yyyy-mm-dd HH:mm:ss
这是错误的。 这应该是yyyy-MM-dd HH:mm:ss
。 有关将日期从一种格式转换为另一种格式的更多详细信息,请参阅此http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html 。
您使用了错误的格式月份。
yyyy-mm-dd HH:mm:ss ----> yyyy-MM-dd HH:mm:ss mm--->Minutes. MM--->Months
尝试
DateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
更改
DateFormat outputFormat = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss");
至
DateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
DateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); mm for Minutes MM for Months