2011-10-27 15 views
3

ありがとうGiulio Piancastelli私は今、複数のライン機能を持つリストビューを持っています。今、私は2行目の日付の書式設定に問題があります。すべての日付は同じです。飼料には違いがあります。日、月、年(2011年10月27日(木))の形式で日付をフォーマットするのに役立つ人が必要です。 Android Date format in ListView

この

動作しないコードです:私は私の以前の日付コードを削除する場合

List<Map<String, String>> data = new ArrayList<Map<String, String>>(); 
    for (RSSItem item : feed.getAllItems()) { 
     Map<String, String> datum = new HashMap<String, String>(2); 
     datum.put("title", item.getTitle()); 

     String dateStr = item.getPubDate(); 
     SimpleDateFormat curFormater = new SimpleDateFormat("EEEE, MMMM dd, yyyy"); 
     Date dateObj = new Date(); 
     try { 
      dateObj = curFormater.parse(dateStr); 
     } catch (ParseException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     SimpleDateFormat postFormater = new SimpleDateFormat("EEEE, MMMM dd, yyyy"); 

     String newDateStr = postFormater.format(dateObj); 

     datum.put("date", newDateStr); 
     data.add(datum); 
    } 
    SimpleAdapter adapter = new SimpleAdapter(this, data, 
               android.R.layout.simple_list_item_2, 
               new String[] {"title", "date"}, 
               new int[] {android.R.id.text1, 
                 android.R.id.text2}); 

    itemlist.setAdapter(adapter); 

    itemlist.setOnItemClickListener(this); 

    itemlist.setSelection(0); 

Repeating date

それがうまく動作しますが、間違ってフォーマットされています。

このコードは、作業を行いますが、間違ってフォーマットされます。

List<Map<String, String>> data = new ArrayList<Map<String, String>>(); 
    for (RSSItem item : feed.getAllItems()) { 
     Map<String, String> datum = new HashMap<String, String>(2); 
     datum.put("title", item.getTitle()); 
     datum.put("date", item.getPubDate().toString()); 
     data.add(datum); 
    } 
    SimpleAdapter adapter = new SimpleAdapter(this, data, 
               android.R.layout.simple_list_item_2, 
               new String[] {"title", "date"}, 
               new int[] {android.R.id.text1, 
                 android.R.id.text2}); 

    itemlist.setAdapter(adapter); 

    itemlist.setOnItemClickListener(this); 

    itemlist.setSelection(0); 

incorrect date format

は、私は私は日、月、年(10月(木曜日)の形式で私の日付の書式を助けるために誰かを必要とします27、2011)。ありがとうございました!

+0

したがって、基本的に12:00:00 GMTの部分を取り除きたいですか? –

+0

日に日、月、年を読みたい。このように:2011年10月27日木曜日 –

+0

最初のスクリーンショットで見ているものがあなたの望むものではありませんか? –

答えて

4

問題は、あなたが後で形式にそれを使用している同じパターンで解析文字列にしようとしているということです。あなたはそれがすでにあるパターンで解析するべきです、明らかに "Fri、21 Oct 2011 12:00:00 GMT"のように見えます。

だから私はあなたが何かしたい疑う:利用可能なフォーマットを見てみましょう

SimpleDateFormat curFormater = new SimpleDateFormat("EEEE, MMMM dd, yyyy"); 

// You probably actually want to set the time zone of the 
// formatting pattern - but you'll need to think what time zone you 
// really want. We don't know enough to say. Ditto the locale... 
private static final DateFormat PARSING_PATTERN = 
    new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US); 
private static final DateFormat FORMATTING_PATTERN = 
    new SimpleDateFormat("EEEE, MMMM dd, yyyy"); 

... 

for (RSSItem item : feed.getAllItems()) { 
    Map<String, String> datum = new HashMap<String, String>(2); 
    datum.put("title", item.getTitle()); 

    String outputDate; 
    try { 
     Date date = PARSING_PATTERN.parse(item.getPubDate()); 
     outputDate = FORMATTING_PATTERN.format(date); 
    } catch (ParseException e) { 
     outputDate = "Invalid date"; // Or whatever... 
    } 
    datum.put("date", outputDate); 
    data.add(datum); 
} 
+0

私は今度はcatchステートメントを打つ。この問題をどうやって解決するのですか? –

+0

@CKallemeres:ハングアップ - 私はちょうど私がカンマが多すぎることを発見した。今すぐ試してください - 解析パターンを少し変更しました。 –

+0

それはまだ起こっている。 –

1

EDIT:

あなたはこの行を変更する必要がありますがSimpleDateFormatになります。これを変更する内容は、使用しているRSSフィードから返される形式に依存します。

はさて、今日は今ある、あなたのdateObjnew Date()に設定されたまま、私はあなたがそれを処理していないので、あなたがParseExceptionを取得し、されていることである何が起こっているのかを考える10月27日(木曜日)、2011年です。

読み込んでいるパターンが間違っている可能性があります。 (実際に出力したい書式と同じですので、意味が分かりません)RSSフィードから入力されたパターンを見て、構文解析に使用するパターンをDateオブジェクトに再評価します。

また、解析が失敗した場合はRuntimeExceptionを投げ、ParseExceptionを飲み込まないようにします。

try { 
     dateObj = curFormater.parse(dateStr); 
    } catch (ParseException e) { 
     throw new RuntimeException("Couldn't parse dateStr: " + dateStr); 
    } 
+0

どうすれば解決できますか? –