簡體   English   中英

CAT 2012年8月31日星期五00:00:00的日期格式異常

[英]Date format exception for Fri Aug 31 00:00:00 CAT 2012

我正在嘗試使用EEE MMM dd yyyy hh:mm:ss zzzz yyyy格式格式化日期Fri Aug 31 00:00:00 CAT 2012 ,但是卻得到了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());

我在這里想念什么嗎?

您的格式還有額外的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");

您在其中兩次獲得了年令牌:

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

奇怪的是,您已經有一個Date ,想要通過其標准輸出通過toString()對其進行格式化,然后再次將其解析為Date 此過程甚至失去了原始Date的毫秒級分數(通過myObj.getDate() )。 無論如何,正確的格式模式進行解析是:

EEE MMM dd HH:mm:ss zzz yyyy

並且不要忘記將SimpleDateFormat -object的語言環境設置為英語。 請注意,您有兩次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";

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM