How to convert timestamp field from firestore to Date in android?

I have a field createAt (timestamp) in a document. When I get it from firestore, I receive below constructor. How can I convert it to Date in Java?

D/TEST: Timestamp field: Timestamp(seconds=1621098000, nanoseconds=0)

here is my code

fStore.collection("Bill").whereEqualTo("userID", userID).get()
                .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
            @Override
            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                if (task.isSuccessful()) {
                    if (!task.getResult().isEmpty()) {
                        for (QueryDocumentSnapshot document : task.getResult()) {
                            ArrayList<Map<String, Object>> billArrayList = new ArrayList<>();
                            billArrayList.add(document.getData());
                            String id = document.getId();
                            Integer addressID = Integer.parseInt(billArrayList.get(0).get("addressID").toString());
                            //Timestamp ts = (Timestamp) billArrayList.get(0).get("createAt");
                            Log.d("TEST", "Timestamp field: " + billArrayList.get(0).get("createAt"));
                        }
                    }
                }
            }}); 

I have tried convert this timestamp to long but it not work

Answers 1

  • I just find the solution for my problem

    Timestamp ts = (Timestamp) billArrayList.get(0).get("createAt");
    Date createAt = ts.toDate();
    

Related Articles