0

I am currently developing a chat app, and whenever I store data to Firestore, using the FieldValue.serverTimestamp() the date timestamp is saved to the database under the dateCreated field. The only issue I have is that whenever the sent button is clicked I receive an error in my adapter saying the date is null but double checking the database the timestamp is still there.

Alternatively, I can create my own Date locally using the Calendar instance, format it then save it to the Message object which is then saved to Firestore(reference code below) but the issue with this is that I can manually set the Date/Time settings on my phone and the Calendar instance uses the same time which if set wrong by other users the wrong date would then be reflected on the Chat fragment view/UI. I know most people wouldn't want to set a wrong date or time on their phone, but is there any way I can prevent this and ensure that all dates stored on the database are from updated sources, exactly after the data is stored on the database, not from the setting on the phone?

The FieldValue.serverTimestamp() option would give me the accurate date but again I receive an error with it on my chat view adapters(for the user sending messages). The chat fragment is listening for messages added to the database and whenever I send chat messages the app crashes. The receiver though(app on another phone)works fine, renders the new message on the chat fragment with the proper date as well. After looking at the logs, the error message says that the "date must not be null". But looking at the database, the date is set.

This is the Calendar option, works fine but then again users can set date manually on their phone settings and a wrong date could be reflected on the database.

    String saveCurrentDate;
    Calendar calForDate = Calendar.getInstance();

    SimpleDateFormat currentDate = new SimpleDateFormat("MM-dd-yy-hh:mm:ssa");
    saveCurrentDate = currentDate.format(calForDate.getTime());

    ChatMessage message = new ChatMessage();
    message.message = inputMessage.getText().toString();
    message.dateCreated = saveCurrentDate;

Below are some codes from the Adapter:

    private String getReadableDateTime(Date date) { 
    return new SimpleDateFormat("MMMM dd, yyyy - hh:mm a", 
    Locale.getDefault()).format(date); } 

    void setData(ChatMessage chatMessage)
    textMessage.setText(chatMessage.message);
textDateTime.setText(getReadableDateTime(chatMessage.dateCreated));

These are the code for the listener in the Chat fragment:

    public final EventListener<QuerySnapshot> eventListener = 
    (value, error) -> {
    if (error != null) {
        return;
    }
    if (value != null) {
        pb.setVisibility(View.VISIBLE);
        int count = chatMessages.size();
        for (DocumentChange documentChange : value.getDocumentChanges()) {
            if (documentChange.getType() == DocumentChange.Type.ADDED) {
                ChatMessage chatMessage = new ChatMessage();
                chatMessage.senderId = documentChange.getDocument().getString(Constants.KEY_SENDER_ID);
                chatMessage.receiverId = documentChange.getDocument().getString(Constants.KEY_RECEIVER_ID);
                chatMessage.message = documentChange.getDocument().getString(Constants.KEY_MESSAGE);
                chatMessage.dateCreated = documentChange.getDocument().getDate("dateCreated");
                chatMessages.add(chatMessage);
                chatAdapter.notifyItemInserted(chatMessages.size()-1);
            }
        }
        chatRec.setVisibility(View.VISIBLE);
        pb.setVisibility(View.GONE);
    } else {
        pb.setVisibility(View.GONE);
    }
 };
5
  • Use an SNTP client library, or TrustedTime, to get the time. Commented Apr 15 at 15:17
  • FieldValue.serverTimestamp() does use a correct value determined by Firestore servers and doesn't depend on the time set on the user's device. If you're saying that it's not actually the correct time, then please edit the question to show code and explain what exactly isn't working the way you expect. We should be able to copy what you show and observe the same result. It's likely that you're doing something wrong, but we can't see what that is. Commented Apr 15 at 16:45
  • Hi Doug the serverTimestamp seem to work but I have an error with it on my chat view adapters(for the user sending messages). The chat fragment is listening for messages added to the database and whenever I send chat messages the app crashes. The receiver though(app on another phone)works fine, renders the new message on the chat fragment with the proper date as well. After looking at the logs, the error message says that the "date must not be null". But looking at the database, the date is set.
    – Me Compu
    Commented Apr 16 at 5:50
  • I have the ff. codes in my adapter private String getReadableDateTime(Date date) { return new SimpleDateFormat("MMMM dd, yyyy - hh:mm a", Locale.getDefault()).format(date); } void setData(ChatMessage chatMessage){ textMessage.setText(chatMessage.message); textDateTime.setText(getReadableDateTime(chatMessage.dateCreated)); }
    – Me Compu
    Commented Apr 16 at 5:55
  • Please edit the question to share relevant details about what isn't working the way you expect and format that to make it easy to read. Don't bury all that in comments. Commented Apr 16 at 12:42

2 Answers 2

0

You could (1) use a [cloud function on firebase](https://firebase.google.com/docs/functions) to update the data instead, using the server time directly. Or (2) use the [TrustedTime API](https://android-developers.googleblog.com/2025/02/trustedtime-api-introducing-reliable-approach-to-time-keeping-for-apps.html).

0

The Firestore Timestamp is always set on the Firebase servers and is independent of any time set on the user's device. So there is no connection between them. If you want to be sure, you can set a Timestamp using a Cloud Function, as also @NickFelker mentioned in his comment, or you can use the newly added TrustedTime API. For more info, please check the following blog post:

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.