简体   繁体   中英

Compare two timestamp days

In my application I designed a Spring scheduler that should pick records that has "abcDate" field with tomorrow's timestamp as value.

I have calculated timestamp in my java application as,

Calendar cal = Calendar.getInstance();
            cal.add(Calendar.DAY_OF_YEAR, 1);
            long abcTimeStamp = DateUtils.convertDateTimeToUTCzone(cal.getTimeInMillis()); 

value of this is : 1387348986 ie) 12 / 18 / 13 @ 6:43:06am UTC

In DB I have a record, with timestamp 1387365854 ie) 12 / 18 / 13 @ 11:24:14am UTC.

I am using couch DB map reduce view, and I pass the timestamp value as key to the view.

But since the time is different in both timestamps, I am not able to fetch this record. Is there a way to match only the days in timestamp and filter the records in view itself?

CouchDB's concept of startkey and endkey is super useful here.

It sounds like you've already got a view, but just for the sake of completeness let's assume it looks like this:

{
   "_id": "_design/mydesign",
   "_rev": "1-088646f81629ce559b6d9772c6116113",
   "views": {
       "by_timestamp": {
           "map": "function(doc) { emit(doc.timestamp, doc); }"
       }
   }
}

And you've got a database named 'timestamps' running locally with a handful of documents, as shown by:

$ curl localhost:5984/timestamps/_design/mydesign/_view/by_timestamp

To get a range of documents, add the startkey and endkey query parameters:

$ curl localhost:5984/timestamps/_design/mydesign/_view/by_timestamp\?startkey\=1387348986\&endkey\=1387365854

Now to get those parameters you'll need the timestamp of the start of tomorrow and the end of tomorrow. This answer covers that with the java.util.Calendar API and Joda-Time.

For more on finding many, check out CouchDB's awesome new docs .

Check Moment.js . It is a javascript date library for parsing, validating, manipulating, and formatting dates.

You could use Joda time API . It has more features.

From a calendar you can get the Date with the method getTime(). This date you can use for an object from the above API. Or use milli secs:

...
DateTime dt = new DateTime(millis);
int day = dt.getDayOfYear();
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM