无法将类型java.lang.String的值转换为Date(在“ time_stamp”字段中找到)

时间:2019-03-10 11:26:36

标签: java android timestamp google-cloud-firestore

我正在尝试在TextView上显示Firestore的时间戳。 我使用转换器将时间戳转换为较长时间。

 long millisecond = question_list.get(position).getTime_stamp().getTime();
    String dateString = new SimpleDateFormat("MM/dd/yyyy").format(new Date(millisecond)).toString();
    holder.setTime(dateString);

但是它给了我致命的致命主体:

 java.lang.RuntimeException: Could not deserialize object. Failed to convert value of type java.lang.String to Date (found in field 'time_stamp')
                                                                     at com.google.android.gms.internal.zzevb.zzb(Unknown Source:79)
                                                                     at com.google.android.gms.internal.zzevb.zza(Unknown Source:719)
                                                                     at com.google.android.gms.internal.zzevb.zza(Unknown Source:355)
                                                                     at com.google.android.gms.internal.zzevb.zzb(Unknown Source:0)
                                                                     at com.google.android.gms.internal.zzevb$zza.zza(Unknown Source:139)
                                                                     at com.google.android.gms.internal.zzevb.zza(Unknown Source:1025)
                                                                     at com.google.android.gms.internal.zzevb.zza(Unknown Source:2)
                                                                     at com.google.firebase.firestore.DocumentSnapshot.toObject(Unknown Source:10)
                                                                     at com.example.dell.step.QuestionsFragment$1.onEvent(QuestionsFragment.java:81)
                                                                     at com.example.dell.step.QuestionsFragment$1.onEvent(QuestionsFragment.java:72)
                                                                     at com.google.firebase.firestore.zzi.onEvent(Unknown Source:17)
                                                                     at com.google.android.gms.internal.zzevc.zza(Unknown Source:6)
                                                                     at com.google.android.gms.internal.zzevd.run(Unknown Source:6)
                                                                     at android.os.Handler.handleCallback(Handler.java:873)
                                                                     at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                     at android.os.Looper.loop(Looper.java:193)
                                                                     at android.app.ActivityThread.main(ActivityThread.java:6669)
                                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                                     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

QuestionsFragment.java:81是这个

 for (DocumentChange doc : documentSnapshots.getDocumentChanges()) {

                if (doc.getType() == DocumentChange.Type.ADDED) {


                    QuestionPost questionPost = doc.getDocument().toObject(QuestionPost.class);
                    question_list.add(questionPost);

                    questionRecyclerAdapter.notifyDataSetChanged();
                }


            }

和QuestionsFragment.java:72是这个

firebaseFirestore.collection("questions").addSnapshotListener(new EventListener<QuerySnapshot>() {
        @Override
        public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {

            for (DocumentChange doc : documentSnapshots.getDocumentChanges()) {

                if (doc.getType() == DocumentChange.Type.ADDED) {


                    QuestionPost questionPost = doc.getDocument().toObject(QuestionPost.class);
                    question_list.add(questionPost);

                    questionRecyclerAdapter.notifyDataSetChanged();
                }


            }

1 个答案:

答案 0 :(得分:0)

您可以通过这种方式隐藏时间戳。

  long millisecond = question_list.get(position).getTime_stamp().getTime();      

  SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy"); // your date format

  StringBuilder dateString= new StringBuilder(dateFormat.format(millisecond ));

  holder.setTime(dateString);