Firebase TIMESTAMP to date and Time

后端 未结 18 2594
温柔的废话
温柔的废话 2020-11-27 03:39

I am using firebase for my chat application. In chat object I am adding time stamp using Firebase.ServerValue.TIMESTAMP method.

I need to show the messa

18条回答
  •  旧时难觅i
    2020-11-27 03:48

    First Of All Firebase.ServerValue.TIMESTAMP is not working anymore for me.

    So for adding timestamp you have to use Firebase.database.ServerValue.TIMESTAMP

    And the timestamp is in long millisecond format.To convert millisecond to simple dateformat .

    Ex- dd/MM/yy HH:mm:ss

    You can use the following code in java:

    To get the timestamp value in string from the firebase database

    String x = dataSnapshot.getValue (String.class);
    

    The data is in string now. You can convert the string to long

    long milliSeconds= Long.parseLong(x);
    

    Then create SimpleDateFormat

    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yy HH:mm:ss");
    

    Now convert your millisecond timestamp to ur sdf format

    String dateAsString = sdf.format (milliSeconds);
    

    After that you can parse it to ur Date variable

     date = sdf.parse (dateAsString);
    

提交回复
热议问题