在Recyclerview Android中从Firebase检索日期

不飞则已,一飞冲天;不鸣则已,一鸣惊人。这篇文章主要讲述在Recyclerview Android中从Firebase检索日期相关的知识,希望能为你提供帮助。
我想创建一个程序,显示Firebase实时数据库中引入的日期。目前我的程序显示的是三个字符串,这些变量是RecyclerView中的字符串。我想获取Firebase中引入的日期,然后将其与今天的日期进行比较。
我告诉你我的代码:
主要活动:

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); } }

}
气球课。在这段代码中,我添加了对象保险,这是一个日期,但不确定它是否正确:
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; }

在firebase中,我的JSON文档如下所示:here
谢谢:)
答案最好使用Date的时间戳。但是,如果您仍然使用String,那么您应该像SimpleDateFormat一样将其转换为正确的日期格式。
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

另一答案
目前,我的程序显示了三个变量,它们是RecyclerView中的字符串。
实际上,这不是您应该如何在Firebase实时数据库中存储时间戳。添加时间戳的正确方法在我从这个post的答案中解释,而不是像你现在那样作为字符串。
我想获取Firebase中引入的日期,然后将其与今天的日期进行比较。
【在Recyclerview Android中从Firebase检索日期】要解决此问题,请更改insurance属性以保存时间戳,然后以您的方式从数据库中获取它,然后将其与以下内容进行比较:
Date date = new Date(); long now = date.getTime();


    推荐阅读