0

I want to make a program which displays a date introduced in the Firebase realtime database. Currently my program shows thre variables which are strings in a RecyclerView. I want to obtain the date introduced in Firebase and then compare it to the date of today.

I show you my code:

Main Activity:

public class Documentation extends AppCompatActivity {

FirebaseDatabase firebaseDatabase;
FirebaseAuth mAuth;
FirebaseUser user;

DatabaseReference reference;
RecyclerView recyclerView;
ArrayList<Balloon> list;
MyAdapterDoc adapterDoc;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);;
    recyclerView=(RecyclerView)findViewById(R.id.recycler_view);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));

    list=new ArrayList<Balloon>();
    firebaseDatabase=FirebaseDatabase.getInstance();
    mAuth=FirebaseAuth.getInstance();
    user=mAuth.getCurrentUser();

    //txt_user.setText(user.getEmail());

    reference= FirebaseDatabase.getInstance().getReference().child(user.getUid());
    reference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            for(DataSnapshot dataSnapshot1:dataSnapshot.getChildren()){
                Balloon b=dataSnapshot1.getValue(Balloon.class);
                list.add(b);
            }
            adapterDoc=new MyAdapterDoc(Documentation.this,list);
            recyclerView.setAdapter(adapterDoc);

        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
            Log.w("Ch3","Failed to read value.", databaseError.toException());

        }
    });

}

MyAdapter:

public class MyAdapterDoc extends RecyclerView.Adapter<MyAdapterDoc.MyViewHolder> {

Context context;
ArrayList<Balloon> balloons;

public MyAdapterDoc(Context c,ArrayList<Balloon> b){
    context=c;
    balloons=b;
}

@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    return new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.balloon_item,viewGroup,false));
}

@Override
public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int i) {
    myViewHolder.license.setText(balloons.get(i).getLicense());
    myViewHolder.cda.setText(balloons.get(i).getCda());
    myViewHolder.cdm.setText(balloons.get(i).getCdm());



}

@Override
public int getItemCount() {
    return balloons.size();
}

class MyViewHolder extends RecyclerView.ViewHolder{
    TextView license,cdm,cda,insurance;

    public MyViewHolder(@NonNull View itemView){
    super(itemView);
    license=(TextView)itemView.findViewById(R.id.txt_license);
    cda=(TextView)itemView.findViewById(R.id.txt_cda);
    cdm=(TextView)itemView.findViewById(R.id.txt_cdm);


    }
}

}

Balloon class. In this code I added the object insurance which is a Date, but not sure if it is correct:

public class Balloon {
private String license,cda,cdm;
//private boolean cda,cdm;
private Date insurance;


public Balloon() {
}

public Balloon(String license, String cda, String cdm) {
    this.license = license;
    this.cda = cda;
    this.cdm = cdm;
    this.insurance = insurance;
}

public String getLicense() {
    return license;
}

public void setLicense(String license) {
    this.license = license;
}

public String getCda() {
    return cda;
}

public void setCda(String cda) {
    this.cda = cda;
}

public String getCdm() {
    return cdm;
}

public void setCdm(String cdm) {
    this.cdm = cdm;
}

public Date getInsurance() {
    return insurance;
}

public void setInsurance(Date insurance) {
    this.insurance = insurance;
}

In firebase, my JSON document looks like this: here

Thank you:)

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193

2 Answers2

0

Currently my program shows thre variables which are strings in a RecyclerView.

Actually this is not how you should store a timestamp in a Firebase realtime database. The correct way of adding a timestamp is explained in my answer from this post and not as a String as you currently do.

I want to obtain the date introduced in Firebase and then compare it to the date of today.

To solve this, change the insurance property to hold a timestamp and then just get it from the database in the way you do and then simply compare it with:

Date date = new Date();
long now = date.getTime();
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
0

It is better to use timestamp for Date. But if you still use String then you should convert it into proper Date format by SimpleDateFormat like this.

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
    Date date = sdf.parse(insurance); //pass insurance date String here
    //Now compare this date to current date
Ved
  • 1,035
  • 14
  • 28