Customizing date format in Java jersey2 client

This commit is contained in:
xhh
2015-11-09 11:58:41 +08:00
parent a45af48e35
commit cbc18c97ba
5 changed files with 113 additions and 8 deletions

View File

@@ -60,13 +60,15 @@ public class ApiClient {
private DateFormat dateFormat;
public ApiClient() {
// Use ISO 8601 format for date and datetime.
// See https://en.wikipedia.org/wiki/ISO_8601
this.dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
// Use RFC3339 format for date and datetime.
// See http://xml2rfc.ietf.org/public/rfc/html/rfc3339.html#anchor14
this.dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
// Use UTC as the default time zone.
this.dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
this.json.setDateFormat((DateFormat) dateFormat.clone());
// Set default User-Agent.
setUserAgent("Java-Swagger");
@@ -81,6 +83,13 @@ public class ApiClient {
authentications = Collections.unmodifiableMap(authentications);
}
/**
* Gets the JSON instance to do JSON serialization and deserialization.
*/
public JSON getJSON() {
return json;
}
public String getBasePath() {
return basePath;
}
@@ -235,6 +244,8 @@ public class ApiClient {
*/
public ApiClient setDateFormat(DateFormat dateFormat) {
this.dateFormat = dateFormat;
// also set the date format for model (de)serialization with Date properties
this.json.setDateFormat((DateFormat) dateFormat.clone());
return this;
}