oracle.sql.TIMESTAMP 处理日期 [转]

个人觉得Oracle有些“独裁”在日期类型对象方面,自己的JDBC驱动不兼顾JDK的标准类。
我想好多朋友遇到过此类问题。这里不再详述。

个人的解决方案:

private String getDate(Object value) {

Timestamp timestamp = null;
try {
timestamp = (Timestamp) value;
} catch (Exception e) {
timestamp = getOracleTimestamp(value);
}
if(timestamp!=null)
return (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S")) .format(timestamp);
else return null;
}
/**
     * @reference oracle.sql.Datum.timestampValue();
     * @return
     */
private Timestamp getOracleTimestamp(Object value) {
try {
Class clz = value.getClass();
Method m = clz.getMethod("timestampValue", null);
                       //m = clz.getMethod("timeValue", null); 时间类型
                       //m = clz.getMethod("dateValue", null); 日期类型
return (Timestamp) m.invoke(value, null);

} catch (Exception e) {
return null;
}
}



出自 http://xuechenyoyo.iteye.com/blog/373318


jdk 1.6 + oracle11g 连接时,从数据库取出数据是oracle.sql.TIMESTAMP对象。不方便在sql语句中转换,用上面的方法转换成功了。

猜你喜欢

转载自yubolg.iteye.com/blog/1953709