When using the GSON
library for JSON
manipulation, I noticed that when parsers are done the date is being changed, it's tiny, but it's enough to break my unit test with JUnit
. >
Follow a sketch code to show the strange behavior.
Gson gson = new Gson();
Date data = new Date();
String dataNoFormatoJSON = gson.toJson(data);
Date dataDoJSON = gson.fromJson(dataNoFormatoJSON, Date.class);
long dataTime = data.getTime();
long dataDoJSONTime = dataDoJSON.getTime();
System.out.println(data + " - " + dataTime);
System.out.println(dataDoJSON + " - " + dataDoJSONTime);
Output from this code:
Tue Oct 17 17:02:03 GFT 2017 - 1508270523483
Tue Oct 17 17:02:03 GFT 2017 - 1508270523000
First the toString()
of Date
and then the date.getTime()
which is the representation of that date in long
.
Notice that toString
, apparently does not show any change, since date.getTime
is different.
first (before parse JSON) 1508270523483
After (after parse JSON) 1508270523000