2016-05-23 4 views
0
私はフォーマット 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"

日付形式の例外金8月31日午後12時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()); 

アムIについてここに何かがない?

+0

日付をフォーマットしようとすると、 'unparseable date'例外が発生しません。 [最小で完全で検証可能な例](http://www.stackoverflow.com/help/mcve)を提供してください。 –

+0

@AndyTurner plsは私の編集をチェックします – Abx

答えて

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"); 
+0

ありがとう...その部分を逃しました... – Abx

2

あなたは二回そこに年のトークンを持っている:

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

あなたがすでにDate -objectを持っていること奇妙である、を経由してその標準出力を使用して、それをフォーマットしたいですtoString()を開き、再びDateオブジェクトに解析します。この手順では、元のDate -objectのミリ秒の部分も失われます(myObj.getDate())。とにかく、解析を行うための適切なフォーマットのパターンは次のとおりです。

EEE MMMはHHをddは:MM:SSのZZZのYYYY

と英語のおSimpleDateFormat -objectのロケールを設定することを忘れないでください。あなたはyyyy-partを2回持ち、 "H"(時)の代わりに "h"(am/pmの時)を使用したことに注意してください。 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"; 
関連する問題