Spring 将字符串转换为LocalDateTime

Spring 将字符串转换为LocalDateTime,spring,postgresql,hibernate,spring-boot,spring-security,Spring,Postgresql,Hibernate,Spring Boot,Spring Security,我在注册任务时出错,因为时间必须从String转换为LocalDateTime,我不知道如何转换 下面是我的任务课程: @Entity @Table(name = "task", schema = "public") public class Task { @Id @GeneratedValue private Long id; @NotEmpty private String date; @NotEmpty private Loca

我在注册任务时出错,因为时间必须从
String
转换为
LocalDateTime
,我不知道如何转换

下面是我的
任务
课程:

@Entity
@Table(name = "task", schema = "public")
public class Task {

    @Id
    @GeneratedValue
    private Long id;

    @NotEmpty
    private String date;

    @NotEmpty
    private LocalDateTime startTime;

    @NotEmpty
    private LocalDateTime stopTime;

    @NotEmpty
    @Column(length = 1000)
    private String description;

    @ManyToOne
    @JoinColumn(name = "USER_EMAIL")
    private User user;

    public Task() {
    }

    public Task(String date, LocalDateTime startTime, LocalDateTime stopTime, String description, User user) {
        this.date = date;
        this.startTime = startTime;
        this.stopTime = stopTime;
        this.description = description;
        this.user = user;
    }

    public Task(String date, LocalDateTime startTime, LocalDateTime stopTime, String description) {
        this.date = date;
        this.startTime = startTime;
        this.stopTime = stopTime;
        this.description = description;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public LocalDateTime getStartTime() {
        return startTime;
    }

    public void setStartTime(LocalDateTime startTime) {
        this.startTime = startTime;
    }

    public LocalDateTime getStopTime() {
        return stopTime;
    }

    public void setStopTime(LocalDateTime stopTime) {
        this.stopTime = stopTime;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }
}
TaskController
类中,I具有在数据库中注册任务的功能:

@PostMapping("/addTask")
public String addTask(@Valid Task task,
                      BindingResult bindingResult,
                      HttpSession session,
                      @RequestParam("datetime")
                      @DateTimeFormat(pattern = "dd.MM.yyyy HH:mm:ss.SSSZ") LocalDateTime dateAndTime) {

    if (bindingResult.hasErrors()){
        return "views/taskForm";
    }

    String email = (String) session.getAttribute("email");
    taskService.addTask(task, userService.findOne(email));

    return "redirect:/users";
}
TaskService
中,是在
TaskController
中添加任务的功能:

public void addTask(Task task, User user) {
    task.setUser(user);
    taskRepository.save(task);
}

请某人解决此错误:

无法将java.lang.String类型的属性值转换为必需的 键入属性startTime的java.time.LocalDateTime;嵌套异常 是org.springframework.core.convert.ConversionFailedException:失败 将[java.lang.String]类型转换为 [@org.hibernate.validator.constraints.NotEmpty java.time.LocalDateTime]表示值09:00;嵌套异常是 java.lang.IllegalArgumentException:解析值的尝试失败 [09:00]

这是:

无法将java.lang.String类型的属性值转换为必需的 键入java.time.LocalDateTime作为属性stopTime;嵌套异常 是org.springframework.core.convert.ConversionFailedException:失败 将[java.lang.String]类型转换为 [@org.hibernate.validator.constraints.NotEmpty java.time.LocalDateTime]表示值18:00;嵌套异常是 java.lang.IllegalArgumentException:解析值的尝试失败 [18:00]

我已经花了很多天,我无法解决这个问题


提前谢谢

使用下面的命令将时间从字符串转换为LocalDateTime,但请确保以字符串形式获取时间

String str = "2018-12-10 12:30";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
LocalDateTime dateTime = LocalDateTime.parse(str, formatter);

您可以编写一个转换器,将
字符串
转换为
LocalDateTime
,反之亦然

@Converter(autoApply = true)
public class LocalDateTimeConverter implements AttributeConverter<LocalDateTime, String> {

    private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    @Override
    public String convertToDatabaseColumn(LocalDateTime locDate) {
        return (locDate == null ? null : formatter.format(locDate));
    }

    @Override
    public LocalDateTime convertToEntityAttribute(String dateValue) {
        return (dateValue == null ? null : LocalDateTime.parse(dateValue, formatter));
    }
}

您输入的
dateAndTime
字符串是什么样子的?@mrkernelpanic看起来像mm:HH AM/pm为什么要从问题中删除请求参数??顺便说一句,我指的是
dateAndTime
@mrkernelpanic的实际值,我把它再说一遍,因为需求中的模式与我在前端使用的模式不同。只要发送“10/11/2018 09:00”,您的问题就会解决。不能将没有日期的字符串时间转换为LocalDateTime。如果您已经将startTime和stopTome作为LocalDateTime,那么为什么需要列date?在哪个类中?在任何实用程序类中使用它创建一个方法,使用上面的代码并将其公开为公共静态。例如,我将尝试将其作为类本身。如果
(自动应用=真)
已设置JPA将转换所有属性(如果没有)。您必须如上所述在变量上指定转换器。我使用该类,还为我要使用的每个变量定义了转换器,但我给出了错误:由处理程序执行引起的已解决异常:org.springframework.web.bind.MissingServletRequestParameterException:所需的LocalDateTime参数“datetime”不存在。看起来与原始问题不同。这个问题似乎指向您的Rest端点映射
@RequestParam(“datetime”)
。我仍然不理解这个问题
@Convert(converter = LocalDateTimeConverter.class)
private LocalDateTime stopTime;