【问题标题】:How to display Firestore timestamp(Date and Time) in a RecyclerView如何在 RecyclerView 中显示 Firestore 时间戳(日期和时间)
【发布时间】:2019-05-21 12:06:17
【问题描述】:

我正在构建一个银行应用程序并希望显示帐户交易的历史记录,当我将时间保存到 Firestore 时,它​​的格式为时间戳,但当我尝试在我的 RecyclerView 中显示它时,它只有几秒和纳秒。

如何显示日期和时间?

我的recyclerView方法:

    private void setUpRecyclerView() {
        String userId = FirebaseAuth.getInstance().getCurrentUser().getUid();

        CollectionReference accountTransRef = db.collection(userId).document("accounts")
                .collection("accounts").document(accountID).collection("transactions");

        Query query = accountTransRef.orderBy("tTimestamp",Query.Direction.DESCENDING);
        FirestoreRecyclerOptions<AccountTransactionModel> options = new FirestoreRecyclerOptions.Builder<AccountTransactionModel>()
                .setQuery(query, AccountTransactionModel.class)
                .build();

        adapter = new AccountTransferAdapter(options);

        RecyclerView recyclerView = findViewById(R.id.rwTransactionList);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(adapter);
    }

我的交易模型

public class AccountTransactionModel {
    private String tType,tAccountToId, tDocumentId;
    private Timestamp tTimestamp;
    private double tAmount;

    public AccountTransactionModel() {
    }

    public AccountTransactionModel(String tType, String tAccountToId, String tDocumentId, Timestamp tTimestamp, double tAmount) {
        this.tType = tType;
        this.tAccountToId = tAccountToId;
        this.tDocumentId = tDocumentId;
        this.tTimestamp = tTimestamp;
        this.tAmount = tAmount;
    }

    public String gettType() {
        return tType;
    }

    public String gettAccountToId() {
        return tAccountToId;
    }

    @Exclude
    public String gettDocumentId() {
        return tDocumentId;
    }

    public void settDocumentId(String tDocumentId) {
        this.tDocumentId = tDocumentId;
    }

    public Timestamp gettTimestamp() {
        return tTimestamp;
    }

    public double gettAmount() {
        return tAmount;
    }
}

我的适配器

public class AccountTransferAdapter extends FirestoreRecyclerAdapter<AccountTransactionModel, AccountTransferAdapter.TransferHolder > {


    public AccountTransferAdapter(@NonNull FirestoreRecyclerOptions<AccountTransactionModel> options) {
        super(options);
    }

    @Override
    protected void onBindViewHolder(@NonNull TransferHolder holder, int position, @NonNull AccountTransactionModel model) {
        holder.tvTransListAmount.setText(Double.toString(model.gettAmount()));
        holder.tvTransListType.setText(model.gettType());
        holder.tvTransListTime.setText(model.gettTimestamp().toString());
    }

    @NonNull
    @Override
    public TransferHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.transactions_list,viewGroup,false);
        return new TransferHolder(v);

    }

    class TransferHolder extends RecyclerView.ViewHolder{
        TextView  tvTransListAmount;
        TextView tvTransListTime;
        TextView tvTransListType;

        public TransferHolder(@NonNull View itemView) {
            super(itemView);
            tvTransListAmount = itemView.findViewById(R.id.trans_list_amount);
            tvTransListTime = itemView.findViewById(R.id.trans_list_time);
            tvTransListType = itemView.findViewById(R.id.trans_list_type);
            //tvAccName = itemView.findViewById(R.id.tvAccountName);
            //tvAccBalance = itemView.findViewById(R.id.tvAccountBalance);
        }
    }
}

我的视图、应用程序和 Firestore 中显示的内容

时间戳(秒=1558437203,纳秒=72000000)

【问题讨论】:

  • Timestamp 类来自 firebase 包对吗?

标签: android firebase google-cloud-firestore


【解决方案1】:

如果Timestamp 是firebase 包,那么你可以使用Timestamp#toDate() 函数

model.gettTimestamp().toDate().toString() 应该给你完整的日期

【讨论】:

    【解决方案2】:

    改变这个:

     holder.tvTransListTime.setText(model.gettTimestamp().toString());
    

    进入这个:

     holder.tvTransListTime.setText(model.gettTimestamp().toDate());
    

    来自docs

    public Date toDate ()

    返回与此时间戳对应的新日期。

    【讨论】:

      猜你喜欢
      • 2021-06-05
      • 2019-10-06
      • 1970-01-01
      • 2020-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-13
      相关资源
      最近更新 更多