0

日付をフォーマットしてからAndroid StudioのTextViewに入れようとしています

2016 年 2 月 29 日 (月) 22:31:00 GMT

2016-02-29 22:31:00

私のGenymotion Google Nexus 5、Samsung Galaxy S5などのデバイスでは、完璧に見えます. このアプリケーションを実際の電話 (1 つだけではない) で実行すると、日付の TextView が空でした: getFormatPubDate は、「解析不能な日付」で ParseException をスローします。

 date = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z").parse(pubDate);

pubDate 変数の日付は正しく、null ではありません。

// strings.xml
    <string name="data_time_format">yyyy-MM-dd HH:mm:ss</string>

// someClass.java
    public String getFormatPubDate(String format){
                try {
                Date date = null;
                    date = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z").parse(pubDate);
                    return new SimpleDateFormat(format).format(date);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                return pubDate;
            }

// someActivity.java    
viewHolder.timePublishedTextView.setText(cachedNews.getFormatPubDate(activity.getResources().getString(R.string.data_time_format)));

PS HH (24 時間) の形式を使用していますが、時刻が正しくありません。エミュレータの例 - 2016 年 2 月 29 日月曜日 23:01:00 GMT を 2016-02-29 18:01:00 にフォーマット

4

1 に答える 1

0
 //This is original date format
 String date = "Mon, 29 Feb 2016 22:31:00";

 SimpleDateFormat sdf = new SimpleDateFormat("EE, dd MMM yyyy HH:mm:ss",
            Locale.ENGLISH);

 //This is new date format
 SimpleDateFormat print = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

 Date parsedDate = null;

 try 
 {
     parsedDate = sdf.parse(date);
 } catch (ParseException e) {
     e.printStackTrace();
 }

 String text = print.format(parsedDate);
于 2016-03-01T06:58:17.583 に答える