0

I've been trying to look for an answer for a while but couldn't find one corresponding to mine.

I have stored the Date Of Birth of a person in firebase AS a String. I've retrieve it correctly using onDataChange. But the problem arises when i need to calculate the age of that person and i can't extract the year from that String. Can anyone help me?

Thank you

Here is the data stored in firebase

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
OoMaR jOhUr
  • 1,503
  • 2
  • 7
  • 10

1 Answers1

-1

Split your String and take the last value like this:

int year = Integer.parseInt(date.split("/")[2]);
Amadej Kastelic
  • 577
  • 5
  • 9
  • No I've tried but it doesn't work. Here is my code myDB.addValueEventListener(new ValueEventListener() { Override public void onDataChange(NonNull DataSnapshot dataSnapshot) { additionalDetails AD = dataSnapshot.getValue(additionalDetails.class); String dob = AD.getDOB(); int year = Integer.parseInt(dob.split("/")[2]); testDate.setText(year); } Override public void onCancelled(@NonNull DatabaseError databaseError) { } }); – OoMaR jOhUr Oct 02 '18 at 11:47
  • Well yeah... setText method accepts String so you should put date.split("/")[2] there – Amadej Kastelic Oct 02 '18 at 11:55
  • I've solved the problem. Since setText does not display int value, we need to convert it by using **String.valueOf()** Thank you very much! – OoMaR jOhUr Oct 02 '18 at 12:04