将字符串转换为LocalDateTime

时间:2018-12-10 11:19:31

标签: spring postgresql hibernate spring-boot spring-security

您好,我在任务注册中存在错误,因为时间必须从 String 转换为 LocalDateTime ,而且我不知道如何转换。下面显示了 Task 类。

@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 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;
 }

 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 Task() {
 }

}

TaskController 类中,我具有用于在db中注册Tasks的功能

@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的属性值转换为属性stopTime所需的类型java.time.LocalDateTime;嵌套异常是org.springframework.core.convert.ConversionFailedException:无法从类型[java.lang.String]转换为类型[@ org.hibernate.validator.constraints.NotEmpty java.time.LocalDateTime]的值18:00;嵌套异常为java.lang.IllegalArgumentException:解析尝试失败,值[18:00]

因为我有很多日子无法解决。

谢谢!

2 个答案:

答案 0 :(得分:2)

您可以编写一个转换器,以将String转换为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));
    }
}

如果您想为每个变量定义一个转换器

@Convert(converter = LocalDateTimeConverter.class)
private LocalDateTime stopTime;

答案 1 :(得分:1)

使用下面的方法将时间从String转换为LocalDateTime,但请确保您以String形式获取时间。

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