Ricevo una data/ora in json come 2011-10-26T20:29:59-07:00
. Qual è il modo corretto di utilizzare gsonBuilder.setDateFormat
per formattare correttamente questa volta?GsonBuilder setDateFormat per "2011-10-26T20: 29: 59-07: 00"
risposta
Il -07:00
è la notazione Fuso orario ISO 8601. Questo non è supportato da SimpleDateFormat
fino al Java 7. Quindi, se si può upgrade a Java 7, quindi è possibile utilizzare il X
per rappresentare quel fuso orario notazione:
Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ssX").create();
Su Java 6 si avrebbe bisogno di fare un po 'il pattern matching e la sostituzione sulla stringa JSON prima di sostituire il -07:00
parte dalla RFC 822 notazione -0700
in modo da poter utilizzare Z
:
Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").create();
o dal fuso orario generale notazione GMT-07:00
in modo da poter usare z
:
Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ssz").create();
È passato un po 'di tempo da quando è stato pubblicato, ma ci siamo imbattuti durante il tentativo di utilizzare GSON per analizzare una risposta API con questo formato di data. Ho pensato di condividere le 2 piccole funzioni che ho scritto usando regex per trovare tutte le date e cambiarle nel formato che GSON può quindi analizzare.
private static String cleanDateFormat(String json){ // takes in a string of JSON
Pattern regex = Pattern.compile("\\d\\d:\\d\\d:\\d\\d[-\\+]\\d\\d:\\d\\d");
Matcher regexMatcher = regex.matcher(json);
StringBuffer buff = new StringBuffer();
while(regexMatcher.find()){
regexMatcher.appendReplacement(buff, getSubOfMatch(regexMatcher));
}
regexMatcher.appendTail(buff);
return buff.toString();
}
//then pull out the colon.
private static String getSubOfMatch(Matcher matcher){
StringBuilder sb = new StringBuilder(matcher.group(0));
sb.deleteCharAt(sb.length()-3);
return sb.toString();
}
Per Java 8 (non hanno verificato per Java 7), basta usare il modello
yyyy-MM-dd'T'HH:mm:ssXXX
per ottenere esattamente il formato di 2011-10-26T20:29:59-07:00
.
Il modello del fuso orario è da Java Date Time - Custom Date Format Patterns
- X
Zone offset Example: X +09 XX +0930 XXX +09:30 XXX -05:00 XXXX +093045 XXXXX +08:30:45
mio api formato della data risposta è esattamente lo stesso con la vostra, in questo modo:
"weather": {
"temperature": 31,
"time": "2016-06-23T09:28:38+08:00"
}
nel progetto Android, io uso il seguente codice, entrambi funzionano per me.
Gson gson = new GsonBuilder()
.setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
.create();
// or, also work
Gson gson = new GsonBuilder()
.setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ")
.create();
"yyyy-MM-dd'T'HH:mm:ssX"
non funziona in Android, anche se ho ConfigEd uso Java 8 in build.gradle
.
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
come facciamo a sapere il formato della data dovremmo impostare, infatti è possibile trovare la risposta dal codice sorgente di SimpleDateFormat.java
:
* <p>Which produces this output when run in the America/Los_Angeles time zone:
* <pre>
* yyyy-MM-dd 1969-12-31
* yyyy-MM-dd 1970-01-01
* yyyy-MM-dd HH:mm 1969-12-31 16:00
* yyyy-MM-dd HH:mm 1970-01-01 00:00
* yyyy-MM-dd HH:mmZ 1969-12-31 16:00-0800
* yyyy-MM-dd HH:mmZ 1970-01-01 00:00+0000
* yyyy-MM-dd HH:mm:ss.SSSZ 1969-12-31 16:00:00.000-0800
* yyyy-MM-dd HH:mm:ss.SSSZ 1970-01-01 00:00:00.000+0000
* yyyy-MM-dd'T'HH:mm:ss.SSSZ 1969-12-31T16:00:00.000-0800
* yyyy-MM-dd'T'HH:mm:ss.SSSZ 1970-01-01T00:00:00.000+0000
* </pre>
Grazie @ BalusC! è "yyy" e non "yyyy"? – LuxuryMode
Oops, quello era un errore di battitura, dovrebbe infatti essere "yyyy". – BalusC
Mi ha aiutato molto grazie. Vorrei avere più di un upvotes. :) –