0

Is it possible to run a date based query from a Firebase database if the date is saved as a String? Or can it be converted? I want to be able to search for records within a particular date range but the dates are saved and retrieved as strings. I use a DatePickerDiaglog to set the date when I add the record. The following is the method for adding a record:

private void addAnimal(){
        String breed = editTextBreed.getText().toString().trim();
        String gender = spinnerGender.getSelectedItem().toString();
        String source = editTextSource.getText().toString().trim();
        String DOB = mDisplayDate.getText().toString().trim();
        String tag = editTextTagNumber.getText().toString().trim();

        if(TextUtils.isEmpty(breed)){
            Toast.makeText(this, "You should enter a breed", Toast.LENGTH_LONG).show();
        }
        else if(TextUtils.isEmpty(source)){
            Toast.makeText(this, "You should enter a source", Toast.LENGTH_LONG).show();
        }
        else if (TextUtils.isEmpty(DOB)){
            Toast.makeText(this, "You should enter a Date of Birth", Toast.LENGTH_LONG).show();
        }
        else if (TextUtils.isEmpty(tag)){
            Toast.makeText(this, "You should enter a Tag Number", Toast.LENGTH_LONG).show();
        }
        else if (tag.length() < 6){
            Toast.makeText(this, "Please enter valid tag number", Toast.LENGTH_LONG).show();

        }
        else{
            String id = databaseAnimal.push().getKey();

            Animal animal = new Animal(id, breed, gender, source, DOB, tag);

            databaseAnimal.child(id).setValue(animal);

            Toast.makeText(this, "Animal added", Toast.LENGTH_LONG).show();
        } }

This is the code I used to retrieve all the records.

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_data_retrieved);

        listView = findViewById(R.id.list_item);

        databaseReference = FirebaseDatabase.getInstance().getReference("animals");
        animalList = new ArrayList<>();


        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                String tempListValue =(listView.getItemAtPosition(position).toString());
                //String tempListValue = animalList.get(position).toString();
                        //.get(position).toString();

                TextView animalID = (TextView) parent.findViewById(R.id.animalID);
                String animalIDText = (String) animalID.getText();

                TextView animalGender = (TextView) parent.findViewById(R.id.gender);
                String animalGenderText = (String) animalGender.getText();


                TextView animalDOB = (TextView) parent.findViewById(R.id.DOB);
                String animalDOBText = (String) animalDOB.getText();

                TextView source = (TextView) parent.findViewById(R.id.source);
                String sourceText = (String) source.getText();

                TextView animalBreed = (TextView) parent.findViewById(R.id.breed);
                String animalBreedText = (String) animalBreed.getText();

                TextView animalTag = (TextView) parent.findViewById(R.id.tag);
                String animalTagText = (String) animalTag.getText();

                //Multiple Values
                Intent intent = new Intent(DataRetrieved.this, ViewAnimalDetails.class);
                Bundle extras = new Bundle();
                extras.putString("EXTRA_ID",animalIDText);
                extras.putString("EXTRA_DOB",animalDOBText);
                extras.putString("EXTRA_GENDER",animalGenderText);
                extras.putString("EXTRA_SOURCE",sourceText);
                extras.putString("EXTRA_BREED", animalBreedText);
                extras.putString("EXTRA_TAG", animalTagText);
                intent.putExtras(extras);
                startActivity(intent);

                //intent.putExtra("ListViewClickedValue", tempListValue);

                startActivity(intent);


            }
        }); //List view item click ends
    }

    @Override
    protected void onStart(){
        super.onStart();
        databaseReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                for(DataSnapshot animalSnapshot : dataSnapshot.getChildren()){
                    Animal animal = animalSnapshot.getValue(Animal.class);
                    animalList.add(animal);
                }

                AnimalInfoAdapter animalInfoAdapter = new AnimalInfoAdapter(DataRetrieved.this, animalList);
                listView.setAdapter(animalInfoAdapter);

            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });
    }

Let me know if there is any other information needed, Thanks!

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
C.Mc
  • 9
  • 3
  • If you don't want to use date object for DOB, consider saving the date-string in such format as `YYYYMMddHHmmSSS` so that it will be easy for you to compare. – makata Jan 31 '19 at 17:25

1 Answers1

0

Is it possible to run a date based query from a Firebase database if the date is saved as a string?

The simple answer is yes, you can query based on a String but the results will be wrong. The results that you get when you query literal strings is totally different than the result you get when querying based on Date objects.

Or can it be converted?

Yes it can but the result will be the same as above. The simplest solution to solve this, is to convert the date property from String to Date and query the data according to it.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • How do I go about doing that? Is it being converted before it's saved or upon its retrieval? – C.Mc Jan 31 '19 at 15:58
  • It is saved as a Date object in the first place. Take a look at my answer from this **[post](https://stackoverflow.com/questions/48474957/servertimestamp-is-allways-null-on-firebase-firestore/48475027)**. – Alex Mamo Jan 31 '19 at 15:59
  • 1
    Great! Thanks for your help – C.Mc Feb 01 '19 at 21:38