user2532297
user2532297

Reputation: 81

Google App Engine query filter by date

How exactly does GAE filter query results by date?

I'm trying to pass a datestamp through the query but I can't get it to work.

In the Google Dashboard Datastore viewer the date field is stored as a gd:when type with the format: YYYY-MM-DD HH:MM:SS but when displayed on a webpage displays as Mon Jul 15 20:15:35 UTC 2013.

I request the string Mon Jul 15 20:15:35 UTC 2013 from a jsp page and parse it but the filter isn't working.

    String strDatestamp = req.getParameter("stamp");
    String FormatString = "EEE MMM dd HH:mm:ss z yyyy";
    Date datestamp = null;
    try {
        datestamp = new SimpleDateFormat(FormatString).parse(strDatestamp);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    Filter filter = new FilterPredicate
                           ("date", Query.FilterOperator.EQUAL, datestamp);
    Query query = new Query("Example", key).setFilter(filter);

Upvotes: 2

Views: 1755

Answers (1)

Peter Knego
Peter Knego

Reputation: 80340

Date in computer systems is actually represented as time, in particular case it's internally represented as unix time in milliseconds. See for yourself: date.getTime().

What you see is only human-readable representation, which lacks full millisecond precision.

To query for a particular second you will need to query for a particular time range within one second. So you will need to do range query between Mon Jul 15 20:15:35 UTC 2013 and Mon Jul 15 20:15:36 UTC 2013 (note the second change):

Filter startFilter = new FilterPredicate("date", Query.FilterOperator.GREATER_THAN_OR_EQUAL, startSecond);
Filter endFilter = new FilterPredicate("date", Query.FilterOperator.LESS_THAN, nextSecond);

Upvotes: 1

Related Questions