Hibernate在读取和写入Java日历对象到SQL TIMESTAMP时使用什么时区? [英] What time zone does Hibernate use when it reads and writes a Java Calendar object to an SQL TIMESTAMP?
问题描述
当 Hibernate 写入 Java Calendar
对象到SQL TIMESTAMP
列,它调整日期,计算机的日期或日历对象(或其他)中指定的日期?
当Hibernate 将 TIMESTAMP
读取到日历对象中,并将日期转换为哪个时区?
Hiberante 3.x在 Calendnd中使用以下内容arType
(请参阅 HB-1006 ) :
public void set(PreparedStatement st,Object value,int index)throws HibernateException,SQLException {
final Calendar cal =(日历)值;
//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)
,它使用日历的时区。
再次,当Hibernate将TIMESTAMP读入日历对象时,它会将日期转换为哪个时区?让我们看看 CalendarType
类:
public Object get(ResultSet rs ,String name)抛出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);
}
返回cal;
}
else {
return null;
$ b
所以Hibernate 构造一个使用缺省语言环境缺省时区中的当前时间使用默认 作为一个附注,我强烈建议阅读以下问题: When Hibernate writes a Java When Hibernate reads the 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 uses the following in the So Hibernate uses When Hibernate reads the TIMESTAMP into the calendar object, to which time zone does it translate the date? Well, again, let's look at the So Hibernate constructs a default As a side note, I highly suggest to read the following question: 这篇关于Hibernate在读取和写入Java日历对象到SQL TIMESTAMP时使用什么时区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! GregorianCalendar
。
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)?TIMESTAMP
into the calendar object, to which time zone does it translate the date?
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 );
}
PreparedStatement#setTimestamp(int, Timestamp, Calendar)
which uses the time zone of the calendar.
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;
}
}
GregorianCalendar
using the current time in the default time zone with the default locale.