What time zone does Hibernate use when it reads and writes a Java Calendar object to an SQL TIMESTAMP?(Hibernate 在读取 Java 日历对象并将其写入 SQL TIMESTAMP 时使用哪个时区?)
问题描述
当 Hibernate 写 Java Calendar
对象到 SQL TIMESTAMP
列,它调整到哪个时区日期,计算机的日期或日历对象(或其他)中指定的日期?
When Hibernate writes a Java Calendar
object to an SQL TIMESTAMP
column, to which time zone does it adjust the date, that of the computer or that specified in the calendar object (or some other)?
当 Hibernate 读取 TIMESTAMP
到日历对象中时,它将日期转换到哪个时区?
When Hibernate reads the TIMESTAMP
into the calendar object, to which time zone does it translate the date?
推荐答案
当 Hibernate 将 Java 日历对象写入 SQL TIMESTAMP 列时,它将日期调整到哪个时区,计算机的日期或日历对象(或其他)中指定的日期?
When Hibernate writes a Java Calendar object to an SQL TIMESTAMP column, to which time zone does it adjust the date, that of the computer or that specified in the calendar object (or some other)?
Hiberante 3.x 在 CalendarType
中使用以下内容(参见 HB-1006):
Hiberante 3.x uses the following in the CalendarType
(see HB-1006):
public void set(PreparedStatement st, Object value, int index) throws HibernateException, SQLException {
final Calendar cal = (Calendar) value;
//st.setTimestamp( index, new Timestamp( cal.getTimeInMillis() ), cal ); //JDK 1.5 only
st.setTimestamp( index, new Timestamp( cal.getTime().getTime() ), cal );
}
所以 Hibernate 使用 PreparedStatement#setTimestamp(int, Timestamp, Calendar)
使用日历的时区.
So Hibernate uses PreparedStatement#setTimestamp(int, Timestamp, Calendar)
which uses the time zone of the calendar.
当 Hibernate 将 TIMESTAMP 读入日历对象时,它将日期转换到哪个时区?
When Hibernate reads the TIMESTAMP into the calendar object, to which time zone does it translate the date?
好吧,我们再来看看 CalendarType
类:
Well, again, let's look at the CalendarType
class:
public Object get(ResultSet rs, String name) throws HibernateException, SQLException {
Timestamp ts = rs.getTimestamp(name);
if (ts!=null) {
Calendar cal = new GregorianCalendar();
if ( Environment.jvmHasTimestampBug() ) {
cal.setTime( new Date( ts.getTime() + ts.getNanos() / 1000000 ) );
}
else {
cal.setTime(ts);
}
return cal;
}
else {
return null;
}
}
因此,Hibernate 使用默认时区和默认语言环境中的当前时间构造一个默认GregorianCalendar
.
So Hibernate constructs a default GregorianCalendar
using the current time in the default time zone with the default locale.
作为旁注,我强烈建议阅读以下问题:
As a side note, I highly suggest to read the following question:
- 夏令时和时区最佳做法
这篇关于Hibernate 在读取 Java 日历对象并将其写入 SQL TIMESTAMP 时使用哪个时区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Hibernate 在读取 Java 日历对象并将其写入 SQL TIMESTAMP 时使用哪个时区?


基础教程推荐
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 降序排序:Java Map 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01