0

I am creating a Spring Boot application and I have a timestamp variable in Firestore DB I want to send date in that field. How can I do that?

This is from my Firebase DB I am using dob as time stamp

This is my data type am using to call that timestamp from DB but that is wrong I am not able to send the correct date

This is my service I am sending data to Firebase

    public Patient addPatient(Patient patients) throws InterruptedException, ExecutionException  {
    
    Firestore dbFirestore = FirestoreClient.getFirestore();
    ApiFuture<WriteResult> collectionApiResult = dbFirestore.collection(this.fireStoreCollection).document(patients.getUserId()).set(patients);
    return patients;
}

my Firestore DB screenshot I want to send dob in this

hmatar
  • 2,437
  • 2
  • 17
  • 27
  • 1
    Hi, Can you be more clear with your question? Do you want to retrieve Firestore's timestamp data in readable form or do you wanna add the timestamp data into Firestore? – Zeenath S N Nov 16 '21 at 07:06
  • Hi, Both i want to do i want to retrieve as well as want to send. above screenshot i have added that data into databse manually . I want to do that same with my api . i have written get API Also i want that all in readable form as well as to send in json to the db. – vaibhav singh baghel Nov 16 '21 at 07:45
  • 1
    Hi, can you check if [adding server timestamp field to the Object](https://stackoverflow.com/questions/47355540/adding-server-timestamp-field-to-the-object-which-being-added) and [Convert Cloud Firestore timestamp to readable date](https://stackoverflow.com/questions/56259128/convert-cloud-firestore-timestamp-to-readable-date) helps you? – Zeenath S N Nov 16 '21 at 08:36
  • HI, not exactly getting the desired output in comment i am not able to share images or code .. Please if possible you can share me your contact email . so you can help me in better way . Thanks . – vaibhav singh baghel Nov 16 '21 at 13:03
  • i am sharing a document in which i have added some screen shot with which you can check once.. --------------- https://docs.google.com/document/d/1aZs3YIiTyGhJgMy7tsZgkE9fD4eNDkqgg7KZWZ6YHKI/edit?usp=sharing – vaibhav singh baghel Nov 16 '21 at 14:28
  • after using Map i was able to add the data but that was not as the desired output i have shared screenshot in doc. – vaibhav singh baghel Nov 16 '21 at 14:31
  • Hey, so I found this [answer](https://stackoverflow.com/a/62704887/15774177) on stackoverflow, can you take a look at it and see if it helps? – Zeenath S N Nov 18 '21 at 06:25

1 Answers1

0

As mentioned by @Zeenath S N above, this post has your answer.

You can use ServerTimestamp annotation. Try changing

private Timestamp dob;

to

private Date dob;

in your code.

Alternatively, as below:

If you want to write a timestamp field, and you can't use a server timestamp, you will need to either provide a regular Java Date object, or a Firebase Timestamp object. Either of these types of values will convert to a timestamp field in the document.

When you query for the document, the field will always show up in the document snapshot as a Timestamp type object.

Gourav B
  • 864
  • 5
  • 17