Skip to content

Commit

Permalink
Merge pull request #73 from JanLapp/json-datetime-fix
Browse files Browse the repository at this point in the history
JSONCommunicator: use ZonedDateTime to parse OCPP DateTime (ISO8601)
  • Loading branch information
TVolden authored Jul 19, 2018
2 parents e0f87cc + b2ff35f commit ad6905e
Showing 1 changed file with 4 additions and 12 deletions.
16 changes: 4 additions & 12 deletions ocpp-v1_6/src/main/java/eu/chargetime/ocpp/JSONCommunicator.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
import org.slf4j.LoggerFactory;

import java.lang.reflect.Type;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.ZonedDateTime;
import java.time.format.DateTimeParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;

Expand Down Expand Up @@ -110,16 +110,8 @@ private class CalendarDeserializer implements JsonDeserializer<Calendar> {
public Calendar deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
try {
String dateString = json.getAsJsonPrimitive().getAsString();

hasLongDateFormat = dateString.length() == DATE_FORMAT_WITH_MS_LENGTH;
SimpleDateFormat formatter = new SimpleDateFormat(hasLongDateFormat ? DATE_FORMAT_WITH_MS : DATE_FORMAT);

formatter.setTimeZone(TimeZone.getTimeZone("GMT+00:00"));
Calendar calendar = Calendar.getInstance();
Date date = formatter.parse(dateString);
calendar.setTime(date);
return calendar;
} catch (ParseException e) {
return GregorianCalendar.from(ZonedDateTime.parse(dateString));
} catch (DateTimeParseException e) {
throw new JsonParseException(e);
}
}
Expand Down

0 comments on commit ad6905e

Please sign in to comment.