title: Common time for Firebase Database
body: <p>I am creating a chatting application for android. I am using Firebase Real time database for this purpose. This is how "chats" branch of database looks like :<a href="https://i.stack.imgur.com/l27UW.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/l27UW.png" alt="enter image description here"></a></p>
  
  <p>There are unique ID's for chat rooms generated using Users unique ID's such as "513","675" etc. Inside theese chatrooms there are message objects which also have unique ID's and inside them they store information of the date message sent, name of the sender, and the text of the message. Constructor of Message object is as follows : </p>
  
  <pre><code>public Message(String text,String senderUID, Long date){
          this.text = text;
          this.senderUID = senderUID;
          this.date = date;
      }
  </code></pre>
  
  <p>This is how I generate Time for the each message and send them to firebase database.</p>
  
  <pre><code>sendButton.setOnClickListener(new View.OnClickListener() {
  
              @Override
              public void onClick(View v) {
                  calendar = Calendar.getInstance();
                  String second,hour,minute;
                  String time;
                  if(calendar.get(Calendar.SECOND)&lt;10){
                      second = "0"+calendar.get(Calendar.SECOND);
                  }
                  else
                  {
                      second = ""+calendar.get(Calendar.SECOND);
                  }
  
                  if(calendar.get(Calendar.MINUTE)&lt;10){
                      minute = "0"+calendar.get(Calendar.MINUTE);
  
                  }
                  else
                  {
                      minute = ""+calendar.get(Calendar.MINUTE);
                  }
  
                  if(calendar.get(Calendar.HOUR)&lt;10){
                      hour = "0"+calendar.get(Calendar.HOUR);
                  }
                  else
                  {
                      hour = ""+calendar.get(Calendar.HOUR);
                  }
  
                  time = date + hour + minute + second;
                  Log.d("time",time);
                  Message message = new Message(messageEditText.getText().toString(), user.getDisplayName(), Long.valueOf(time));
                  chatRoomDatabaseRef.child(chatID).child(user.getUid() + generateRandomNumber()).setValue(message);
                  messageEditText.setText("");
              }
          });
  </code></pre>
  
  <p>Here is how I get the data from database with value event listener : </p>
  
  <pre><code>chatRoomDatabaseRef.child(chatID).addValueEventListener(new ValueEventListener() {
  
              @Override
              public void onDataChange(DataSnapshot dataSnapshot) {
  
                  Set&lt;Message&gt; set = new HashSet&lt;Message&gt;();
                  for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                      Message message = snapshot.getValue(Message.class);
                      set.add(message);
                  }
                  messageList.clear();
                  messageList.addAll(set);
  
                  Collections.sort(messageList, new Comparator&lt;Message&gt;() {
                      @Override
                      public int compare(Message o1, Message o2) {
                          return Long.valueOf(o1.date).compareTo(Long.valueOf(o2.date));
                      }
                  });
  
                  messageAdapter.notifyDataSetChanged();
                  messageListView.setSelection(messageAdapter.getCount() - 1);
              }
  
              @Override
              public void onCancelled(DatabaseError databaseError) {
  
              }
          });
  </code></pre>
  
  <p>After I get the data from Firebase database I order them according to their date attribute and list them. Everything works fine but when I am filling messages' date attribute, it fills according to the local time on the phone because of that I can't sort the messages correctly. Time can differ device to device. I need to use a Time which is common and same for all the devices using my app. But I couldn't find a way. </p>
  
  <p>Edit:
  I still couldn't figure out but as a quick solution I created an object called sequence number in the database. I added one more attribute to the message constructor called sequence number. I read the sequence number from the database, give that number to the next message and increase the value in the database for the new messages. Then I order messages according to that number. It is not the best way to do that but it is something until I find a better way.</p>