java - Gson omits hours when parsing Joda DateTime object -
i have joda datetime
field in object receive server. example, field value 2016-09-01t11:30:00.000+03:00
. then, when calling gson.tojson()
, field converted date-only string, 2016-09-01
.
code:
final gsonbuilder builder = new gsonbuilder().registertypeadapter(datetime.class, new datetimeserializer()); final gson gson = builder.create(); string str = gson.tojson(response.body());
when debugging noticed custom type adapter's serialize
method not called. maybe i'm not understanding correctly, expecting method used when converting object json string (on other hand, deserialize
called when using gson.fromjson()
on string).
this custom type adapter:
public class datetimeserializer implements jsondeserializer<datetime>, jsonserializer<datetime> { private static final datetimeformatter date_format = isodatetimeformat.date(); @override public datetime deserialize(final jsonelement je, final type type, final jsondeserializationcontext jdc) throws jsonparseexception { final string dateasstring = je.getasstring(); return dateasstring.length() == 0 ? null : date_format.parsedatetime(dateasstring); } @override public jsonelement serialize(final datetime src, final type typeofsrc, final jsonserializationcontext context) { return new jsonprimitive(src == null ? "" : date_format.print(src)); } }
any idea why parsed way , how solve this? thanks!
as stated in comments, using isodatetimeformat.date()
instead of isodatetimeformat.datetime()
.
Comments
Post a Comment