I use Gson to convert the java.util.Date object to Json, and then convert the Json back to the java.util.Date object:
Date date = new Date(); System.out.println("date=" + date + "; date.getTime()=" + date.getTime()); String json = gson.toJson(date); System.out.println("date in json format=" + json); Date newDate = gson.fromJson(json, Date.class); System.out.println("newDate=" + newDate + "; gettime=" + date.getTime()); if (!newDate.equals(date)) { System.out.println("dates are not the same - bad"); } else System.out.println("dates are the same - good");
2 Date objects should be equal, but as you can see in the output, this is not:
date=Fri Nov 23 12:18:21 EST 2012; date.getTime()=1353691101023 date in json format="Nov 23, 2012 12:18:21 PM" newDate=Fri Nov 23 12:18:21 EST 2012; gettime=1353691101023 dates are not the same - bad
How can Date objects differ when the Javadoc for the Date.equals () method says that "two Date objects are equal if and only if the getTime method returns the same long value for both"? As you can see from the output, both Date objects return the same value for getTime ().
source share