0

In my Book class , I have several fields. One of them is additionTime, I need to get server timestamp in this field. My class is here

public class Book{

    private String bookId;
    private String bookName;
    private String authors;
    private float rating;
    private String category;
    private String thumbnail;
    private BookState bookState;
    private long additionTime;
    private String ownerID;

    public Book(){

    }

    //other getter and setters
    public FieldValue getAdditionTime(){
        return  FieldValue.serverTimestamp();
    }

    @Exclude
    public long getAdditionTimeLong(){
        return additionTime;
    }

    public void setAdditionTime(long additionTime) {
        this.additionTime = additionTime;
    }


}

But in additionTime field I'm getting like August 17, 2019 at 10:03:28 PM UTC+6, how can I get Unix Timestamp? like 1566058638. enter image description here

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Shahrear Bin Amin
  • 1,075
  • 13
  • 31
  • Convert you time into timestamp.... – Kabir Aug 17 '19 at 16:45
  • @Kabir thanks. How can I do that? – Shahrear Bin Amin Aug 17 '19 at 17:06
  • I just want to know...all time you get " August 17, 2019 at 10:" like format??? – Kabir Aug 17 '19 at 17:08
  • SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = format.parse("your string goes here"); long timestamp = date.getTime(); – Kabir Aug 17 '19 at 17:11
  • @Kabir Yes..... – Shahrear Bin Amin Aug 17 '19 at 17:12
  • Try above code... – Kabir Aug 17 '19 at 17:12
  • @Kabir FieldValue.serverTimestamp() returns FieldValue. How can I get the string format? Firestore has a data type named `timestamp`, I think it automatically maps this FieldValue to `timestamp`. – Shahrear Bin Amin Aug 17 '19 at 17:20
  • Check this:https://stackoverflow.com/questions/51416027/how-to-get-a-javascript-date-object-from-the-new-firebase-firestore-fieldvalue/51416028 – Kabir Aug 17 '19 at 17:23
  • @Kabir I have some issue while adding new documents. I've added new a new question. https://stackoverflow.com/questions/57578448/error-in-getting-server-timestamp-in-firestore?noredirect=1#comment101617113_57578448. Can you please check this? – Shahrear Bin Amin Aug 21 '19 at 02:28
  • Ok sure.....but i need some time...i will sure reply soon. – Kabir Aug 21 '19 at 02:29
  • @Kabir After some workaround, I found that when I'm returning to the activity immediately where I showed user list app crashes, It seems like my document is added but `ServerTimestamp` expression is not replaced with the correct timestamp. When I start my app again, It shows my added document in the list. Or If I wait for a while then return my activity It shows my userlist properly. – Shahrear Bin Amin Aug 21 '19 at 03:09
  • Now i have checked your new question.crash is related totime in your model class.I just suggest one link of whole example of firestore:https://github.com/mitchtabian/FirestoreGettingStarted/tree/Inserting_Data_Start And https://www.youtube.com/playlist?list=PLgCYzUzKIBE8Jl8nTU2epOY1soaCWEt7f I hope this example help you to solve the problem. – Kabir Aug 21 '19 at 04:09

0 Answers0