How to convert firebase timestamp into date and time

I have tried to get date and time from firebase timestamp as follows:

   Date date=new Date(timestamp*1000);
   SimpleDateFormat sfd = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
   sfd.format(date)

but I'm getting results like:

:02-02-48450 04:21:54
:06-02-48450 10:09:45
:07-02-48450 00:48:35

as you can see the year is not as we live.

So, please help me to fix this.


Your timestamp 1466769937914 equals to 2016-06-24 12:05:37 UTC . The problem is that you are multiplying the timestamp by 1000 for no real reason, which in result gives you 1466769937914000 . And that equals to 48450-02-01 21:51:54 UTC , so results you are getting are technically correct and all is working fine. You just feed your code with wrong input data and this us the only part that must be fixed. A solution is simply to remove multiplication and you should be all good:

   SimpleDateFormat sfd = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
   sfd.format(new Date(timestamp))

According to Firebase documentation, the types that are available JSON are:

  • String
  • Long
  • Double
  • Boolean
  • Map<String, Object>
  • List<Object>
  • Quoting another Stack Overflow post, I suggest you use JSON date string format yyyy-MM-dd'T'HH:mm:ss.SSSZ instead of epoch timestamp.

    Comparing 1335205543511 to 2012-04-23T18:25:43.511Z , you can noticed that:

  • It's human readable but also succinct
  • It sorts correctly
  • It includes fractional seconds, which can help re-establish chronology
  • It conforms to ISO 8601
  • ISO 8601 has been well-established internationally for more than a decade and is endorsed by W3C, RFC3339, and XKCD


    String time=dataSnapshot.child("timeStamp").getValue().toString(); Long t=Long.parseLong(time);

    Date myDate = new Date(t*1000);

    Result


    Fri May 11 05:37:58 GMT+06:30

    链接地址: http://www.djcxy.com/p/46664.html

    上一篇: 如何在Asp.net Web API中使用特定的CultureInfo

    下一篇: 如何将Firebase时间戳转换为日期和时间