Lyla Lyla - 5 months ago 109
Android Question

When making a POJO in Firebase, can you use ServerValue.TIMESTAMP?

When you're making a Plain Old Java Object that's meant to be serialized from and deserialized to Firebase, is there a way to use the


For example, let's say I want to have an object where one of the properties is the last time it was edited and you'd like to use the

In the POJO class, you might have:

private String timeLastChanged;


private Map<String, String> timeLastChanged;

In the first example with the
, I run into the issue of setting
timeLastChange = ServerValue.TIMESTAMP;
, because
is a Map.

In the second example with the
Map<String, String>
I get a "failed to debounce" error because it can't properly deserialize the long stored in the database into a
Map<String, String>
. Is there any work around for this?


I finally came up with a felxible solution for working with Dates and ServerValue.TIMESTAMP. This is working off of examples from Ivan V, Ossama, and puf.

I couldn't figure out a way to deal with the conversion between long and HashMap<String, String>, but if you nest the property in a more generic HashMap<String, Object> it can go into the database as either a single long value ("date", "1443765561874") or as the ServerValue.TIMESTAMP hash map ("date", {".sv", "servertime"}). Then when you pull it out, it will always be a HashMap with ("date", "some long number"). You can then create a helper method in your POJO class using the @JsonIgnore annotation to easily get the long value from the returned HashMap to use in your app.

Full example of a POJO class is below:

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.firebase.client.ServerValue;

import java.util.HashMap;
import java.util.Map;

public class ExampleObject {
    private String name;
    private String owner;
    private HashMap<String, Object> dateCreated;
    private HashMap<String, Object> dateLastChanged;

     * Required public constructor
    public ExampleObject() {

    public ExampleObject(String name, String owner, HashMap<String,Object> dateCreated) { = name;
        this.owner = owner;
        this.dateCreated = dateCreated;

        //Date last changed will always be set to ServerValue.TIMESTAMP
        HashMap<String, Object> dateLastChangedObj = new HashMap<String, Object>();
        dateLastChangedObj.put("date", ServerValue.TIMESTAMP);
        this.dateLastChanged = dateLastChangedObj;

    public String getName() {
        return name;

    public String getOwner() {
        return owner;

    public HashMap<String, Object> getDateLastChanged() {
        return dateLastChanged;

    public HashMap<String, Object> getDateCreated() {
      //If there is a dateCreated object already, then return that
        if (dateCreated != null) {
            return dateCreated;
        //Otherwise make a new object set to ServerValue.TIMESTAMP
        HashMap<String, Object> dateCreatedObj = new HashMap<String, Object>();
        dateCreatedObj.put("date", ServerValue.TIMESTAMP);
        return dateCreatedObj;

// Use the method described in
// to get the long values from the date object.
    public long getDateLastChangedLong() {

        return (long)dateLastChanged.get("date");

    public long getDateCreatedLong() {
        return (long)dateCreated.get("date");