Asked  7 Months ago    Answers:  5   Viewed   105 times

I want to save the current date/time in specific field when I add new value to Firebase Realtime Database via control panel.

How can I achieve that?

Please help me.



The best practice is to save your data as a TIMESTAMP like this ServerValue.TIMESTAMP.

DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
Map map = new HashMap();
map.put("timestamp", ServerValue.TIMESTAMP);

Also remember, when you set the TIMESTAMP, you set it as a Map, but when you retrieve it, you retrieve it as a Long. To get the data back, i suggest you use this method:

public static String getTimeDate(long timestamp){
        DateFormat dateFormat = getDateTimeInstance();
        Date netDate = (new Date(timestamp));
        return dateFormat.format(netDate);
    } catch(Exception e) {
        return "date";

Edit: The model class should look like this:

public class YourModelClass {
    //private fields
    private Map<String, String> timestamp;

    public YourModelClass() {}

    //public setters and getters for the fields

    public void setTimestamp(Map<String, String> timeStamp) {this.timestamp= timestamp;}
    public Map<String, String> getTimestamp() {return timestamp;}

Remember, ServerValue.TIMESTAMP is just a token that Firebase Realtime Database converts to a number on server side when it's used as a child value during write operation. The date only appears in the database after the write operation completes.

To get the timestamp, there is also another approach, which would be to write a frunction in Cloud Functions for Firebase and it will be as easy as:

exports.currentTime = functions.https.onRequest((req, res) => {
    res.send({"timestamp":new Date().getTime()})

You can host this in Cloud Function and get the server timestamp without user interaction.

Tuesday, June 1, 2021
answered 7 Months ago

To let the FirebaseRecyclerAdapter and FirebaseListAdapter show the data on the activity

You need to use this:

protected void onStart() {

protected void onStop() {

Since FirebaseListAdapter uses a listener to check for changes in the firebase database, then to being listening for data you need to add adapter.startListening() inside the onStart() to be able to show the data in the listview.

Then inside onStop() (when activity is stopped), you can use adapter.stopListening() to remove the listener and the data in the adapter.

Check this for more info: Adapter LifeCycle


If after using the above, you get a nullpointexception or cannot resolve symbol, you have to declare adapter as global variable and please check the below answer: Error in startListening()

Tuesday, June 1, 2021
answered 7 Months ago

The problem is that you are creating the property "mobile_phone" as a String and on Firebase it is a Long type.


private String mobile_phone;


private Long mobile_phone;
Thursday, June 24, 2021
answered 6 Months ago

There is a macro on that page with a LocalTimeToUTC method. Looks like it would do the trick. Also some formula examples if you wanted to go that route.

Edit - Another link. This page has several methods for date/time. Pick your poison. Either should do the trick, but I feel like the second is prettier. ;)

Monday, August 2, 2021
answered 4 Months ago

First get uid of the current user that is logged in:

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
String userid = user.getUid();

then retrieve the data of the current user:

DatabaseReference ref = FirebaseDatabase.getInstance().getReference("Users");
ref.child(userid).addListenerForSingleValueEvent(new ValueEventListener() {
public void onDataChange(DataSnapshot dataSnapshot) { 
String name = dataSnapshot.child("name").getValue().toString(); 

Assuming you have this:

     name: peter
Thursday, August 12, 2021
answered 4 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :