Reputation: 1
I have code :
val value: HashMap<String, Any> = hashMapOf()
value["sender_id"] = userId!!
value["name"] = firstname!!
value["text"] = et_search_box.text.toString()
value["avatar"] = avatar!!
value["date"] = ServerValue.TIMESTAMP.toDouble()
how to add date types to double?
Upvotes: 0
Views: 642
Reputation: 8106
Firebase Timestamp is has two specific functions getSeconds() and getNanoseconds() to resolve the time in any other units.
For example to get the time in seconds with upto 9 digits of accuracy (nano second accuracy)
val timestampInSeconds: Double = ServerValue.TIMESTAMP.run {
getSeconds().toDouble + getNanoseconds.toDouble() / 1_000_000_000
}
This is very accurate, however timestamps are usually represented by milliseconds and can easily be deserialized to a Date object if you want to, for instance java.util.Date uses millisecond value to evaluate the date object.
To get standard millisecond value:
val stdTimestampMillis: Long = ServerValue.TIMESTAMP.toDate().getTime()
// to deserialize it to Timestamp/Date object later
val date: Date = Date(stdTimestampMillis)
val timestamp: Timestamp = Timestamp(date)
Upvotes: 1
Reputation: 171
Looking at the image from the link you have shared, ServerValue.TIMESTAMP
is of type String
.
Depending on the date-format, you need to parse the String to some Time object.
Then that can be converted to double.
For e.g., I have used java.time.Instant
to convert ISO 8601 date string to double.
val instant = Instant.parse("2020-05-13T05:05:52.945571Z")
val d: Double = instant.epochSecond + instant.nano / 1_000_000_000.0
Note for Android: java.time.Instant
is available since Java 8.
Upvotes: 1