Prathmesh Patil Prathmesh Patil - 5 months ago 564
Android Question

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.

Answer

Your timestamp 1466769937914 equals 2016-06-24 12:05:37 UTC. The problem is that you are multiplying the timestamp by 1000 for no real reason. then you get 1466769937914000 which equals 48450-02-01 21:51:54 UTC. So results you are getting are technically fine and all is working correctly. You just feed it with wrong input data. Remove this * 1000 and you should be good:

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