如何在RecyclerView中显示Firestore时间戳(日期和时间)

时间:2019-05-21 12:06:18

标签: android firebase google-cloud-firestore

我正在构建银行应用程序,并想显示该帐户上的交易记录,当我将时间保存到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);
        }
    }
}

我的View,App和Firestore中显示的内容

  

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

what it looks like in the app What my Firestore data looks like

2 个答案:

答案 0 :(得分:1)

更改此:

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

对此:

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

来自docs

  

public Date toDate ()

     

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

答案 1 :(得分:1)

如果Timestamp是firebase软件包,则可以使用Timestamp#toDate()函数

model.gettTimestamp().toDate().toString()应该给您整个约会