2012-01-18 10 views
6

Vorrei analizzare una data. La mia data per le stringhe è "Gio 19 Gen 2012 20:00". E il mio codice da analizzare è:java.text.ParseException: Data non analizzabile: "Gio 19 Gen 2012 08:00 PM"

format = new SimpleDateFormat("EEE MMM dd yyyy hh:mm aaa"); 
this.settDate(new Timestamp((format.parse(sDate)).getTime())); 

Tuttavia, non funziona. Come posso analizzare questa data?

metodo completo è:

public void saveTask(int iDevice, String description, String sDate) throws ParseException { 
    format = new SimpleDateFormat("EEE MMM dd yyyy hh:mm aaa"); 
    this.setiDeviceId(iDevice); 
    this.setsDescription(description); 
    this.settDate(new Timestamp((format.parse(sDate)).getTime())); 
    DatabaseManager.save(this); 
} 

E eccezione:

java.text.ParseException: Unparseable date: "Thu Jan 19 2012 01:00 AM" 

Debug immagine:

enter image description here

Grazie!

+0

Hai provato 'format = new SimpleDateFormat (" EEE MMM gg aaaa hh: mm a ");' – bdares

+0

il tuo codice funziona correttamente per me. Mostra altro codice. – dogbane

+0

il tuo formato va bene, funziona per me, ricevo: 'Gio 19:00 20:00 SAST 2012' qual è il tuo errore, come funziona? – epoch

risposta

15

Prova sottostante Codice ... Testato e lavorato

String dateStr = "Thu Jan 19 2012 01:00 PM"; 
    DateFormat readFormat = new SimpleDateFormat("EEE MMM dd yyyy hh:mm aaa"); 

    DateFormat writeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
    Date date = null; 
    try { 
     date = readFormat.parse(dateStr); 
    } catch (ParseException e) { 
     e.printStackTrace(); 
    } 

    String formattedDate = ""; 
    if(date != null) { 
    formattedDate = writeFormat.format(date); 
    } 

    System.out.println(formattedDate); 

uscita è 2012-01-19 13:00:00

Evviva !!! Felice di aiutare!!!

1

Qual è la locale predefinita? Dal momento che la stringa data è in inglese, provare l'analisi con la locale US:

DateFormat format = new SimpleDateFormat("EEE MMM dd yyyy hh:mm a", Locale.US); 
+0

Ho provato sia nel Regno Unito che negli Stati Uniti e funzionano. – Andrey

+1

@Andrey Sì, ma le impostazioni locali predefinite di Jose Hdez probabilmente non sono Stati Uniti o Regno Unito, quindi riceve un errore se non specifica quale lingua deve essere utilizzata. – Jesper

0

Assicurarsi che il valore realmente avuto modo di quella chiamata. Metti un breakpoint direttamente su quella linea e ricontrolla il valore attuale.

Come potete vedere qui il codice http://ideone.com/MBzYn funziona perfettamente.

5

provate ad impostare un Locale quale gli Stati Uniti in questo caso:

SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd yyyy hh:mm aaa",Locale.US); 
format.parse("Thu Jan 19 2012 08:00 PM"); 
Problemi correlati