如何将firebase时间戳转换为日期和时间 [英] How to convert firebase timestamp into date and time

查看:301
本文介绍了如何将firebase时间戳转换为日期和时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试从firebase时间戳获取日期和时间如下:

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)

但我得到的结果如下:

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

你可以看到一年不是

所以,请帮我解决这个问题。

So, please help me to fix this.

推荐答案

p>您的时间戳 1466769937914 等于 2016-06-24 12:05:37 UTC 。问题是你将时间戳乘以1000,没有真正的原因,结果给你 1466769937914000 。这等于 48450-02-01 21:51:54 UTC ,所以你获得的结果在技术上是正确的,一切都正常。您只需输入错误的输入数据,只有这部分必须被修复。一个解决方案是简单地去除乘法,你应该都很好:

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 only this part 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))

这篇关于如何将firebase时间戳转换为日期和时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆