截至8月31日00:00:00 CAT 2012

时间:2016-05-23 07:24:50

标签: java simpledateformat

我尝试使用格式Fri Aug 31 00:00:00 CAT 2012格式化日期EEE MMM dd yyyy hh:mm:ss zzzz yyyy,但我正在Unparseable date: "Fri Aug 31 00:00:00 CAT 2012"

使用此代码

String DATE_FORMAT = "EEE MMM dd yyyy hh:mm:ss zzzz yyyy"; 
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT); 
Date date = sdf.parse(myObj.getDate().toString());

我在这里错过了什么吗?

3 个答案:

答案 0 :(得分:2)

您的格式额外有yyyy。 试试这个:

SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd hh:mm:ss zzzz yyyy");
Date date = sdf.parse("Fri Aug 31 00:00:00 CAT 2012");

答案 1 :(得分:2)

你有两年的年份代币:

String DATE_FORMAT = "EEE MMM dd yyyy hh:mm:ss zzzz yyyy"; 
                                 ^ remove this one.

答案 2 :(得分:1)

奇怪的是,您已经拥有Date - 对象,希望通过toString()使用标准输出对其进行格式化,然后再将其解析为Date - 对象。此过程甚至会丢失原始Date - 对象的毫秒部分(myObj.getDate())。无论如何,进行解析的正确格式模式是:

   EEE MMM dd HH:mm:ss zzz yyyy

并且不要忘记将SimpleDateFormat - 对象的区域设置为英语。请注意,您有两次yyyy-part并且还使用了" h" (上午/下午)代替" H" (一天中的小时)。另请参阅类java.util.Date的源代码:

/**
 * Converts this <code>Date</code> object to a <code>String</code>
 * of the form:
 * <blockquote><pre>
 * dow mon dd hh:mm:ss zzz yyyy</pre></blockquote>
 * where:<ul>
 * <li><tt>dow</tt> is the day of the week (<tt>Sun, Mon, Tue, Wed,
 *     Thu, Fri, Sat</tt>).
 * <li><tt>mon</tt> is the month (<tt>Jan, Feb, Mar, Apr, May, Jun,
 *     Jul, Aug, Sep, Oct, Nov, Dec</tt>).
 * <li><tt>dd</tt> is the day of the month (<tt>01</tt> through
 *     <tt>31</tt>), as two decimal digits.
 * <li><tt>hh</tt> is the hour of the day (<tt>00</tt> through
 *     <tt>23</tt>), as two decimal digits.
 * <li><tt>mm</tt> is the minute within the hour (<tt>00</tt> through
 *     <tt>59</tt>), as two decimal digits.
 * <li><tt>ss</tt> is the second within the minute (<tt>00</tt> through
 *     <tt>61</tt>, as two decimal digits.
 * <li><tt>zzz</tt> is the time zone (and may reflect daylight saving
 *     time). Standard time zone abbreviations include those
 *     recognized by the method <tt>parse</tt>. If time zone
 *     information is not available, then <tt>zzz</tt> is empty -
 *     that is, it consists of no characters at all.
 * <li><tt>yyyy</tt> is the year, as four decimal digits.
 * </ul>
 *
 * @return  a string representation of this date.
 * @see     java.util.Date#toLocaleString()
 * @see     java.util.Date#toGMTString()
 */
public String toString() {
    // "EEE MMM dd HH:mm:ss zzz yyyy";