Lingzhi Zhang
Lingzhi Zhang

Reputation: 893

how to write a query get Firestore current server time using firebase java admin sdk

I want to write a query to get a list of document which was saved in the past 24 hours. When the document was created, I add a "last updated" value using FieldValue.serverTimestamp(). Now my query would be like:

Date pastDay = (getFireStoreServerTime) - (24hours)
Query query = collectionRef.orderBy("lastUpdated", Query.Direction.DESCENDING).where("last updated" > pastDay);

However, I wasn't able to find the API in java admin SDK to get the server time. Is this supported in Firebase Admin SDK for Cloud FireStore?

Upvotes: 1

Views: 1199

Answers (1)

Sebastian Schmidt
Sebastian Schmidt

Reputation: 494

Firestore doesn't have an API that just exposes the server time. The Server SDKs do expose a "read time" however, which you could (ab)use to fetch the current time of the backend.

DocumentSnapshot doc = firestore.doc("coll/nonexistingdocument").get();
Instant instant = doc.getReadTime();
Date pastDay = (instant - 24hours);

You can do the same thing with a QuerySnapshot, but that is a more expensive operation.

Upvotes: 2

Related Questions