如何通过 Hibernate 以 UTC 格式存储时间戳?
how to store timestamp in UTC format through Hibernate?
目前我正在根据 sysdate 存储时间戳,但我想以 UTC 格式保留时间戳。 PFB 在我的实体 class 中的代码片段。
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "CREATE_DT")
private Date createDt = new Date();
如何以 UTC 格式存储时间戳。
只需将 JDBC 选项更改为您的 JDBC 连接 URL:
这些是您需要更改的参数:
useTimezone=true
serverTimezone=UTC
示例:
<bean id="HB_SessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="hibernateProperties">
<props>
<!-- Connection parameters -->
<prop key="hibernate.connection.driver_class">com.mysql.jdbc.Driver</prop>
<prop key="hibernate.connection.url">jdbc:mysql://hostname/databaseName?useTimezone=true&serverTimezone=UTC</prop>
...
目前我正在根据 sysdate 存储时间戳,但我想以 UTC 格式保留时间戳。 PFB 在我的实体 class 中的代码片段。
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "CREATE_DT")
private Date createDt = new Date();
如何以 UTC 格式存储时间戳。
只需将 JDBC 选项更改为您的 JDBC 连接 URL:
这些是您需要更改的参数:
useTimezone=true
serverTimezone=UTC
示例:
<bean id="HB_SessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="hibernateProperties">
<props>
<!-- Connection parameters -->
<prop key="hibernate.connection.driver_class">com.mysql.jdbc.Driver</prop>
<prop key="hibernate.connection.url">jdbc:mysql://hostname/databaseName?useTimezone=true&serverTimezone=UTC</prop>
...