2013-07-29 10 views
8

Voglio cambiare il mio formato di data che è comeModifica il formato della data della stringa e impostato in TextView in Android?

String date ="29/07/13"; 

Ma mi sta mostrando l'errore di * Data di analizzarlo: "29/07/2013" (all'offset 2) * Voglio ottenere la data in questo formato 29 lug 2013.

Ecco il mio codice che sto usando per cambiare il formato.

tripDate = (TextView) findViewById(R.id.tripDate); 
    SimpleDateFormat df = new SimpleDateFormat("MMM d, yyyy"); 
      try { 
       oneWayTripDate = df.parse(date); 
      } catch (ParseException e) { 

       e.printStackTrace(); 
      } 
      tripDate.setText(oneWayTripDate.toString()); 

risposta

24

Prova in questo modo:

String date ="29/07/13"; 
SimpleDateFormat input = new SimpleDateFormat("dd/MM/yy"); 
SimpleDateFormat output = new SimpleDateFormat("dd MMM yyyy"); 
try { 
    oneWayTripDate = input.parse(date);     // parse input 
    tripDate.setText(output.format(oneWayTripDate)); // format output 
} catch (ParseException e) { 
    e.printStackTrace(); 
} 

Si tratta di un processo in 2 fasi: è necessario prima analizzare la stringa esistente in un oggetto Date. Quindi è necessario formattare l'oggetto Date in una nuova stringa.

+2

Grazie ha funzionato per me .. – Developer

7

Modificare la stringa di formato per MM/dd/yyyy, mentre parse() e utilizzare dd MMM yyyy mentre format().

Esempio:

String str ="29/07/2013"; 
// parse the String "29/07/2013" to a java.util.Date object 
Date date = new SimpleDateFormat("dd/MM/yyyy").parse(str); 
// format the java.util.Date object to the desired format 
String formattedDate = new SimpleDateFormat("dd MMM yyyy").format(date); 
0
DateFormat df = new SimpleDateFormat("dd/MM/yyyy, HH:mm"); 
String date = df.format(Calendar.getInstance().getTime()); 
Problemi correlati