将 Meteor.js DDP 日期转换为 Java 日期
Cast Meteor.js DDP date to Java Date
我正在使用 Android DDP Client library to connect my Android client to Meteor-JS 服务器。当我收到一个新对象时,我使用以下代码来解析 "createdAt" 字段
private Map<String, Object> fields;
private Date timestamp;
/*...*/
timestamp = (Date) fields.get("createdAt");
我错误地认为它应该是 Date 类型。我收到一条错误消息的异常:
com.google.gson.internal.LinkedHashTreeMap cannot be cast to java.util.Date
如何正确解析Meteor发送的Date?
Meteor-JS 向您发送 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. 我建议将时间戳存储为 long
字段而不是 Date
.
我正在使用 Android DDP Client library to connect my Android client to Meteor-JS 服务器。当我收到一个新对象时,我使用以下代码来解析 "createdAt" 字段
private Map<String, Object> fields;
private Date timestamp;
/*...*/
timestamp = (Date) fields.get("createdAt");
我错误地认为它应该是 Date 类型。我收到一条错误消息的异常:
com.google.gson.internal.LinkedHashTreeMap cannot be cast to java.util.Date
如何正确解析Meteor发送的Date?
Meteor-JS 向您发送 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. 我建议将时间戳存储为 long
字段而不是 Date
.