首页 > 解决方案 > 当我从我的 sql 到我的 spring-boot 应用程序时,为什么我的 LocalDateTime 变量改变了 5 小时?

问题描述

每次通过查询我的 sql 表得到它时,我的 LocalDateTime 变量都会更改 5 小时。如果我有一个从上午 9:30 开始的时间,那么它将返回上午 4:30 作为时间。 每当我尝试通过在我的 repo 中查询来从我的 sql 表中获取日期时间类型字段时

    @Query(value = "SELECT start FROM course_section WHERE crn = ?1", nativeQuery = true)
    LocalDateTime getStartTimeByCrn(int term);
    
    @Query(value = "SELECT end FROM course_section WHERE crn = ?1", nativeQuery = true)
    LocalDateTime getEndTimeByCrn(int term);

我将其发送给我的服务

//Method to get the Start time for Section class
public LocalDateTime getStartTimeByCrn(int term) {
    LocalDateTime start = (LocalDateTime) repo.getStartTimeByCrn(term);
    return start;
}
//Method to get the End time for the Section class
public LocalDateTime getEndTimeByCrn(int term) {
    LocalDateTime end = (LocalDateTime) repo.getEndTimeByCrn(term);
    return end;

我的控制器使用这些方法

@RequestMapping(value = "/accepted", method = RequestMethod.POST)
    public String addSchedule(@ModelAttribute("schedule") Schedule schedule) throws JsonProcessingException {
    
        int crn = schedule.getCrn();
        
        Section sec = new Section();
        
        //set all the attributes of Section object 
        
        sec.setStart(service.getStartTimeByCrn(crn));
        sec.setEnd(service.getEndTimeByCrn(crn));
        
//      String stringToJson = new JsonMapper().writeValueAsString(sec);
        
        service.save(sec);
        
        System.out.print( sec.getStart() + " is the subject!!!"); 
        return "calendar";
    }

当我从我的 sql 表中通过 crn 获取它时,我的 datetime 变量将连续更改五个小时。其他都没有改变,年、月、日、分或秒都没有改变。

我的 @Entity LocalDateTime 变量看起来像

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss")
private LocalDateTime start; 

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss")
private LocalDateTime end;

发生此错误似乎没有合乎逻辑的理由...

标签: sqlspring-bootdatetimejacksontimezone

解决方案


推荐阅读