gpt4 book ai didi

java.time.DateTimeException : Unable to obtain LocalTime from TemporalAccessor: 2017-10-29 of type java. 时间.LocalDate

转载 作者:行者123 更新时间:2023-11-30 06:24:56 27 4
gpt4 key购买 nike

我试图将 datePicker 中的日期作为字符串保存到 mysql 表中。我使用 Javafx 场景生成器创建了 datePicker,但是当我尝试以字符串格式保存日期时,它返回此错误:

java.time.DateTimeException:无法从 TemporalAccessor 获取 LocalDateTime:2017-10-29 类型为 java.time.LocalDate

enter image description here

首先,我想将用户的输入数据添加到可观察列表中,然后将其显示在表格中给用户。之后,一旦用户完成添加他们想要的相关数据,我想将可观察列表数据插入到mysql数据库中。

我想以字符串的形式将 datePicker 数据添加到可观察列表中。

我的问题是如何将日期选择器的值更改为字符串并将其传递到可观察列表中。在我的代码中,我使用了 .getEditor().getText() 但它引发了上述错误。

我的日期选择器已标记

     fromDateCreateTask
toDateCreateTask

数据库中T_startDate和T_endDate列的数据类型为varchar(50)。

这是我的代码:

void addTaskToTable(){


AllTaskData.add(new Task(
Integer.parseInt(taskNumberCreateTask.getText()),
taskNameCreateTask.getText(),
getProjectId(projectSelectedCombobox.getValue()),
taskStatusCreateTask.getText(),taskInformationCreateTask.getText(),
(String) teamHeadComboBoxCreateTask.getValue(),
fromDateCreateTask.getEditor().getText(),
toDateCreateTask.getEditor().getText(),
timeSpan()));
createTaskTable.setItems(AllTaskData);

taskNameCreateTask.clear();
fromDateCreateTask.getEditor().clear();
toDateCreateTask.getEditor().clear();
teamHeadComboBoxCreateTask.setValue("Team Lead");
taskInformationCreateTask.clear();
taskCount++;

taskNumberCreateTask.setText(Integer.valueOf(getTotalNumberOfTasks() + 1+taskCount).toString());

}

添加到mysql表:

public void createMultipleTasks() {

String sql = " insert into Task (T_id, T_name, P_projectCode, T_status, T_Description, T_lead,T_startDate,T_endDate,T_timespan)"
+ " values (?, ?, ?, ?, ?,?,?,?,?)";

try {

for(Task userTasks: AllTaskData){

int taskNumber = userTasks.getTaskNumber();
String taskName = userTasks.getTaskName();
String taskStatus = userTasks.getTaskStatus();
String taskDescription = userTasks.getTaskInformation();
String teamLead = userTasks.getTaskTeamMemberAssigned();
String startingDate = userTasks.getTaskFromDate();
String endingingDate = userTasks.getTaskToDate();
int taskcompletionTime = userTasks.getTaskTimeSpan();
int projectId = userTasks.getProjectCode();//getProjectId(projectSelectedCombobox.getValue());



preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1, taskNumber);
preparedStatement.setString(2, taskName);
preparedStatement.setInt(3, projectId);
preparedStatement.setString(4, taskStatus);
preparedStatement.setString(5, taskDescription);
preparedStatement.setString(6, teamLead);
preparedStatement.setString(7, startingDate);
preparedStatement.setString(8,endingingDate);
preparedStatement.setInt(9, taskcompletionTime);

try{
preparedStatement.execute();
}catch (Exception e) {
DBConnection.infoBox("Error Saving Data", "Fail", null);
e.printStackTrace();
}
}
try {
DBConnection.infoBox("Click ok to cancel this message", "Sucess", "Saving of tasks has been successfull");

} catch (Exception e) {
DBConnection.infoBox("Error Unable to Open View", "Fail", null);
e.printStackTrace();
}

} catch (Exception e) {
DBConnection.infoBox("Error Saving Data", "Fail", null);
e.printStackTrace();
}

}

调用函数:

    @FXML
void handleAddNewTaskAction(ActionEvent event) {

if (taskNameCreateTask.getText()==null || taskNameCreateTask.getText()==" " || taskNameCreateTask.getText().isEmpty() ||
fromDateCreateTask.getValue() == null || toDateCreateTask.getValue() == null ||
teamHeadComboBoxCreateTask.getValue() == null || teamHeadComboBoxCreateTask.getValue().isEmpty() || teamHeadComboBoxCreateTask.getValue() == "Team Lead" ||
taskInformationCreateTask.getText() == null || taskInformationCreateTask.getText().isEmpty() || taskInformationCreateTask.getText() == " "
){
DBConnection.infoBox("Please enter text in missing fields", "Error Adding Data", "nil");
}else{

addTaskToTable();

}
}

最佳答案

在处理数据库日期时,您可以使用 java.sql.Date 或 java.sql.Timestamp。请引用下文。

public class JavaDateExceptionResolver {

public static void main(String[] args) throws ParseException {
dateIssueResolverType1("2017-10-29");
dateIssueResolverType2("2017-10-29");
}

private static Date dateIssueResolverType1(String input) throws ParseException {

SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
java.util.Date dt=sdf.parse(input);
java.sql.Date sqlDate=new java.sql.Date(dt.getTime());
System.out.println(sqlDate);
return sqlDate;
}

private static Timestamp dateIssueResolverType2(String input) throws ParseException {
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
java.sql.Timestamp stmp=new java.sql.Timestamp(sdf.parse(input).getTime());
System.out.println(stmp);
return stmp;
}
}

关于java.time.DateTimeException : Unable to obtain LocalTime from TemporalAccessor: 2017-10-29 of type java. 时间.LocalDate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47374873/

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