从Parse.com获取当前时间与增加时间之间最多1小时差异的条目

时间:2014-10-17 13:55:42

标签: java android parse-platform

如何在Parse.com数据库上进行查询,只获取最长1小时的条目?

我有这段代码:

@Override
        protected Void doInBackground(Void... params) {

            String currentDateTimeString = DateFormat.getTimeInstance().format(new Date());
            ParseQuery<ParseObject> query = ParseQuery.getQuery("AlertsClass");
            query.orderByDescending("_created_at");
            query.whereGreaterThanOrEqualTo("_created_at", currentDateTimeString);
            try {
                ob = query.find();
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;
}

但我被困住了。有人可以帮我正确地进行这个查询吗?

谢谢!

1 个答案:

答案 0 :(得分:1)

这样的事情应该有效:

Date now = new Date());
long nowMillis = now.getTime();
long oneHourInMills = 3600000;
Date anHourAgo = new Date(nowMillis - oneHourInMillis);
ParseQuery<ParseObject> query = ParseQuery.getQuery("AlertsClass");
query.orderByDescending("createdAt");
query.whereGreaterThanOrEqualTo("createdAt", anHourAgo);