0

I want to save date from my Android app to the Firebase Realtime Database as a Date Object.

I have already tried:

System.currentTimeMillis() / 1000L shows this

new Date(System.currentTimeMillis() / 1000L) shows this

It must be stored as shown. Any ideas? Thanks for your time.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
Div MoD
  • 158
  • 8

1 Answers1

2

Realtime Database doesn't have a Date type value. It only supports the types of values that are also supported by JSON. This means you should be storing your timestamps as integers. Typically one uses the System.currentTimeMillis() representation (not divided by 1000 as you're showing), because this is also the way ServerValue.TIMESTAMP works, and the way that security rules internally represents the current time.

Note that this is different than Cloud Firestore, which does have a native Timestamp field type.

What you're showing in the last screenshot is just a formatted string date. I don't recommend using that in a database, because formatted strings can be difficult to compare in the way you'd expect. You should instead format the string on the client, using the device's preferred date formatting for the user's locale.

If you must store a formatted string date, then you will have to do that yourself, maybe using one of the options provided by Android.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441