1

I cant get this right and wonder how to show the Firestore FieldValue.serverTimestamp() formatet individual depending on what local the device has.

The Firestore timestamp look like this

Sat Mar 10 19:42:32 GMT+01:00 2018

try {
      SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS", Locale.getDefault());
        Date parsedDate = dateFormat.parse("Sat Mar 10 19:42:32 GMT+01:00 2018");
        txtCreationDate.setText(new Date(parsedDate.getTime()).toString());
     } catch (ParseException e) {
        txtCreationDate.setText("time error");
     }

If one user i USA create the date time object then if another user i Italy view it then the creation time must adjusted to his clock

I have read many pages about this but the dateFormat parser give me error only.
Please advice

Tord Larsen
  • 2,670
  • 8
  • 33
  • 76

2 Answers2

1

What I normally do is simply use Date and setTimezine. The timestamp variable in my models are long variables and I use a code like this to convert the server time into a local one.

    DateFormat formatter = DateFormat.getDateInstance(DateFormat.SHORT, Locale.getDefault());
    formatter.setTimeZone(TimeZone.getTimeZone("STRING FOR TIMEZONE"));
    String localizedToday = formatter.format(YOURDATEOBJECT);
Levi Moreira
  • 11,917
  • 4
  • 32
  • 46
  • But I don´t know the temezone, I mean I can see it in the String like "Sat Mar 10 19:42:32 GMT+01:00 2018" . Should a do some String manipulation here to get the GMT+01? – Tord Larsen Mar 11 '18 at 16:13
  • Where is you timestamp variable? Do you have a model that holds it? – Levi Moreira Mar 11 '18 at 16:16
  • It´s just and `java.util.Date` I just retrieved the Firestore `MyClass my = document.toObject(MyClass);` and it has a `Date` field for `FieldValue.serverTimestamp()` – Tord Larsen Mar 11 '18 at 16:21
  • 1
    Ok, so if it is a Java Data you can check the edit and pass it to the date formatter. You've saved your object with the serve data, in your app you check the user timezone and then generate a string with the server time converted to a local time. – Levi Moreira Mar 11 '18 at 16:25
  • 1
    Haven´t had time but will spin up some emulators with dif timezones and se how it goes – Tord Larsen Mar 12 '18 at 13:03
  • Both answers to my Q is wrong, you both should use `getDateTimeInstance` instead of `getDateInstance` and getTimeInstance` – Tord Larsen Mar 12 '18 at 16:42
  • I reckon the point is that you should use setTimeZone() but okay – Levi Moreira Mar 12 '18 at 16:49
  • Thanks no sorry maybe misunderstanding, actually i dont have to set time zone with Alex answer , I just want to displace the timestamp – Tord Larsen Mar 12 '18 at 16:53
  • sure, no problem :) – Levi Moreira Mar 12 '18 at 16:54
0

Assuming that you have a model class named MyClass that has a Date field and public setters and getters, to get the date, please use the following code:

Date date = yourClass.getDate();
if (date != null) {
    DateFormat dateFormat = SimpleDateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, Locale.getDefault());
    String creationDate = dateFormat.format(date);
    Log.d("TAG", creationDate);
}
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Thanks no matter if I use `DateFormat.FULL` or `DateFormat.LONG` there is no time part like it say it should be [here](https://developer.android.com/reference/java/text/DateFormat.html) . There´s just like "January 12, 1952" - no time in there...I debug and see the `Date` is "Sat Mar 10 19:42:11 GMT+01:00 2018" – Tord Larsen Mar 12 '18 at 13:01
  • Sorry, my mistake. Please see my updated answer. Does it work now with the new code? – Alex Mamo Mar 12 '18 at 13:19
  • Both answers to my Q is wrong, you both should use `getDateTimeInstance` instead of `getDateInstance` and getTimeInstance` – Tord Larsen Mar 12 '18 at 16:42
  • `getTimeInstance()` is getting only the time? Is not what you want? If you want both, let me know to update my answer with `getDateTimeInstance`, ok? – Alex Mamo Mar 12 '18 at 16:45
  • thanks Alex, little misunderstanding , I think of a `timestamp` as both date and time – Tord Larsen Mar 12 '18 at 16:48
  • it should be(i found out) `getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, Locale.getDefault());` – Tord Larsen Mar 12 '18 at 16:50
  • Oh you're right. Just updated my anwer again. Cheers! – Alex Mamo Mar 12 '18 at 16:51