Advertisement
rachmadi

Lyla's Pojo (timestamp)

Mar 7th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.96 KB | None | 0 0
  1. import com.google.firebase.database.Exclude;
  2. import com.firebase.client.ServerValue;
  3.  
  4. import java.util.HashMap;
  5. import java.util.Map;
  6.  
  7. public class ExampleObject {
  8.     private String name;
  9.     private String owner;
  10.     private HashMap<String, Object> dateCreated;
  11.     private HashMap<String, Object> dateLastChanged;
  12.  
  13.     /**
  14.      * Required public constructor
  15.      */
  16.     public ExampleObject() {
  17.     }
  18.  
  19.     public ExampleObject(String name, String owner, HashMap<String,Object> dateCreated) {
  20.         this.name = name;
  21.         this.owner = owner;
  22.         this.dateCreated = dateCreated;
  23.  
  24.         //Date last changed will always be set to ServerValue.TIMESTAMP
  25.         HashMap<String, Object> dateLastChangedObj = new HashMap<String, Object>();
  26.         dateLastChangedObj.put("date", ServerValue.TIMESTAMP);
  27.         this.dateLastChanged = dateLastChangedObj;
  28.     }
  29.  
  30.     public String getName() {
  31.         return name;
  32.     }
  33.  
  34.     public String getOwner() {
  35.         return owner;
  36.     }
  37.  
  38.     public HashMap<String, Object> getDateLastChanged() {
  39.         return dateLastChanged;
  40.     }
  41.  
  42.     public HashMap<String, Object> getDateCreated() {
  43.       //If there is a dateCreated object already, then return that
  44.         if (dateCreated != null) {
  45.             return dateCreated;
  46.         }
  47.         //Otherwise make a new object set to ServerValue.TIMESTAMP
  48.         HashMap<String, Object> dateCreatedObj = new HashMap<String, Object>();
  49.         dateCreatedObj.put("date", ServerValue.TIMESTAMP);
  50.         return dateCreatedObj;
  51.     }
  52.  
  53. // Use the method described in http://stackoverflow.com/questions/25500138/android-chat-crashes-on-datasnapshot-getvalue-for-timestamp/25512747#25512747
  54. // to get the long values from the date object.
  55.     @Exclude
  56.     public long getDateLastChangedLong() {
  57.  
  58.         return (long)dateLastChanged.get("date");
  59.     }
  60.  
  61.     @Exclude
  62.     public long getDateCreatedLong() {
  63.         return (long)dateCreated.get("date");
  64.     }
  65.  
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement