2

I have read lots of stackoverflow questions related to

ServerValue.TIMESTAMP

but I can't figure out how to use it in my app.

I need to get the timeStamp of the creation of a post. The timeStamp should be added to the same place as with uid , author etc. of the post.

Code snippet which writes the post the firebase database:

 private void writeNewPost(String userId, String username, String title, String body) {
    // Create new post at /user-posts/$userid/$postid and at
    // /posts/$postid simultaneously
    String key = mDatabase.child("posts").push().getKey();
    Post post = new Post(userId, username, title, body);
    Map<String, Object> postValues = post.toMap();

    Map<String, Object> childUpdates = new HashMap<>();
    childUpdates.put("/posts/" + key, postValues);
    childUpdates.put("/user-posts/" + userId + "/" + key, postValues);

    mDatabase.updateChildren(childUpdates);
}

My Post.java file

@IgnoreExtraProperties
public class Post {

public String uid;
public String author;
public String title;
public String body;
public int starCount = 0;
public Map<String, Boolean> stars = new HashMap<>();
public Long timeStamp;

public Post() {
    // Default constructor required for calls to DataSnapshot.getValue(Post.class)
}

public Post(String uid, String author, String title, String body) {
    this.uid = uid;
    this.author = author;
    this.title = title;
    this.body = body;
}

// [START post_to_map]
@Exclude
public Map<String, Object> toMap() {
    HashMap<String, Object> result = new HashMap<>();
    result.put("uid", uid);
    result.put("author", author);
    result.put("title", title);
    result.put("body", body);
    result.put("starCount", starCount);
    result.put("stars", stars);

    return result;
}
// [END post_to_map]

}

How should I use

ServerValue.TIMESTAMP 

in my code to get the time of creation of post.

Arnav Vohra
  • 387
  • 5
  • 16

3 Answers3

7

ServerValue.TIMESTAMP is simply a placeholder value. When the Firebase server processes an update request and finds ServerValue.TIMESTAMP as a value, it replaces it with the current server clock.

In your writeNewPost() method you could add a line to set the creation time:

Map<String, Object> postValues = post.toMap();
postValues.put("timeStamp", ServerValue.TIMESTAMP);

If you use Post.toMap() only to create posts, and not for updates, you could put a similar statement there instead of in writeNewPost().

Bob Snyder
  • 37,759
  • 6
  • 111
  • 158
  • 1
    I want that the variable timeStamp (in Post.java) should contain the value of post creation. How do I do that – Arnav Vohra Jul 21 '16 at 18:25
0

You can try to add this to your Post.java

//Declaring the timestamp variable

public Map timestamp;


//add it to your constructor
public Post(String uid, String author, String title, String body, Map timestamp) {       
     this.uid = uid;
     this.author = author;
     this.title = title;
     this.body = body;
     this.timestamp = timestamp
}

And finally you can add the timestamp value to your toMap() function the same way you did for the stars HashMap.

Finally were ever you initialise your values for the other variables like Author and etc you can then add this line.

Map<String, String> timestamp = ServerValue.TIMESTAMP;

And also make sure you pass timestamp to your writeNewPost() method.

Tinashe Makuti
  • 131
  • 1
  • 3
0

Create Post Model

You should create your Post.java model class file to include timestamp field as:

public String uid, title, body;
public long timestamp;

public Post();

public Post(String u, String t, String b){
    this.uid = u;
    this.title = t;
    this.body = b;
}

@Exclude
public Map<String, Object> toMap(){
    Map<String, Object> map = new HashMap<>();
    map.put("timestamp", ServerValue.TIMESTAMP);
    map.put("uid", uid);
    map.put("title", title);
    map.put("body", body);
    return map;
}

The default constructor is required to read your Post model form getValue().

Read Post data

To read your post data from as from the path used in your writeNewPost() function:

public void readPost(){
    mDatabase.getReference("/posts").addChildEventListener(new ChildEventListener(){
        public void onChildAdded(DataSnapshot, String previousChildName){
            Post post = snapshot.getValue(Post.class);
            Log.i(TAG, "time:" + post.timestamp);
        }
        // .....
    }
}
ganiular
  • 331
  • 2
  • 8