Firebase Servervalue.Timestamp in Java Data Models Objects

Firebase ServerValue.TIMESTAMP in Java data models objects

This sounds similar to this question: When making a POJO in Firebase, can you use ServerValue.TIMESTAMP?

When creating POJOs used to store/retrieve data apart from the default empty constructor I usually use a constructor similar to this:

Param param1;
Param param2;
HashMap<String, Object> timestampCreated;

//required empty constructor
public DataObject(){}

public DataObject(Param param1, Param param2) {
this.param1 = param1;
this.param2 = param2;
HashMap<String, Object> timestampNow = new HashMap<>();
timestampNow.put("timestamp", ServerValue.TIMESTAMP);
this.timestampCreated = timestampNow;
}

Be sure to include a getter for the HashMap<> used to store the Timestamp:

public HashMap<String, Object> getTimestampCreated(){
return timestampCreated;
}

Then use the @Exclude annotation to create a getter that you can use in your code to get the value of the timestamp if you need it. The @Exclude annotation will cause Firebase to ignore this getter and not look for a corresponding property

@Exclude
public long getTimestampCreatedLong(){
return (long)timestampCreated.get("timestamp");
}

Firebase ref.setValue(ServerValue.TIMESTAMP) setting a huge value (e.g. 1570654658313)

The ServerValue.TIMESTAMP writes a UNIX-style timestamp, which is the number of milliseconds that have passed since the epoch. The value you gave corresponds to "Wed Oct 09 2019 13:57:38 GMT-0700 (Pacific Daylight Time)" after converting it to a date.

A quick way to validate that is to convert the number back to a date in JavaScript:

console.log(new Date(1570654658313))

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

Update 12/27/2016

Switched out @JsonIgnore for @Exclude as many have mentioned.


I finally came up with a flexible 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 @Exclude annotation (meaning Firebase will ignore it and not treat it as a method for serializing to/from the database) 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.google.firebase.database.Exclude;
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) {
this.name = 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 https://stackoverflow.com/questions/25500138/android-chat-crashes-on-datasnapshot-getvalue-for-timestamp/25512747#25512747
// to get the long values from the date object.
@Exclude
public long getDateLastChangedLong() {

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

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

}

Firebase Android ServerValue.Timestamp Map to Long

after stuck for 14 hours, i finally came up with solution.

i inspired from here

Since the source don't have any explanation, i will rewrite it here according to my case with some explanation.

First i remove all my serverValue.timeStamp outside my class, i also remove it from my constructor.

then i add this variable member Long creationDate;

and add the public getter so it can write the timestamp to the DB:

 public java.util.Map<String, String> getCreationDate() {
return ServerValue.TIMESTAMP;
}

last thing to do is to add the method to retrieve the value as long

@Exclude
public Long getCreationDateLong() {
return creationDate;
}

That's it, I tested it and worked, i hope it can help someone later too.

Retrieve ServerValue.Timestamp from Firebase in Android app, when data is sent

Firebase.ServerValue.TIMESTAMP is set as a Map (containing {.sv: "timestamp"}) which tells Firebase to populate that field with the server's time. When that data is read back, it is the actual unix time stamp which is a Long.

Something like this will work:

Firebase ref = new Firebase("https://YOUR-FIREBASE.firebaseio.com");    

ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
Long timestamp = (Long) snapshot.getValue();
System.out.println(timestamp);
}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});

ref.setValue(ServerValue.TIMESTAMP);

For another example, you can see my answer to this question: Android chat crashes on DataSnapshot.getValue() for timestamp

ServerValue.TIMESTAMP saving and retrieving timestamp in Firebase

You are getting that error because you are trying to use a variable that has no visibility in setupWidgets() method. Your timestamp variable is visible only inside your ViewHolder class. That's its scope. See, you can use that variable only in the constructor you cannot use outside the class. To solve this, you either move those metods inside your ViewHolder class or you pass it as an argument when calling them.



Related Topics



Leave a reply



Submit