mysql - 如何使用 Java 和 spring/hibernate 在我的数据库中正确插入日期(在我的数据库中得到错误的时间)?

标签 mysql spring hibernate datetime calendar

最近,我一直在项目中使用日历和日期数据类型,但在将正确的小时数插入(和检索)我的数据库(使用 Spring 和 Hibernate/MySql5Dialect)时遇到问题。

当我尝试在 Spring 应用程序中使用存储过程从数据库中选择一些日期(DATETIME 数据类型)时,Java 中返回的对象始终比数据库中的实际日期早一小时(例如,2019-03) -30 09:00:00 在我的数据库中,我从我的服务返回 2019-03-30 08:00:00 )。现在,当我在 mysql workbench 中调用相同的过程时,它可以完美运行。

现在我注意到,当我尝试从 Spring 应用程序插入一 strip 有日期的记录时,例如 2019-03-30 09:00:00,我在数据库中得到 2019-03-30 08:00:00 ,这是我插入日期的方法:

private PatientAppointmentDTOR createPatientAppointmentDTOR() {
        PatientAppointmentDTOR dtor = factory.manufacturePojo(PatientAppointmentDTOR.class);
        dtor.setCpId(5L); // foreign keys...
        dtor.setBranchId(2L);
        dtor.setPtId(32L);
        dtor.setEmployeeId(55L);
        dtor.setInDepartmentId(7L);
        dtor.setPatientAppointmentStatusId(20L);
        Calendar cal1 = Calendar.getInstance();
        cal1.set(Calendar.YEAR, 2019);
        cal1.set(Calendar.MONTH, 2);
        cal1.set(Calendar.DAY_OF_MONTH,30);
        cal1.set(Calendar.HOUR_OF_DAY, 12);
        cal1.set(Calendar.MINUTE, 0);
        cal1.set(Calendar.SECOND, 0);
        cal1.set(Calendar.MILLISECOND, 0);
        Date start = cal1.getTime();
        Calendar cal2 = Calendar.getInstance();
        cal2.set(Calendar.YEAR, 2019);
        cal2.set(Calendar.MONTH, 2);
        cal2.set(Calendar.DAY_OF_MONTH,30);
        cal2.set(Calendar.HOUR_OF_DAY, 12);
        cal2.set(Calendar.MINUTE, 30);
        cal2.set(Calendar.SECOND, 0);
        cal2.set(Calendar.MILLISECOND, 0);
        Date end = cal2.getTime();
        dtor.setStartTime(start);
        dtor.setEndTime(end);
        Company company = companyService.getEntityById(5L);
        GroupDTOS gdtos = groupService.getDTOById(2L);
        when(auth.userDetails())
         .thenReturn(EntityFactory
         .createUserDetails(DBStaticFields.Role.CLINIC_ADMIN, company, gdtos, factory));
        return dtor;
    }

然后我使用 JUnit 测试将其插入到我的数据库中:

    @Test
    @DisplayName("test insertion")
    void testInsert() {
        PatientAppointmentDTOR dtor = this.createPatientAppointmentDTOR();
        PatientAppointmentDTOS dtos = patientAppointmentService.insert(dtor);
        assertThat(dtos).isNotNull();
        assertThat(dtos.getId()).isNotNull();
    }

所以,是的,当我从应用程序中插入这些值时,数据库中的 startDate 为 2019-03-30 11:00:00,endDate 为 2019-03-30 11:30:00。有人知道吗?

最佳答案

正如 akuma8 指出的那样,问题出在我的应用程序属性中设置的时区。如果有人遇到类似的问题,只需在连接数据库的应用程序属性中设置正确的时区“&serverTimezone=$$$”,现在一切都正确匹配。

谢谢akuma8!

关于mysql - 如何使用 Java 和 spring/hibernate 在我的数据库中正确插入日期(在我的数据库中得到错误的时间)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58524999/

相关文章:

php - 在 Homestead 不清楚的全新 Laravel 安装中连接到 MySQL

java - 无法使用 Spring Boot 和 Hibernate 连接到 Oracle

Mysql查询非常慢,但是重启后工作很快(CPU使用率99%)

php - 检查IP地址是否存储在数据库中

java - Spring Boot @EnableTransactionManagement(mode = AdviceMode.ASPECTJ) 不起作用

java - Jpa 事务 javax.persistence.RollbackException : Transaction marked as rollbackOnly

java - 服务器时区值 'AEST' 无法识别或代表多个时区

java - 在 JPA 实体内实例化 JavaFX 属性会导致 "Unable to load class"错误

hibernate - 如果我们有 session.evict(),为什么我们需要 javax.persistence.EntityManager.detach()?

mysql - SELECT 查询从每组返回 1 行