12

I am trying to add a timestamp field in an Android client with Firebase Firestore.

According to the documentation:

Annotation used to mark a Date 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.

But when I try it:

@ServerTimestamp
Date serverTime = null; // I tried both java.util.Date and java.sql.Date

//...

Map<String, Object> msg = new HashMap<>();
// ... more data
msg.put("timestamp", serverTime);

On the Cloud Firestore database this field is always null.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
Melk90
  • 370
  • 1
  • 2
  • 8
  • You're annotating a local variable, not a POJO field. There is a big difference between the two. – Doug Stevenson Jan 27 '18 at 16:50
  • With that anotation you are telling Firestore convertion that when the POJO is used the field with that equivalent name in the Store DB must be transformed from Firestore tiemstamp to date. You have to set the timestamp first. Firestore wont set the Timestamp automatically because Firestore doesn't know when the timestamp should be set, on creation? on update? on an specific field write? Look at the Ali answer – cutiko Jan 27 '18 at 17:15

4 Answers4

25

That is not the correct way of how to add the time and date to a Cloud Firestore database. The best practice is to have a model class in which you can add a date field of type Date together with an annotation. This is how your model class should look like:

import java.util.Date;

public class YourModelClass {
    @ServerTimestamp
    private Date date;

    YourModelClass() {}

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }
}

When you create on object of YourModelClass class, there is no need to set the date. Firebase servers will read your date field, as it is a ServerTimestamp (see the annotation), and it will populate that field with the server timestamp accordingly.

Another approach would be to use FieldValue.serverTimestamp() method like this:

Map<String, Object> map = new HashMap<>();
map.put("date", FieldValue.serverTimestamp());
docRef.update(map).addOnCompleteListener(new OnCompleteListener<Void>() {/* ... */}
Mangesh
  • 5,491
  • 5
  • 48
  • 71
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Is there everything alright? Can I help you with other informations? – Alex Mamo Jan 28 '18 at 13:29
  • Great! Now it's working, Effectively, writing this field in a pojo class does the trick. Thank you. – Melk90 Jan 28 '18 at 16:29
  • I am getting null Logger.info("Date", new YourModelClass().getDate() + ""); – Raj Kumar Feb 22 '18 at 12:59
  • Add a new question that cotains a mcve so me and other user can help you too. – Alex Mamo Feb 22 '18 at 13:01
  • Nothing i have done i just copied above model and printed the date on log Like Log.d("Date",new YourModelClass().getDate()+"") but i am getting null – Raj Kumar Feb 22 '18 at 13:08
  • @RajKumar You say you have done nothing but still not working. That;s why I said to post a new question so we can take a closer look. – Alex Mamo Feb 22 '18 at 13:10
  • @M.Yogeshwaran Please post another fresh question containing a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve), so me and other users can help you. – Alex Mamo Jul 16 '18 at 09:13
  • am using above model class but my date field is always null – M.Yogeshwaran Jul 16 '18 at 09:18
  • @M.Yogeshwaran Without seeing your code, I cannot help you at all. So please add another question. – Alex Mamo Jul 16 '18 at 09:20
  • https://stackoverflow.com/q/51358563/5498065 @AlexMamo please take a look – M.Yogeshwaran Jul 16 '18 at 09:26
  • can you help me @AlexMamo https://stackoverflow.com/questions/61124994/how-to-fetch-documents-from-one-month-old-only-firestore/61125159#61125159 will upvote – SNM Apr 09 '20 at 18:43
6

use FieldValue.serverTimestamp() get server timestamp

Map<String, Object> msg = new HashMap<>();
msg.put("timestamp", FieldValue.serverTimestamp());
Ali Faris
  • 17,754
  • 10
  • 45
  • 70
1

I have similar problem, and I found this at my catlog and solved my problem

firebaseFirestore = FirebaseFirestore.getInstance();
FirebaseFirestoreSettings settings = new FirebaseFirestoreSettings.Builder()
                .setTimestampsInSnapshotsEnabled(true)
                .build();
firebaseFirestore.setFirestoreSettings(settings);
Yogesh Rathi
  • 6,331
  • 4
  • 51
  • 81
RickyS
  • 11
  • 2
0

I had a similar problem,

I was getting the exception ...has type java.sql.Timestamp, got java.util.Date ..., so I just replace the type from Timestamp to Date ( from java.util.Date) and worked fine.

Leonardo Costa
  • 994
  • 11
  • 26