2

I used a method to save Time and date to firebase database. I have put the method in Onstart Method. But when I change my system time it takes system time first then get the server time. I want to skip system time.

        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
                Map values = new HashMap();
                values.put("Date", ServerValue.TIMESTAMP);
                ref.child("Current-Date").updateChildren(values);
                Toast.makeText(getApplicationContext(),"Date Saved",Toast.LENGTH_SHORT).show();
            }
        });
    }

    }
  • to be sure you want to save server timestamp to your database not the device time , I'm I right ? – Ali Faris Jan 27 '18 at 06:50
  • yes I want that @ali –  Jan 27 '18 at 07:10
  • checkout the answer – Ali Faris Jan 27 '18 at 07:15
  • 1
    See my answer from this [post](https://stackoverflow.com/questions/43584244/how-to-save-the-current-date-time-when-i-add-new-value-to-firebase-realtime-data/43584581#43584581). – Alex Mamo Jan 27 '18 at 08:37
  • This is the expected behavior for Firebase: when you request the server time, it first fires an event with the local estimate and then another event with the actual server-side value when that comes back. There is no way to prevent this behavior. See my answer here for a longer explanation: https://stackoverflow.com/questions/37864974/how-to-use-the-firebase-server-timestamp-to-generate-date-created/37868163#37868163 – Frank van Puffelen Jan 27 '18 at 15:42

1 Answers1

0

to save server timestamp value use this code

@Override
protected void onStart() {
    super.onStart();

    //save server time to database
    DatabaseReference ref = 
    FirebaseDatabase.getInstance().getReference();
    Map values = new HashMap();
    values.put("time", ServerValue.TIMESTAMP);
    ref.child(Uid).updateChildren(values);
}
Ali Faris
  • 17,754
  • 10
  • 45
  • 70
  • same issue. it first take system tile and thn get the server time. –  Jan 27 '18 at 08:50
  • when i change my system time and click on the button it first get system time then it gets the server time :( –  Jan 27 '18 at 09:02
  • sorry I didn't get you , how you can tell that it takes system time and then get server time ? – Ali Faris Jan 27 '18 at 09:37
  • i can't put the full code . sof don't allow to put the full code cause my code is long. –  Jan 27 '18 at 09:42
  • is there any other way to get the current date GMT+6 ? without system. –  Jan 27 '18 at 09:43