如何转换为String到LocalTimeZone?

问题描述 投票:0回答:1

我正在开发一个应用程序,并在从String转换为LocalTimeZone时遇到问题。我已经通过以下两个屏幕截图显示了问题,以便更好地了解和理解:

enter image description here enter image description here比定义模型的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() {
 }
}

以下是在数据库中添加新任务的功能

@GetMapping("/addTask")
public String taskForm(String email, Model model, HttpSession session) {
    session.setAttribute("email", email);
    model.addAttribute("task", new Task());
    return "views/taskForm";
}
@PostMapping("/addTask")
public String addTask(@Valid Task task, BindingResult bindingResult, HttpSession session) {
    if(bindingResult.hasErrors()){
        return "views/taskForm";
    }
    String email = (String) session.getAttribute("email");
    taskService.addTask(task, userService.findOne(email));
    return "redirect:/users";
}

以下是我要求的这个任务所需的另一个类中的函数。

@Scheduled(cron = "0-59 * * * * ?")
public void cronTime() {
    logger.info("Cron Task : Execution Time - {}", dateTimeFormatter.format(LocalDateTime.now()));
    System.out.println("Cron job is executed again!");
    List<LocalDateTime> a = taskService.stopTimeTasks();
    for (LocalDateTime tmp : a) {
        if (LocalDateTime.now().isAfter(tmp)) {
            taskService.deleteAll();
        }
    }
}

如果您有关于此错误的信息,请帮助我。提前致谢!

spring hibernate spring-boot cron
1个回答
0
投票

您可以使用DateTimeFormatter,如下所示:

LocalDate localDate = LocalDate.from(DateTimeFormatter.ofPattern("dd/MM/yyyy").parse("10/11/2018");

LocalTime localTime = LocalTime.from(DateTimeFormatter.ofPattern("HH:mm").parse("09:00");

LocalDateTime locaDateTime = LocalDateTime.of(localDate, localTime);

我建议你更改域模型,接受startD的LocalDateTime和日期的stopTime。此时您将保存“10/11/2018 09:00”和“10/11/2018 18:00”,在日期之间应用sql过滤器会更加容易。我看到不必要的日期列。

© www.soinside.com 2019 - 2024. All rights reserved.