在Firebase Firestore中获取当前服务器的日期和时间 [英] Get current server Date and Time in Firebase Firestore

查看:106
本文介绍了在Firebase Firestore中获取当前服务器的日期和时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Firebase Firestore中获得服务器时间,以便可以将其用于倒计时逻辑.我在任何地方都找不到,大多数情况下,他们建议使用FiedValue.serverTimeStampServerValue.TIMESTAMP两者都用于保存数据.我不想保存数据,我只想获取服务器时间.

I want to get my server time in Firebase Firestore so I can use it for my countdown logic. I can't find anywhere and mostly they suggest to use FiedValue.serverTimeStamp or ServerValue.TIMESTAMP which is both are used to save data. I don't want to save data I just want to get the server time.

是否有像FirebaseFirestore.getInstance().getServerTime()这样的静态方式?

Is there a static way like FirebaseFirestore.getInstance().getServerTime()?

推荐答案

按照@Doug的说法,无法仅从Firebase获得时间.但是,相同的处理方式很多.

As per said by the @Doug there is no way of getting just only time from Firebase. However there are many round-ways for the same.

首先,由于当今时代的所有时间都与卫星同步,因此您可以通过应用用户的设备使用倒数计时.可以使用:

Firstly, you can use the count-down using the device of the app user since all the time in today era is synced with satellite. This can be done using:

Date dateStart = new Date();
Date dateEnd = new Date();
System.out.println(dateEnd.getTime() - dateStart.getTime()/1000);

其次,在firebase中为开始时间创建一个对象(针对特定用户),然后当倒计时结束时,计算当前时间与数据库中上传时间之间的时差.

Secondly, create an object in firebase for the starting time (for particular user) and then when the countdown is over then count the difference between the current time and the time uploaded in database.

FirebaseFirestore db = FirebaseFirestore.getInstance();

// At the time of setting.

Map<String, Object> map= new HashMap<>();
map.put("startTime", (new Date()).getTime());

db.collection(userdata).document(userid).set(map);

// At the time of retrieving:

db.collection(userdata).document(userid).get()
.addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
        if (task.isSuccessful()) {
            DocumentSnapshot document = task.getResult();
            if (document.exists()) {
                // Here you can get the time..
            }
        } else {
            Log.d(TAG, "get failed with ", task.getException());
        }
    }
});

现在,如果您不喜欢上述任何一种方法,则可以在 https://://firebase.google.com/support

Now if you don't like any of the above method then you can file a support request for time at https://firebase.google.com/support

这篇关于在Firebase Firestore中获取当前服务器的日期和时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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