解析异常,不可解析的日期

时间:2014-09-17 12:20:04

标签: android parsing date simpledateformat

每当我在我的nexus 5智能手机上执行我的应用程序时,我都会收到此异常,但是当我在平板电脑nexus 7上测试它时,它不会发生,为什么会这样?这是班级:

package com.example.unisannioavvisiv1;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;


public class RSSItem 
{
    private String titolo = null;
    private String descrizione = null;
    private String link = null;
    private String dataPub = null;

    private SimpleDateFormat  dataOutFormato =
            new SimpleDateFormat("EEE dd/MM/yy HH:mm ", Locale.getDefault());
    private SimpleDateFormat dataInFormato =
            new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z", Locale.getDefault());
    public String getTitolo() {
        return titolo;
    }

    public void setTitolo(String titolo) {
        this.titolo = titolo;
    }

    public String getDescrizione() {
        return descrizione;
    }

    public void setDescrizione(String descrizione) {
        this.descrizione = descrizione;
    }

    public String getLink() {
        return link;
    }

    public void setLink(String link) {
        this.link = link;
    }

    public String getDataPub() {
        return dataPub;
    }

    public void setDataPub(String dataPub) {
        this.dataPub = dataPub;
    }


    /*
     * Un funzione che converte la data di pubblicazione dalla modalita' lunga
     * in una piu' semplice da leggere per il lettore. Va bene per il formato anglosassone.
     */
    public String ottieniDataPubFormattata()
    {
        try
        {
            Date data = this.dataInFormato.parse(this.dataPub.trim());
            String dataPubFormattata =  this.dataOutFormato.format(data);
            return dataPubFormattata;

        }
        catch(ParseException e)
        {
            throw new RuntimeException(e);
        }
    }


}

这是我在应用程序执行时在logcat中获得的内容:

09-17 14:13:10.325: D/dalvikvm(28669): GC_FOR_ALLOC freed 240K, 2% free 17032K/17304K, paused 16ms, total 16ms
09-17 14:13:10.505: D/Avvisi Unisannio_FileIO_downloadFile_OK(28669): File Exists!   /data/data/com.example.unisannioavvisiv1/files/avvisi_feed.xml
09-17 14:13:10.505: D/Unisannio Avvisi(28669): Feed scaricati
09-17 14:13:10.565: D/dalvikvm(28669): GC_FOR_ALLOC freed 302K, 2% free 17242K/17576K, paused 9ms, total 9ms
09-17 14:13:10.585: D/Unisannio Avvisi ASYNC TASK(28669): Feed letti
09-17 14:13:10.595: D/AndroidRuntime(28669): Shutting down VM
09-17 14:13:10.595: W/dalvikvm(28669): threadid=1: thread exiting with uncaught exception (group=0x415f2ba8)
09-17 14:13:10.595: E/AndroidRuntime(28669): FATAL EXCEPTION: main
09-17 14:13:10.595: E/AndroidRuntime(28669): Process: com.example.unisannioavvisiv1, PID: 28669
09-17 14:13:10.595: E/AndroidRuntime(28669): java.lang.RuntimeException: java.text.ParseException: Unparseable date: "Wed, 17 Sep 2014 12:49:19 +0200" (at offset 0)
09-17 14:13:10.595: E/AndroidRuntime(28669):    at com.example.unisannioavvisiv1.RSSItem.ottieniDataPubFormattata(RSSItem.java:68)
09-17 14:13:10.595: E/AndroidRuntime(28669):    at com.example.unisannioavvisiv1.ElencoAvvisiActivity.aggiorna_e_visualizza(ElencoAvvisiActivity.java:134)
09-17 14:13:10.595: E/AndroidRuntime(28669):    at com.example.unisannioavvisiv1.ElencoAvvisiActivity$ReadFeed.onPostExecute(ElencoAvvisiActivity.java:108)
09-17 14:13:10.595: E/AndroidRuntime(28669):    at com.example.unisannioavvisiv1.ElencoAvvisiActivity$ReadFeed.onPostExecute(ElencoAvvisiActivity.java:1)
09-17 14:13:10.595: E/AndroidRuntime(28669):    at android.os.AsyncTask.finish(AsyncTask.java:632)
09-17 14:13:10.595: E/AndroidRuntime(28669):    at android.os.AsyncTask.access$600(AsyncTask.java:177)
09-17 14:13:10.595: E/AndroidRuntime(28669):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
09-17 14:13:10.595: E/AndroidRuntime(28669):    at android.os.Handler.dispatchMessage(Handler.java:102)
09-17 14:13:10.595: E/AndroidRuntime(28669):    at android.os.Looper.loop(Looper.java:136)
09-17 14:13:10.595: E/AndroidRuntime(28669):    at android.app.ActivityThread.main(ActivityThread.java:5001)
09-17 14:13:10.595: E/AndroidRuntime(28669):    at java.lang.reflect.Method.invokeNative(Native Method)
09-17 14:13:10.595: E/AndroidRuntime(28669):    at java.lang.reflect.Method.invoke(Method.java:515)
09-17 14:13:10.595: E/AndroidRuntime(28669):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
09-17 14:13:10.595: E/AndroidRuntime(28669):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
09-17 14:13:10.595: E/AndroidRuntime(28669):    at dalvik.system.NativeStart.main(Native Method)
09-17 14:13:10.595: E/AndroidRuntime(28669): Caused by: java.text.ParseException: Unparseable date: "Wed, 17 Sep 2014 12:49:19 +0200" (at offset 0)
09-17 14:13:10.595: E/AndroidRuntime(28669):    at java.text.DateFormat.parse(DateFormat.java:555)
09-17 14:13:10.595: E/AndroidRuntime(28669):    at com.example.unisannioavvisiv1.RSSItem.ottieniDataPubFormattata(RSSItem.java:61)
09-17 14:13:10.595: E/AndroidRuntime(28669):    ... 14 more
09-17 14:13:12.035: I/Process(28669): Sending signal. PID: 28669 SIG: 9

3 个答案:

答案 0 :(得分:1)

如果您要解析的日期时间字符串始终为英语,请使用Locale.US而不是Locale.getDefault()SimpleDateFormat一起将其解析为美国英语,而不是您的设备' s目前的设置,如意大利语。

答案 1 :(得分:1)

问题是我的输入日期格式是英语,而我的nexus 5是用意大利语设置的。因此我的defalut语言环境将启动异常。这是我解决的问题,感谢blackbelt和laalto的帮助。

private SimpleDateFormat  dataOutFormato =
        new SimpleDateFormat("EEE dd/MM/yy HH:mm ", Locale.ITALIAN);
private SimpleDateFormat dataInFormato =
        new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z", Locale.ENGLISH);
public String getTitolo() {
    return titolo;
}

答案 2 :(得分:0)

这与您的问题完全无关,但用英语编码所有内容是一种很好的做法:)