0

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

LeeHari
  • 31
  • 7
  • you can check this https://stackoverflow.com/questions/41144296/how-to-convert-timestamp-to-appropriate-date-format/41144310 – Muhammad Bilal Bangash May 23 '21 at 04:13
  • @MuhammadBilalBangash thanks for your help. I just find solution for my problem. It make me so crazy. `Timestamp ts = (Timestamp) billArrayList.get(0).get("createAt"); Date createAt = ts.toDate();` – LeeHari May 23 '21 at 04:18

1 Answers1

2

I just find the solution for my problem

Timestamp ts = (Timestamp) billArrayList.get(0).get("createAt");
Date createAt = ts.toDate();
LeeHari
  • 31
  • 7