JAVA中toLocalleString,将String转换为LocalDateTime

您好我在任务注册时出错,因为时间必须从 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中注册任务

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

}

Plase可以有人解决此错误

Failed to convert property value of type java.lang.String to required type java.time.LocalDateTime for property startTime; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@org.hibernate.validator.constraints.NotEmpty java.time.LocalDateTime] for value 09:00; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [09:00]

Failed to convert property value of type java.lang.String to required type java.time.LocalDateTime for property stopTime; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@org.hibernate.validator.constraints.NotEmpty java.time.LocalDateTime] for value 18:00; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [18:00]

因为我有很多天我无法解决 .

提前致谢!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值