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);
}
};
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.