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

时间:2017-11-19 08:12:16

标签: java javafx datepicker

我正在尝试将日期从datePicker保存到mysql表中作为String.I使用Javafx场景构建器创建了datePicker但是当我尝试以String的格式保存日期时,它返回此错误:

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

enter image description here

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

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

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

我的datePickers标有

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

    }
}

1 个答案:

答案 0 :(得分:0)

使用数据库日期时,可以使用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;
}       
}