2017-04-05 32 views

回答

0

您可以使用以下内容。

Calendar cal = Calendar.getInstance(); 

cal.setTimeInMillis(ServerValue.TIMESTAMP); 
SimpleDateFormat fmt = new SimpleDateFormat("dd MM yyyy",Locale.US); 
fmt.format(cal.getTime()); //This returns a string formatted in the above way. 

如果ServerValue.TIMESTAMP返回一个字符串,可以解析使用Long.parseLong(Server.TIMESTAMP);

+0

ServerValue.TIMESTAMP是地图类型,因此它不工作 – sunny

+0

瑞尔它不会工作,你无法转换地图像这样直接键入龙 – sunny

1

不能使用Server.TIMESTAMP获得一个约会的字符串。该医生说:

为自动填充当前时间戳由火力地堡服务器决定(自Unix纪元时间,以毫秒为单位)中的占位符值

这意味着,当你setValue()updateChildren() ,你可以在Map中加入这个常量来告诉服务器把纪元时间放在那个节点上。例如:

mRef = FirebaseDatabase.getInstance().child("whatever/path/in/your/database"); 
mRef.setValue(Server.TIMESTAMP); 

这将在<your Firebase>/whatever/path/in/your/database长,将看起来像149141530600.这是当前时代的时间在写这个答案我拿来设置。它对应于从1970年1月1日到复制该值的毫秒数。

Long time = dataSnapshot.getValue(Long.class); 
Calendar calendar = GregorianCalendar.getInstance(); 
calendar.setTimeInMillis(time); 

如果你只希望服务器设置为(在数据库中保存它是没有意义的)的时候,你可以使用:那么,如果你有一个侦听器节点,您可以用得到的日历特殊节点:

`FirebaseDatabase.getInstance().getReference(".info/serverTimeOffset");` 

甲侦听到该节点返回一个Double表示一个近似的设备的时间和服务器时间之间的偏移量。然后,您可以设置Calendar使用:

calendar.setTimeInMillis(System.currentTimeMillis() + offset);