5

I want to save the date that a post was created in Firestore but I do not want to use the System time. Rather I want to use the server timestamp for accuracy sake. So I am using FieldValue.serverTimestamp() to get the server timestamp but the data type of my variable that holds this is Date. So How can I cast FieldValue.serverTimestamp() to Date?

Below is how my data class looks

data class MyModel( var timeStamp: Date,
    constructor(): this(Calendar.getInstance().time, "")
}

PS: When I declare the timestamp as FieldValue in the data class, I get the error below:

java.lang.RuntimeException: No properties to serialize found on class com.google.firebase.firestore.FieldValue

Ilo Calistus
  • 2,005
  • 3
  • 19
  • 23

3 Answers3

8

You get the following error:

java.lang.RuntimeException: No properties to serialize found on class com.google.firebase.firestore.FieldValue

Because FieldValue is not a supported data type. You should use the Date class or any other class that extends Date class, for example Timestamp class.

How do I cast FieldValue.serverTimestamp() to Kotlin/Java Date Class

There is no need to do any cast. In Java there is even no need to initialize the timeStamp field. To make it work, you should only use an annotation, as explained in my answer from the following post:

Edit:

In Kotlin, you should initialize your timeStamp field in the constructor with a null value like this:

data class MyModel(
               @ServerTimestamp
               val timeStamp: Date? = null)
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Thanks for your answer Alex. I have 2 follow up questions 1. You said I don't have to initialize Date. If so, then how will my contractor look. Sample code in Kotlin, please? 2. What is the best way to get the current time when you want to save a post in Firestore. To avoid using client time which may be wrong, I used FieldValue.serverTimestamp(). Do you have a better approach? – Ilo Calistus Aug 17 '19 at 18:54
  • Hi calistus! For the first question, please see my updated answer. Regarding the second, no, there is not a better approach. – Alex Mamo Aug 18 '19 at 10:47
0

You can make use of an object to hold this value and later while using this value check the type of the object and make use of it. As of my knowledge the datatype returned is Long and you have to convert it manually to Data if you need.

The code for this will look like this,

replace this

data class MyModel( var timeStamp: Date,

with

data class MyModel( var timeStamp: Object,

And when using this timeStamp anywhere check it's type. In java it will look like

if (timeStamp instanceof Long) {
    // change Long to Date
    //do this
}else{
    //do something else
}

set the value for timeStamp as FieldValue.serverTimestamp() itself.

0

model class

data class MyModel(
        @get: PropertyName("timestamp") @set: PropertyName("timestamp") var timestamp: Date= Date()
    )

when initialize it;

val model = MyModel().apply{
   this.timestamp to FieldValue.serverTimestamp()
}