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.

 Answers

40

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);
ref.child("yourNode").updateChildren(map);

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){
    try{
        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
 
millenomi
answered 7 Months ago
77

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

You need to use this:

@Override
protected void onStart() {
super.onStart();
adapter.startListening();
}


@Override
protected void onStop() {
super.onStop();
adapter.stopListening();
}

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

Note:

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
 
msg
answered 7 Months ago
msg
43

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

Change:

private String mobile_phone;

To:

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

http://excel.tips.net/Pages/T002185_Automatically_Converting_to_GMT.html

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. http://www.cpearson.com/excel/TimeZoneAndDaylightTime.aspx 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
 
Sibtain
answered 4 Months ago
62

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() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) { 
String name = dataSnapshot.child("name").getValue().toString(); 
name1.setText(name);
}

Assuming you have this:

Users
  userid
     name: peter
     //etc
Thursday, August 12, 2021
 
BradM
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 :  
Share