getting firebase ServerValue.TimeSTAMP in wrong format, how to convert to Date?

Tags: , , ,



I want to get Firebase Server current time, i searched stackoverflow, i found method ServerValue.TIMESTAMP and it’s returning value when i debugged

.sv=timestamp

i am getting above String, but can’t figure out how to convert it to date. I also tried getting it in hashmap, can somebody please help on this.

Note: i haven’t stored date in firebase.

database.child("subscribers").addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    List notes = new ArrayList<>();
                    for (DataSnapshot noteDataSnapshot : dataSnapshot.getChildren()) {
                    String time = String.valueOf(ServerValue.TIMESTAMP);
           }
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

Answer

this psudo code may help you how to add time stamp to firebase and get it from firebase :

ref=FirebaseDatabase.getInstance().getReference().child("timeAdded");
// you can set TimeStamp to this reference like this

String timeAddedLong=ServerValue.TIMESTAMP.toString;

ref.setValue(timeAddedLong);


// get previous time added

ValueEventListner listner=ValueEventListner() {

public onDataChanged(DataSnap snap) {
    if(snap.value!=null) {
     long timeadded=parseToLong(snap.value.tostring)

     }
  ref.removeValueEventListner(this)
 }
}
ref.addValueEventListner(listner);


Source: stackoverflow