-1

How do I handle time in Firebase?

Me123
  • 129
  • 1
  • 2
  • 13
  • 1
    It sounds like you're looking for this: http://stackoverflow.com/questions/36658833/firebase-servervalue-timestamp-in-java-data-models-objects or this: http://stackoverflow.com/questions/33096128/when-making-a-pojo-in-firebase-can-you-use-servervalue-timestamp or this: http://stackoverflow.com/questions/37864974/how-to-use-the-firebase-server-timestamp-to-generate-date-created/37868163#37868163 (my search to find these: http://stackoverflow.com/search?q=%5Bandroid%5D%5Bandroid%5D+ServerValue.TIMESTAMP) – Frank van Puffelen Feb 02 '17 at 14:26
  • @FrankvanPuffelen Thanks for your reply! I've tried the code [from your answer](http://stackoverflow.com/a/37868163/6853372). Unfortunately, this deletes all messages from the database and I receive the timestamp multiple times. Can you please post a complete answer? I'd like to add a timestamp on messages as described in my question. I've been struggling with this issue for over 20 days as you can see from my previous Firebase question. It's so complicated! :) – Me123 Feb 02 '17 at 22:13
  • @FrankvanPuffelen Thanks for your time. I appreciate it. – Me123 Feb 02 '17 at 22:13
  • @FrankvanPuffelen I have edited my question and added my modification of FriendlyMessage.java - What changes should I make to MainActivity.java to achieve what I want? – Me123 Feb 03 '17 at 15:48
  • @FrankvanPuffelen Because the following give me an error: `FriendlyMessage friendlyMessage = new FriendlyMessage(mMessageEditText.getText().toString(), mUsername, mPhotoUrl, ServerValue.TIMESTAMP);` – Me123 Feb 03 '17 at 16:32

1 Answers1

2

In the model class FriendlyMessage add another field named:

String timeStamp;
public FriendlyMessage(String text, String name, String photoUrl, String timeStamp) {
    this.text = text;
    this.name = name;
    this.photoUrl = photoUrl;
    this.timeStamp = timeStamp;
}

and when calling the constructor pass the timeStamp in the format you like:

mSendButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        FriendlyMessage friendlyMessage = new FriendlyMessage(mMessageEditText.getText().toString(), mUsername,
                mPhotoUrl, /*time stamp in the format you like*/ timeStamp);
        mFirebaseDatabaseReference.child(MESSAGES_CHILD).push().setValue(friendlyMessage);
        mMessageEditText.setText("");
        mFirebaseAnalytics.logEvent(MESSAGE_SENT_EVENT, null);
    }
});
Aman Grover
  • 1,609
  • 1
  • 21
  • 38
  • I already did that, it's more complicated that you think - Feel free to check out the entire project [here](https://github.com/firebase/friendlychat/tree/master/android) and test your suggestion. You'll see that you'll get all kinds of errors that I can't post here because they're too many & long. Thanks for your answer though. – Me123 Feb 02 '17 at 12:24
  • Really? Please post your complete working code then. – Me123 Feb 02 '17 at 12:26
  • you need to delete the old database from the firebase , and then run your code again. Even if I give you my code, I am doing the exact same thing I have written above. – Aman Grover Feb 03 '17 at 05:50