小编典典

Hibernate在将Java Calendar对象读取和写入SQL TIMESTAMP时使用什么时区?

hibernate

Hibernate Java
Calendar对象
写入 SQL TIMESTAMP列时,它将日期,计算机日期或日历对象(或其他日期)中指定的日期调整到哪个时区?

当Hibernate TIMESTAMP到日历对象,以哪个时区并把它翻译的日期?


阅读 341

收藏
2020-06-20

共1个答案

小编典典

当Hibernate将Java Calendar对象写入SQL
TIMESTAMP列时,它将日期,计算机日期或Calendar对象(或其他日期)中指定的日期调整到哪个时区?

Hiberante
3.x在CalendarType(请参阅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)哪个使用日历的时区。

当Hibernate将TIMESTAMP读入日历对象时,它将日期转换到哪个时区?

好了,再次让我们看一下这个CalendarType类:

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
*_。

2020-06-20