简体   繁体   English

将 Cloud Firestore 时间戳转换为可读日期

[英]Convert Cloud Firestore Timestamp to readable date

How to convert timestamped retrieved from firebase firestore database and compare it with the current date and time.如何转换从 firebase firestore 数据库中检索到的时间戳并将其与当前日期和时间进行比较。

 db.collection("users").document(user.getuid).get()
                .addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
                    @Override
                    public void onSuccess(DocumentSnapshot documentSnapshot) {
                        String date = documentSnapshot.getData().get("last_login_date").toString();
                       
                    }
                });

Conver the date to readable and deduct it from the current time to show the difference in days, hours and minutes将日期转换为可读并从当前时间中减去以显示天数、小时数和分钟数的差异

The output of date variable is in below format日期变量的输出格式如下

Timestamp(seconds=1558532829, nanoseconds=284000000)

When you read a timestamp type field out of a document in Cloud Firestore, it will arrive as a Timestamp type object in Java.当您从 Cloud Firestore 中的文档中读取时间戳类型字段时,它将作为 Java 中的时间戳类型对象到达。 Be sure to read the linked Javadoc to find out more about it.请务必阅读链接的 Javadoc 以了解更多信息。

Timestamp timestamp = (Timestamp) documentSnapshot.getData().get("last_login_date");

The Timestamp has two components, seconds and nanoseconds.时间戳有两个组成部分,秒和纳秒。 If those values are not useful to you, you can convert the Timestamp to a Java Date object using its toDate() method, but you might lose some of the timestamp's nanosecond precision, since Date objects only use microsecond precision.如果这些值对您没有用处,您可以使用其toDate()方法将 Timestamp 转换为 Java Date 对象,但您可能会失去一些时间戳的纳秒精度,因为 Date 对象仅使用微秒精度。

Date date = timestamp.toDate();

With a Date object, you should be able to easily use other date formatting tools, such as Android's own date formatting options .使用 Date 对象,您应该能够轻松使用其他日期格式化工具,例如Android 自己的日期格式化选项 You can also use the Date's toMillis() method to compare it with the current time from System.currentTimeMillis() ;您还可以使用 Date 的toMillis()方法将其与System.currentTimeMillis()的当前时间进行比较;

See:看:

To get the value of the date property in a correct way, please use the following lines of code: 要以正确的方式获取date属性的值,请使用以下代码行:

db.collection("users").document(user.getuid).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
        if (task.isSuccessful()) {
            DocumentSnapshot document = task.getResult();
            if (document.exists()) {
                Date userDate = document.getDate("date");

                //Do what you need to with the userDate object
            }
        }
    }
});

So having the userDate object you can comparate it with any other Date object you want. 因此, userDate对象,您可以将其与所需的任何其他Date对象进行比较。

You can also use this.你也可以使用这个。

    String date= DateFormat.getDateInstance().format(timestamp.getTime());

it mostly use in RecyclerView's Adapter class.它主要用于 RecyclerView 的 Adapter 类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM