我是 Google Appengine 的新手。在我的应用程序中,数据存储实体存储 java.util.Date 对象。我想查询数据存储以返回特定时间之前的所有实体。我的代码是:
Date date = new Date();
Query<Event> query = ofy().load().type(Event.class).order("date");
query = query.filter("date <", date);
执行后出现错误:无效的日期/时间格式:Sat Apr 04 00:40:22 IST 2015
如果该格式无效,我需要使用哪种格式来查询?
最佳答案
这是一个简单的代码
import java.util.Date;
import com.googlecode.objectify.annotation.Entity;
import com.googlecode.objectify.annotation.Id;
import com.googlecode.objectify.annotation.Index;
@Entity
public class EntityDate {
public EntityDate() {
// Objectify needed
}
@Id
private Long id;
@Index
public Date date;
}
这里是进行查询的代码
Date date = new Date();
Objectify ofy = ObjectifyService.ofy();
ObjectifyService.register(EntityDate.class);
EntityDate entityDate = new EntityDate();
entityDate.date = date;
ofy.save().entities(entityDate);
Query<EntityDate> ofyQuery = ofy.load().type(EntityDate.class).order("date");
ofyQuery = ofyQuery.filter("date <", date);
List<EntityDate> list = ofyQuery.list();
Logger.getLogger("EntityDate").info(list.toString());
实体已正确保存
查询提供了 4 个结果
[EntityDate@6780874d, EntityDate@27330551, EntityDate@6a21cf2, EntityDate@7d1a5744]
该类的默认 toString()
有点难看,但它表明查询正确执行。
您能否提供您的 Event
类的源代码以及执行查询的代码的延续?
关于java - 如何将 Google Appengine DateTime 与 Java 中的 Date 对象进行比较?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29492238/