Skip to content

Commit

Permalink
[Java] Fix okhttp-gson datetime converter compilation (swagger-api#6230)
Browse files Browse the repository at this point in the history
  • Loading branch information
lwander authored and wing328 committed Aug 14, 2017
1 parent d39b1ff commit 23d0fed
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import com.google.gson.stream.JsonWriter;
import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.DateTimeFormatterBuilder;
import org.joda.time.format.ISODateTimeFormat;
{{/joda}}
{{#threetenbp}}
Expand Down Expand Up @@ -132,11 +133,12 @@ public class JSON {
*/
public static class DateTimeTypeAdapter extends TypeAdapter<DateTime> {
private final DateTimeFormatter parseFormatter = ISODateTimeFormat.dateOptionalTimeParser();
private final DateTimeFormatter printFormatter = ISODateTimeFormat.dateTime();
private DateTimeFormatter formatter;
public DateTimeTypeAdapter() {
this(ISODateTimeFormat.dateTime().withOffsetParsed());
this(new DateTimeFormatterBuilder()
.append(ISODateTimeFormat.dateTime().getPrinter(), ISODateTimeFormat.dateOptionalTimeParser().getParser())
.toFormatter());
}

public DateTimeTypeAdapter(DateTimeFormatter formatter) {
Expand All @@ -152,7 +154,7 @@ public class JSON {
if (date == null) {
out.nullValue();
} else {
out.value(printFormatter.print(date));
out.value(formatter.print(date));
}
}

Expand All @@ -164,7 +166,7 @@ public class JSON {
return null;
default:
String date = in.nextString();
return parseFormatter.parseDateTime(date);
return formatter.parseDateTime(date);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import com.google.gson.stream.JsonWriter;
import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.DateTimeFormatterBuilder;
import org.joda.time.format.ISODateTimeFormat;
{{/joda}}
{{#threetenbp}}
Expand Down Expand Up @@ -91,7 +92,9 @@ public class JSON {
private DateTimeFormatter formatter;
public DateTimeTypeAdapter() {
this(ISODateTimeFormat.dateTime().withOffsetParsed());
this(new DateTimeFormatterBuilder()
.append(ISODateTimeFormat.dateTime().getPrinter(), ISODateTimeFormat.dateOptionalTimeParser().getParser())
.toFormatter());
}

public DateTimeTypeAdapter(DateTimeFormatter formatter) {
Expand Down

0 comments on commit 23d0fed

Please sign in to comment.