When using SimpleDateFormat to produce a string from a Firebase Firestore Timestamp object, I get an incorrect/nonsense result. Why?
I fabricate some Timestamps and send them to Firestore. I see expected results in the Firebase/Firestore console. I then get the douments from Firestore, retrieve the Timestamps from the documents, and convert them first to Date objects then to strings using SimpleDateFormat. The last step produces incorrect strings: specifically times "18:08:00" instead of "18:28:53" and "19:08:64" instead of "19:28:53" (Note: seconds > 59)
Firebase console shows the timestamps I expect:
eventTimestamp August 10, 2019 at 6:28:53 PM UTC+1
collectedTimestamp August 10, 2019 at 7:28:53 PM UTC+1
But the following code shows the problem (selectedMedEvent is an object created from the document retrieved, and getEventTimestamp() and getCollectedTimestamp() both return Firebase Timestamp objects):
Date eDate = selectedMedEvent.getEventTimestamp().toDate();
Date cDate = selectedMedEvent.getCollectedTimestamp().toDate();
String eString = selectedMedEvent.getEventTimestamp().toString();
String cString = selectedMedEvent.getCollectedTimestamp().toString();
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:MM:SS");
String eventDateString = formatter.format(eDate);
String collectedDateString = formatter.format(cDate);
Log.d(TAG, "Event Date is '" + eDate + "' or " + eString + " but SimpleDateFormat gives '" + eventDateString + "'");
Log.d(TAG, "Collected Date is '" + cDate + "' or " + cString + " but SimpleDateFormat gives '" + collectedDateString + "'");
Logcat shows the two Date objects printed as expected (matching the Firestore console) but errors elsewhere:
Event Date is 'Sat Aug 10 18:28:53 GMT+01:00 2019' or Timestamp(seconds=1565458133, nanoseconds=0) but SimpleDateFormat gives '10/08/2019 18:08:00'
Collected Date is 'Sat Aug 10 19:28:53 GMT+01:00 2019' or Timestamp(seconds=1565461733, nanoseconds=648000000) but SimpleDateFormat gives '10/08/2019 19:08:64'
Note the non-zero nanoseconds. When I create the Timestamp the nanoseconds value is always 0.
What is this happening and how do I fix it?
Later: I have created a stand-alone routine that demonstrates the problem. I plugged in the seconds and nanoseconds values that the Logcat had given me and I get the same unexpected SimpleDateFormat strings. Code is:
private void showBug() {
Timestamp ts1 = new Timestamp( 1565458133, 0);
Timestamp ts2 = new Timestamp( 1565461733, 648000000);
Date eDate = ts1.toDate();
Date cDate = ts2.toDate();
String eString = ts1.toString();
String cString = ts2.toString();
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:MM:SS");
String eventDateString = formatter.format(eDate);
String collectedDateString = formatter.format(cDate);
Log.d(TAG, "Event Date is '" + eDate + "' or " + eString + " but SimpleDateFormat gives '" + eventDateString + "'");
Log.d(TAG, "Collected Date is '" + cDate + "' or " + cString + " but SimpleDateFormat gives '" + collectedDateString + "'");
}
Logcat output is identical to previously reported.