Reputation: 942
According to the official documentation doc I am using FieldValue to set the document field as timestamp value, my question is:
How can I add multiple timestamp value to the same document, like I need to set the startDate and the endDate and docTimpStamp
I am using the following code:
@ServerTimestamp Date time;
and on adding the document:
Map<String, Object> dataMap = new HashMap<>();
dataMap.put("startDate", FieldValue.serverTimestamp());
time.setTime (...); // here change the date to be the endDate
dataMap.put("endDate", FieldValue.serverTimestamp());
time.setTime (...); // here change the date to be the docTimeStamp
dataMap.put("docTimeStamp",FieldValue.serverTimestamp());
and this solution is not working, the data not found getting with the same values and not real time.
How can I implement this process?
Upvotes: 0
Views: 938
Reputation: 83113
I think you are mixing the use of the ServerTimestamp
annotation with the use of the static serverTimestamp()
method
The ServerTimestamp
annotation is "used to mark a timestamp field to be populated with a server timestamp. If a POJO being written contains null for a @ServerTimestamp-annotated field, it will be replaced with a server-generated timestamp."
In your code there is no POJO containing null for the time
Date object.
On the other hand, when you do dataMap.put("endDate", FieldValue.serverTimestamp());
you tell to Firestore to "include a server-generated timestamp" for the value of endDate
.
So it is normal that you find the same time for the three values, as they are written to the database (quasi) simultaneously.
In addition, note that there is no link between (e.g.) the two following lines.
time.setTime (...); // here change the date to be the endDate
dataMap.put("endDate", FieldValue.serverTimestamp());
In the first line you set a new value for time
but time
is not used in the 2nd line. And FieldValue.serverTimestamp()
is not linked to the time
object, as explained above.
So, in conclusion, you will probably have to take the full control on the values you want to write (with time.setTime(...);
as you do) and avoid using the serverTimestamp()
method but use the time object.
Upvotes: 2