2012-03-30 17 views
47

Sto cercando di analizzare una stringa in un campo data in un'applicazione Android ma non riesco a trovarlo corretto. Ecco la stringa che sto cercando di convertire in una data "26/03/2012 11:49:00 AM". La funzione che sto utilizzando è:Converti stringa in data in java

private Date ConvertToDate(String dateString){ 
    SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss aa"); 
    Date convertedDate = new Date(); 
    try { 
     convertedDate = dateFormat.parse(dateString); 
    } catch (ParseException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    return convertedDate; 
} 

Ma io continuo a ricevere 3/1/112 11:49 come il risultato .. Qualsiasi aiuto Vorrei davvero apprezzare. grazie

+2

Dove stai vedendo "1/1/112 11:49 AM"? Il valore restituito è una data, non una stringa, quindi devi fare * qualcosa * per vederlo come risultato di una stringa ... –

+0

Vedo lunedì 26 marzo 11:49:00 IST come output. –

+0

prova a impostare l'analisi su indulgente usando dateFormat.setLenient (true), quindi controlla i risultati del tuo parsing – manub

risposta

103

Sei sbagliato nel modo in cui si visualizzano i dati immagino, perché per me:

String dateString = "03/26/2012 11:49:00 AM"; 
    SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss aa"); 
    Date convertedDate = new Date(); 
    try { 
     convertedDate = dateFormat.parse(dateString); 
    } catch (ParseException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    System.out.println(convertedDate); 

Stampe:

Mon Mar 26 11:49:00 EEST 2012 
+0

tutti avevano ragione. stava convertendo correttamente ma stavo usando Date.getYear(), getMonth() e tutti quelli che stavo usando in modo errato. Grazie per tutto il tuo aiuto. – bflosabre91

4
String str_date="13-09-2011"; 
DateFormat formatter ; 
Date date ; 
formatter = new SimpleDateFormat("dd-MM-yyyy"); 
date = (Date)formatter.parse(str_date); 
System.out.println("Today is " +date.getTime()); 

Prova questa

17

è andata OK quando ho usato il parametro Locale.US in SimpleDateFormat

String dateString = "15 May 2013 17:38:34 +0300"; 
System.out.println(dateString); 

SimpleDateFormat dateFormat = new SimpleDateFormat("dd MMM yyyy HH:mm:ss Z", Locale.US); 
DateFormat targetFormat = new SimpleDateFormat("dd MMM yyyy HH:mm", Locale.getDefault()); 
String formattedDate = null; 
Date convertedDate = new Date(); 
try { 
    convertedDate = dateFormat.parse(dateString); 
System.out.println(dateString); 
formattedDate = targetFormat.format(convertedDate); 
} catch (ParseException e) { 
// TODO Auto-generated catch block 
e.printStackTrace(); 
} 
System.out.println(convertedDate); 
2

Questo codice ti aiuterà a ottenere un risultato come il 17 FEBBRAIO 20:49.

String myTimestamp="2014/02/17 20:49"; 

    SimpleDateFormat form = new SimpleDateFormat("yyyy/MM/dd HH:mm"); 
    Date date = null; 
    Date time = null; 
    try 
    { 
     date = form.parse(myTimestamp); 
     time = new Date(myTimestamp); 
     SimpleDateFormat postFormater = new SimpleDateFormat("MMM dd"); 
     SimpleDateFormat sdf = new SimpleDateFormat("HH:mm"); 
     String newDateStr = postFormater.format(date).toUpperCase(); 
     String newTimeStr = sdf.format(time); 
     System.out.println("Date : "+newDateStr); 
     System.out.println("Time : "+newTimeStr); 
    } 
    catch (Exception e) 
    { 
     e.printStackTrace(); 
    } 

Risultato:

Data: Feb 17

Time: 20:49

0
GregorianCalendar date; 

CharSequence dateForMart = android.text.format.DateFormat.format("yyyy-MM-dd", date); 

Toast.makeText(LogmeanActivity.this,dateForMart,Toast.LENGTH_LONG).show(); 
1
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy"); 
String dateInString = "07/06/2013"; 

try { 

    Date date = formatter.parse(dateInString); 
    System.out.println(date); 
    System.out.println(formatter.format(date)); 

} catch (ParseException e) { 
    e.printStackTrace(); 
} 

uscita:

2014/08/06 16:06:54 
2014/08/06 16:06:54