I am using Android DDP Client library to connect my Android client to Meteor-JS server. When I receive a new object I use the following code to parse a "createdAt" field
private Map<String, Object> fields;
private Date timestamp;
/*...*/
timestamp = (Date) fields.get("createdAt");
I wrongfully assumed that it should be of Date type. And I get the exception with an error message:
com.google.gson.internal.LinkedHashTreeMap cannot be cast to java.util.Date
How to parse the Date sent by Meteor correctly?
Meteor-JS sends you a JSON.
// The Date value in the JSON response is a Unix timestamp.
// It gives the number of milliseconds since 1 January 1970 00:00:00 UTC.
// So we can do:
Double jsonDate = ((Map<String, Double>) fields.get("createdAt")).get("$date");
timestamp = new Date(jsonDate.longValue());
ps. I'd recommend to store timestamp as long
field instead of Date
.