2009-04-23 12 views
6

non riesco a capire il motivo per cui queste poche righeimprevisto java eccezione SimpleDateFormat parse

Date submissionT; 
    SimpleDateFormat tempDate = new SimpleDateFormat("EEE MMM d HH:mm:ss z yyyy"); 

    public time_print(String time) { 
     try { 
      submissionT=tempDate.parse(time); 
     } 
     catch (Exception e) {  
      System.out.println(e.toString() + ", " + time); 
     } 

    } 

eccezioni Causa e stampare

java.text.ParseException: Unparseable date: "Tue Mar 31 06:09:00 CEST 2009", Tue Mar 31 06:09:00 CEST 2009 

... mentre il tempo "analizzabile" è compatibile con il formato stringa che ho passato a SimpleDateFormat() .. Qualche idea?

+0

Qual è il tuo "tempo" esattamente? –

+0

Ho stampato con println() .. il tempo è una stringa e contiene "mar 31 mar 06:09:00 CEST 2009 " – Emilio

risposta

19

Si tratta di una questione Locale. Usa:

sdf = SimpleDateFormat("EEE MMM d HH:mm:ss z yyyy", Locale.US); 
+0

Sarebbe bello se il messaggio di eccezione non fosse solo "Data non rimpiazzabile" ma anche la posizione nella stringa in cui non è riuscita ... – golimar

-1

Lavori per me.

public class Main { 

public static void main(String[] args) 
{ 
    time_print("Tue Mar 31 06:09:00 CEST 2009"); 
} 

static Date submissionT; 
static SimpleDateFormat tempDate = new SimpleDateFormat("EEE MMM d HH:mm:ss z yyyy"); 

public static void time_print(String time) { 
    try { 
     submissionT=tempDate.parse(time); 
     System.out.println(submissionT); 
    } 
    catch (Exception e) { 
     System.out.println(e.toString() + ", " + time); 
    } 

} 

}

+5

Funziona solo a causa dell'impostazione locale predefinita corrispondente a quella prevista. – kgiannakakis

+0

abbastanza corretto:) .... –

-1

Il 'z' nel formato rappresenta fuso orario e Java riconosce solo alcuni ID fuso orario di. È possibile estrarre l'elenco dalla classe TimeZone come array di stringhe. CEST non compare nella lista ho appena generato dal JDK 1,5

String[] aZones = TimeZone.getAvailableIDs(); 
    for (int i = 0; i < aZones.length; i++) { 
     String string = aZones[i]; 
     System.out.println(string); 
    } 

Spero che questo aiuti.

Problemi correlati