Skip to content

Commit 076217a

Browse files
committed
Device tries old and new forms of dates before giving up.
1 parent f1aa34d commit 076217a

1 file changed

Lines changed: 22 additions & 4 deletions

File tree

src/main/java/com/iobeam/api/resource/Device.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import java.io.Serializable;
99
import java.text.ParseException;
10+
import java.util.Calendar;
1011
import java.util.Date;
1112

1213
/**
@@ -132,12 +133,23 @@ public static Device fromJson(final JSONObject json)
132133
throws ParseException {
133134

134135
final long projectId = json.getLong("project_id");
136+
Date created;
137+
final String dateStr = json.getString("created");
138+
try {
139+
created = Util.DATE_FORMAT.parse(dateStr);
140+
} catch (ParseException e) {
141+
try {
142+
final Calendar c = javax.xml.bind.DatatypeConverter.parseDateTime(dateStr);
143+
created = c.getTime();
144+
} catch (IllegalArgumentException e2) {
145+
created = null;
146+
}
147+
}
135148
final Builder builder = new Builder(projectId).
136149
setId(json.getString("device_id")).
137150
setName(json.optString("device_name", null)).
138-
setType(json.optString("device_type", null));
139-
// TODO(rrk) - Fix the backend inconsistency first.
140-
//Date created = Util.RESOURCE_DATE_FORMAT.parse(json.getString("created"));
151+
setType(json.optString("device_type", null)).
152+
setCreated(created);
141153

142154
return builder.build();
143155
}
@@ -159,6 +171,7 @@ public static class Builder {
159171
private String deviceId;
160172
private String deviceName;
161173
private String deviceType;
174+
public Date created;
162175

163176
public Builder(final long projectId) {
164177
this.projectId = projectId;
@@ -179,8 +192,13 @@ public Builder setType(final String type) {
179192
return this;
180193
}
181194

195+
public Builder setCreated(final Date created) {
196+
this.created = created;
197+
return this;
198+
}
199+
182200
public Device build() {
183-
return new Device(projectId, new Spec(deviceId, deviceName, deviceType), null);
201+
return new Device(projectId, new Spec(deviceId, deviceName, deviceType), created);
184202
}
185203
}
186204
}

0 commit comments

Comments
 (0)