2013-03-12 7 views
5

sto cercando di convertire da stringa localdate (JODA TIME), ma la sua dandomi erroreString all'errore data locale

String theDate = w.getPSDate(); == 6/03/2013 
LocalDate ld = new LocalDate(theDate); 
System.out.println(ld); 

per qualche ragione, devo usare stringa invece che la data. Voglio stampare la data come (06/03/2013). qual è l'errore nel codice?

errore

Exception in thread "main" java.lang.IllegalArgumentException: Invalid format: "06/03/2013" is malformed at "/03/2013" 
at org.joda.time.format.DateTimeFormatter.parseMillis(DateTimeFormatter.java:747) 
at org.joda.time.convert.StringConverter.getPartialValues(StringConverter.java:87) 
at org.joda.time.LocalDate.<init>(LocalDate.java:406) 
at org.joda.time.LocalDate.<init>(LocalDate.java:354) 
at Date.GetDate.main(GetDate.java:94) 

Java Risultato: 1

risposta

7

Utilizzare un DateTimeFormatter invece:

// Are you sure it's 6/03/2013 rather than 06/03/2013? dd would be nicer... 
DateTimeFormatter formatter = DateTimeFormat.forPattern("d/MM/yyyy"); 
LocalDate date = formatter.parseLocalDate(text); 
+0

se stampare la data viene fuori come 2013/03/06, invece di 06/03/2013. è il 06/03/2013. – vijay

+0

@vijay: la tua domanda è stata rivolta alla conversione * da * 'String' * a *' LocalDate'. Per convertire nell'altra direzione, puoi usare lo stesso formattatore con il suo metodo 'print'. –