java - 如何将 Google Appengine DateTime 与 Java 中的 Date 对象进行比较?

标签 java google-app-engine

我是 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());

实体已正确保存 enter image description here

查询提供了 4 个结果

[EntityDate@6780874d, EntityDate@27330551, EntityDate@6a21cf2, EntityDate@7d1a5744]

该类的默认 toString() 有点难看,但它表明查询正确执行。

您能否提供您的 Event 类的源代码以及执行查询的代码的延续?

关于java - 如何将 Google Appengine DateTime 与 Java 中的 Date 对象进行比较?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29492238/

相关文章:

java - 如何测试 Google App Engine 的数据库实现?

google-app-engine - 独特的约束——谷歌应用引擎

java - 在扩展类上添加 'throws' ?

java - USB 设备在打开应用程序后重新连接之前无法使用

java bash命令问题

node.js - 部署到 App Engine 后运行 Sequelize 迁移

google-app-engine - App Engine 上传问题。

java - Bintray Gradle 插件 - POM 不完整,不会同步到 Maven Central?

java - Eclipse 中通知未处理异常的配置是什么?

java - 适用于 Android 的 GWT/GAE 离线应用程序 - 高级