2016-04-18 2 views
1
Parsen

Ich versuche April 18 2016 10:41 AM-04/18/2016 10:41 zu analysieren. Hier ist mein CodeDatumsformat in Android von 18. April 2016 10.41 bis 2016.04.18 10.41

private String dateFormat(String strCurrentDate) throws ParseException { 

     SimpleDateFormat format = new SimpleDateFormat("mmmm dd yyyy hh:mm Z"); 
     Date newDate = null; 
     newDate = format.parse(strCurrentDate); 
     format = new SimpleDateFormat("MM/dd/yyyy hh:mm"); 
     String date = format.format(newDate); 
     Log.d("DATE_FORMATE_TESTING", date); 
     return date; 

    } 

Aber es gibt die folgenden Fehler

04-18 10:41:43.834 7526-7526/gps.clock.com W/System.err: java.text.ParseException: Unparseable date: "April 18 2016 01:41 PM" (at offset 0) 
04-18 10:41:43.834 7526-7526/gps.clock.com W/System.err:  at java.text.DateFormat.parse(DateFormat.java:579) 
04-18 10:41:43.834 7526-7526/gps.clock.com W/System.err:  at gps.clock.com.MainActivity.dateFormat(MainActivity.java:441) 
04-18 10:41:43.834 7526-7526/gps.clock.com W/System.err:  at gps.clock.com.MainActivity.access$500(MainActivity.java:70) 
04-18 10:41:43.834 7526-7526/gps.clock.com W/System.err:  at gps.clock.com.MainActivity$1$4.onClick(MainActivity.java:303) 

Kann mir jemand sagen, wie soll ich es beheben? Warum zeigt es Unparseable date. Wie soll ich es analysieren? Vielen Dank im Voraus.

+0

sind Sie Eibisch mit? –

+1

Sie haben Kleinbuchstaben für den Monat im ersten Muster. Sie sollten in Großbuchstaben geschrieben sein. Und der Charakter für am/pm ist 'a', nicht' Z'. Bitte beachten Sie die [ 'SimpleDateFormat' docs] (http://developer.android.com/reference/java/text/SimpleDateFormat.html) –

+3

Mögliches Duplikat [falscher Konvertierung von String in dem Datumsformat] (http: // Stackoverflow .com/questions/13698165/Inkorrektes Konvertieren von String-in-Datum-Format) –

Antwort

3

Versuchen Sie, den folgenden Code

String strCurrentDate = "April 18 2016 10:41 AM"; 
    SimpleDateFormat format = new SimpleDateFormat("MMM dd yyyy hh:mm a"); 
    Date newDate = null; 
    try { 
     newDate = format.parse(strCurrentDate); 
    } catch (ParseException e) { 
     e.printStackTrace(); 
    } 
    format = new SimpleDateFormat("MM/dd/yyyy hh:mm"); 
    String date = format.format(newDate); 
    System.out.print(date); 
+0

ja das hat bei mir funktioniert. Ich werde diese Antwort akzeptieren. Können Sie mir bitte sagen, was ist der Unterschied zwischen a und Z, die in SimpleDateFormate ("MMM TT yyyy hh: mm a") ist? – anuradha

+0

'a' wird verwendet, um AM oder PM darzustellen, wobei 'Z' (Zulu time) TimeZone darstellt, also konnte es nicht parsen. – user1799171

1

Ich glaube, Sie brauchen Ihr ursprüngliches Format-String zwicken.

SimpleDateFormat format = new SimpleDateFormat("mmmm dd yyyy hh:mm Z");

Sollte

SimpleDateFormat format = new SimpleDateFormat("MMMM dd yyyy hh:mm a");

0

in Java Datum m und M beide unterschiedliche Bedeutung haben Formatierung sein. m verwendet Minuten zu analysieren während M verwendet wird Monate zu analysieren.

In Ihrem Beispiel Sie Parsen Monat mit kleingeschrieben m und verursacht Fehler. Wie von @Mike M. vorgeschlagen, ändern Sie Ihre m s mit Großbuchstaben.

Verwandte Themen