Uscita RFC 3339 Timestamp in Java
Domanda
Voglio produrre un timestamp con un offset PST (ad es., 13-11-2008T13: 23: 30-08: 00). java.util.SimpleDateFormat
non sembra generare offset del fuso orario nel formato ora: minuto , esclude i due punti. C'è un modo semplice per ottenere quel timestamp in Java?
// I want 2008-11-13T12:23:30-08:00
String timestamp = new SimpleDateFormat("yyyy-MM-dd'T'h:m:ssZ").format(new Date());
System.out.println(timestamp);
// prints "2008-11-13T12:23:30-0800" See the difference?
Inoltre, SimpleDateFormat
non può analizzare correttamente l'esempio sopra. Genera un ParseException
.
// Throws a ParseException
new SimpleDateFormat("yyyy-MM-dd'T'h:m:ssZ").parse("2008-11-13T13:23:30-08:00")
Soluzione
A partire da Java 7, c'è la stringa del modello X
per il fuso orario ISO8601. Per le stringhe nel formato che descrivi, usa XXX
. Consulta la documentazione .
Esempio:
System.out.println(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX")
.format(new Date()));
Risultato:
2014-03-31T14:11:29+02:00
Altri suggerimenti
Dai un'occhiata al pacchetto Joda Time . Rendono la formattazione della data RFC 3339 molto più semplice.
Esempio Joda:
DateTime dt = new DateTime(2011,1,2,12,45,0,0, DateTimeZone.UTC);
DateTimeFormatter fmt = ISODateTimeFormat.dateTime();
String outRfc = fmt.print(dt);
Dal reparto "get it done", " una soluzione consiste nell'utilizzare regex per correggere la stringa dopo il completamento di SimpleDateFormat. Qualcosa come s / (\ d {2}) (\ d {2}) $ / $ 1: $ 2 / in Perl.
Se sei anche lontanamente interessato a questo, modificherò questa risposta con il codice Java funzionante.
Ma sì. Sto colpendo anche questo problema. RFC3339, ti sto guardando!
EDIT:
Questo funziona per me
// As a private class member
private SimpleDateFormat rfc3339 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
String toRFC3339(Date d)
{
return rfc3339.format(d).replaceAll("(\\d\\d)(\\d\\d)<*>quot;, "$1:$2");
}
Ho trascorso parecchio tempo a cercare una risposta allo stesso problema e ho trovato qualcosa qui: http://developer.android.com/reference/java/text/SimpleDateFormat.html
Risposta suggerita:
Timestamp stringa = new SimpleDateFormat (" yyyy-MM-dd'T'h: m: ssZZZZZ "). format (new Date ());
Se noti che sto usando 5 'Z' invece di uno. Ciò fornisce l'output con due punti nell'offset in questo modo: "2008-11-13T12: 23: 30-08: 00". Spero che sia d'aiuto.
Il problema è che Z produce l'offset del fuso orario senza due punti (:) come separatore.
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'h:m:ss.SZ");
Non è esattamente ciò di cui hai bisogno?
Ho trovato un PasteBin vagante che mi ha aiutato a risolvere il problema: http://pastebin.com/y3TCAikc
Nel caso in cui i suoi contenuti vengano successivamente cancellati:
// I want 2008-11-13T12:23:30-08:00
String timestamp = new SimpleDateFormat("yyyy-MM-dd'T'h:m:ssZ").format(new Date());
System.out.println(timestamp);
// prints "2008-11-13T12:23:30-0800" See the difference?
// Throws a ParseException
new SimpleDateFormat("yyyy-MM-dd'T'h:m:ssZ").parse("2008-11-13T13:23:30-08:00")
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'h:m:ss.SZ");
Ho creato una InternetDateFormat per RFC3339.
Ma il commento sul codice sorgente è giapponese.
PS: ho creato un po 'di edizione inglese e refactoring.