gpt4 book ai didi

java - 字符串到 Localdate DateTimeParseException

转载 作者:行者123 更新时间:2023-12-02 10:36:21 26 4
gpt4 key购买 nike

我正在从 SQLite 数据库加载一个字符串,然后尝试将其转换为 LocalDate,以便我可以将其添加为对象参数,然后比较列表中对象的日期。

但是应用程序崩溃并给出 DateTimeParseException。

这是崩溃的方法,它在我创建 LocalDate 的行处崩溃。

我在这个网站和其他网站上找到并阅读了许多类似的帖子,但遗憾的是我无法弄清楚问题到底是什么。

private void fillEventsArray(){
Cursor data = mDatabaseHelper.getData();
while(data.moveToNext()){
String name = data.getString(1);
String description = data.getString(2);
String time = data.getString(3);
String date = data.getString(4);

LocalDate localDate= LocalDate.parse(date, DateTimeFormatter.ofPattern("yyyy/MM/dd"));

LocalTime localTime=LocalTime.parse(time, DateTimeFormatter.ofPattern("HH:mm"));

Event event = new Event(name, description, localTime, localDate);

events.add(event);
}

我以相同的“yyyy/mm/dd”格式保存第 4 列中的数据,例如 2018/11/20。

之前我也尝试过像这样将字符串转换为LodalDate/LocalTime,但也有同样的问题:

                String year=date.substring(0,4);
String month=date.substring(5,7);
String day=date.substring(8,10);
LocalDate localDate=LocalDate.of(Integer.parseInt(year),Integer.parseInt(month),Integer.parseInt(day));
String hour=time.substring(0,3);
String minute=time.substring(4,5);
LocalTime localTime=LocalTime.of(Integer.parseInt(hour),Integer.parseInt(minute));

如果数据库为空会导致错误吗?

如果需要更多信息,请告诉我,我会提供。

Logcat 副本:

2018-11-12 15:48:57.836 12038-12038/com.example.dflavio.calendar E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.dflavio.calendar, PID: 12038
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.dflavio.calendar/com.example.dflavio.calendar.MainActivity}: java.time.format.DateTimeParseException: Text 'android.support.v7.widget.AppCompatEditText{ceb966d VFED..CL. .F...' could not be parsed at index 0
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: java.time.format.DateTimeParseException: Text 'android.support.v7.widget.AppCompatEditText{ceb966d VFED..CL. .F...' could not be parsed at index 0
at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1948)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
at java.time.LocalDate.parse(LocalDate.java:394)
at com.example.dflavio.calendar.MainActivity.fillEventsArray(MainActivity.java:104)
at com.example.dflavio.calendar.MainActivity.onCreate(MainActivity.java:45)
at android.app.Activity.performCreate(Activity.java:6975)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) 
at android.app.ActivityThread.-wrap11(Unknown Source:0) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) 
at android.os.Handler.dispatchMessage(Handler.java:105) 
at android.os.Looper.loop(Looper.java:164) 
at android.app.ActivityThread.main(ActivityThread.java:6541) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 

正如有人指出的那样,我在如何从编辑文本中获取字符串方面可能犯了一个错误。目前,代码如下所示:

createBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

EditText nameEditText = (EditText) findViewById(R.id.nameEditText);
EditText descriptionEditText = (EditText) findViewById(R.id.descriptionEditText);
EditText timeEditText = (EditText) findViewById(R.id.timeEditText);
EditText dateEditText = (EditText) findViewById(R.id.dateEditText);

String name = nameEditText.getText().toString();
String description = descriptionEditText.getText().toString();
String time = timeEditText.getText().toString();
String date = dateEditText.getText().toString();
if (name.length() != 0){
if (description.length() != 0){
if (time.length() != 0){
if (date.length() != 0){
AddData(name, description, time, date);
nameEditText.setText("");
descriptionEditText.setText("");
timeEditText.setText("");
dateEditText.setText("");
}
}
}
}else{
toastMessage("Fill in all fields.");
}

感谢您提前提供的帮助。

最佳答案

您的数据库似乎包含垃圾;您尝试将“android.support.v7.widget.AppCompatEditText{ceb966d VFED..CL..F...”之类的字符串解析为日期。

关于java - 字符串到 Localdate DateTimeParseException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53264444/

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