2

My Firestore date format

FireStore Date Format

In My fragment I get the date in this way

Query query = fb.collection("jobs")
                .orderBy("date", Query.Direction.ASCENDING);
 protected void onBindViewHolder(@NonNull JobsViewHolder holder, int position, @NonNull JobsLists jobsmodel) {
                holder.setDate(jobsmodel.getDate());
...
    public void setDate(Date date) {
                da =  view.findViewById(R.id.date);
                SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
                String dateString = sdf.format(date);
                da.setText(dateString);
            }

My Job class

    private Date date;
    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

When I insert the date in FireStore I put it this way

String date_val= new SimpleDateFormat("yyyy-MM-dd",Locale.getDefault()).format(new Date());  
      jobs.put("date",date_val);

Please help me! What should I change?

ora
  • 79
  • 9

1 Answers1

1

You don't have to transform date to a format before saving, just simply pass the date object it will be saved to the database:

  jobs.put("date",new Date());

This will make it easier for firebase to parse it when trying to retrieve the data from the database.

Levi Moreira
  • 11,917
  • 4
  • 32
  • 46
  • full logcat:` java.lang.RuntimeException: Could not deserialize object. Failed to convert value of type java.lang.String to Date (found in field 'date') at com.google.android.gms.internal.zzeym.zzb(Unknown Source) at com.google.android.gms.internal.zzeym.zza(Unknown Source) ` – ora Apr 13 '18 at 19:12
  • did you delete what you had in the database? – Levi Moreira Apr 13 '18 at 19:12
  • same prb I think ,even when I totally deleted the date field in my db ,it continued to occur,then sudenly it stopped ,maybe it needs time to update.How can I update this faster? – ora Apr 13 '18 at 19:35
  • One more thing If you could give me some advice on how I get news from websites and display them into a fragment? – ora Apr 13 '18 at 19:43