我在Google App Engine上的实体中有一个datetime属性。当我插入新记录时,我将此属性设置为:
Date date = new Date();
newEntity.setProperty("dateTime", date);
如何使用datetime属性查询今天保存的记录?
感谢。
答案 0 :(得分:2)
对您的实体执行查询,按dateTime
属性进行过滤。
首先计算今天开始的时间,如下所示:
Calendar c = Calendar.getInstance(); // You might wanna get instance based on user's time zone
c.set( Calendar.HOUR_OF_DAY, 0 );
c.set( Calendar.MINUTE, 0 );
c.set( Calendar.SECOND, 0 );
c.set( Calendar.MILLISECOND, 0 );
Date dayStart = c.getTime();
并过滤dateTime >= dayStart
的实体,不知怎的样:
Query q = new Query( "YourEntityName" );
q.setFilter( new FilterPredicate( "dateTime", FilterOperator.GREATER_THAN_OR_EQUAL, dayStart ) );
// And execute the query:
DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
for ( final Entity e : ds.prepare( q ).asIterable() ) {
// Do something with the entity
}