使用日期时间或 unix时间戳查询Realm数据库对象的正确方法是什么。 我做了两个查询,它们都不起作用,只返回null:
protected void onTests() {
// READ TV GUIDE info.
RealmResults<ProgramObject> result = app.getDatabase().where(ProgramObject.class).findAll();
ProgramObject programObject = result.where().equalTo("channel_id", 997).equalTo("time_start_unix", 1448563800000L).findFirst();
String title = programObject.getTitle();
Log.e("Print result: ", String.valueOf(title));
}
并且,结果我将得到 NullpointerException ,因为 programObject 是 null 我的理解,这一部分:.equalTo("time_start_unix", 1448563800000L)
没有工作。
我的第二个查询,我在那里传递日期时间字符串&#34; 2015-11-26 22:30:00&#34;:
protected void onTests() {
// READ TV GUIDE info.
RealmResults<ProgramObject> result = app.getDatabase().where(ProgramObject.class).findAll();
ProgramObject programObject = result.where().equalTo("channel_id", 997).equalTo("time_start", "2015-11-26 22:30:00").findFirst();
String title = programObject.getTitle();
Log.e("Print result: ", String.valueOf(title));
}
结果我和以前一样, NullpointerException
我的 ProgramObject.class 对象:
public class ProgramObject extends RealmObject {
@PrimaryKey
private int id;
private int channel_id;
public void setId(int id) {
this.id = id;
}
public void setChannel_id(int channel_id) {
this.channel_id = channel_id;
}
public void setTime_start(String time_start) {
this.time_start = time_start;
}
public void setTime_stop(String time_stop) {
this.time_stop = time_stop;
}
public void setTitle(String title) {
this.title = title;
}
public void setDescription(String description) {
this.description = description;
}
public void setCreated(String created) {
this.created = created;
}
public void setModified(String modified) {
this.modified = modified;
}
public void setCategory1(String category1) {
this.category1 = category1;
}
public void setCategory2(String category2) {
this.category2 = category2;
}
public void setStatus(String status) {
this.status = status;
}
public void setSys_status(String sys_status) {
this.sys_status = sys_status;
}
public void setUrl(String url) {
this.url = url;
}
public int getId() {
return id;
}
public int getChannel_id() {
return channel_id;
}
public String getTime_start() {
return time_start;
}
public String getTime_stop() {
return time_stop;
}
public String getTitle() {
return title;
}
public String getDescription() {
return description;
}
public String getCreated() {
return created;
}
public String getModified() {
return modified;
}
public String getCategory1() {
return category1;
}
public String getCategory2() {
return category2;
}
public String getStatus() {
return status;
}
public String getSys_status() {
return sys_status;
}
public Long getTime_start_unix() {
return time_start_unix;
}
public Long getTime_stop_unix() {
return time_stop_unix;
}
public void setTime_start_unix(Long time_start_unix) {
this.time_start_unix = time_start_unix;
}
public void setTime_stop_unix(Long time_stop_unix) {
this.time_stop_unix = time_stop_unix;
}
public String getUrl() {
return url;
}
private String time_start;
private String time_stop;
private Long time_start_unix;
private Long time_stop_unix;
private String title;
private String description;
private String created;
private String modified;
private String category1;
private String category2;
private String status;
private String sys_status;
private String url;
}
来自我的json片段:
{
"id": "1448400947631",
"channel_id": "914",
"time_start": "2015-12-01 09:35:00",
"time_stop": "2015-12-01 10:00:00",
"unix_start": "0",
"unix_stop": "0",
"title": "Jamie's 15 Minute Meals 1 E20",
"description": "On today’s menu are Spiced chicken, bacon, asparagus and spinach lentils, plus Falafel wraps, grilled veg and salsa.",
"created": "2015-11-26 07:34:33",
"modified": "0000-00-00 00:00:00",
"category1": "",
"category2": "",
"status": "0",
"sys_status": "1",
"url": ""
},