2013-03-19 9 views
5

Ho seguito su questo dilemma alcune altre soluzioni su questo sito e non ho installato Joda Time, ma non riesco ancora a capire perché questo non funziona.Data non analizzabile con DateFormat.parse()

Ho anche provato a rimuovere i due punti, come indicato da una soluzione, ma ciò non ha aiutato.

currentNode.getProperty("jcr:created").getString() = 2013-03-07T11: 57: 08.596-05: 00

ottengo questo errore: java.text.ParseException: Data di analizzarlo: "2013-03-07T11: 57: 08,596 -05: 00"

<%@page import=" 
    java.util.Date, 
    java.text.SimpleDateFormat, 
    java.text.DateFormat" 
%> 
<% 
    DateFormat outputFormat = new SimpleDateFormat("MMMM dd, yyyy"); 
    DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ"); 
    String currentDate = currentNode.getProperty("jcr:created").getString(); 
    Date date = inputFormat.parse(currentDate); // <-- Failing here 
    String currentDateString = outputFormat.format(date); 
%> 
+2

Correlato: http://stackoverflow.com/a/2202300/738746 –

risposta

12

Il fuso orario formattato come Z dovrebbe essere -0500, non -05:00.

Quindi io suggerirei di sostituire

DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ"); 

con

DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX"); 

Vedere SimpleDateFormat's javadoc per ulteriori dettagli sui formati disponibili.

Se il tuo jdk non consente il pattern X, dovrai correggere la stringa di input per rimuovere lo :. Questo può essere fatto con una regex:

currentDate = currentDate.replaceAll(":(\\d\\d)$", "$1") 
+0

java.lang.IllegalArgumentException: carattere modello non valido 'X' –

+0

Qual è il tuo JDK? L'ho provato con Java 7 e funziona perfettamente. –

+6

Il pattern 'X' è nuovo per Java 7. – Perception

0

Anche se questa domanda è stato risposto, non v'è un modo alternativo per raggiungere la soluzione, se la vostra esigenza è solo quello di visualizzare la data di creazione nel formato "MMMM dd, yyyy".

Esiste un metodo getDate() per l'oggetto Property che restituisce un oggetto Calendar, dal quale è possibile ottenere l'oggetto data utilizzando getTime().

Quindi, il codice di cui sopra, funzionerebbe se riscritto come mostrato di seguito.

<%@ page import="java.util.Calendar, 
    java.text.SimpleDateFormat, 
    java.text.DateFormat" %> 
<% 
DateFormat outputFormat = new SimpleDateFormat("MMMM dd, yyyy"); 
Calendar currentDate = currentNode.getProperty("jcr:created").getDate(); 
String currentDateString = outputFormat.format(currentDate.getTime()); %> 

Pertanto, eliminerebbe la necessità di convertire String to Date e quindi eseguire le restanti operazioni. Spero che questo ti aiuti.

1

Dopo aver testato la soluzione da bot ubriaco, vedo che un fuso orario con ore e mezza non funziona, come -0530 (India).

Quindi la risposta migliore è quindi:

DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX"); 

Annotare l'aggiunta XX al fine, ora anche i minuti sono presi in considerazione.

Problemi correlati