gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 10:09:42 25 4
gpt4 key购买 nike

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

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


}

最佳答案

您可以通过这种方式转换时间戳。

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

关于java - 无法将 java.lang.String 类型的值转换为 Date(在字段 'time_stamp' 中找到),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55087185/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com