Oracle / JDBC: retrieving TIMESTAMP WITH TIME ZONE value in ISO 8601 format

前端 未结 7 1948
抹茶落季
抹茶落季 2020-12-13 10:58

A lot have been said (and written on SO) on parts of the subject, but not in a comprehensive, complete way, so we can have one \"ultimate, covering-it-all\" solution for eve

7条回答
  •  醉梦人生
    2020-12-13 11:40

    This is untested, but seems like it ought to be a workable approach. I'm not sure about parsing the TZ name out, but just treating the two parts of the TZTZ object as separate inputs to Calendar seems like the was to go.

    I'm not sure whether longValue() will return the value in local or GMT/UCT. If it's not GMT, you should be able to load a calendar as UTC and ask it for a Calendar converted to local TZ.

    public Calendar toCalendar(oracle.sql.TIMESTAMPTZ myOracleTime) throws SQLException {
        byte[] bytes = myOracleTime.getBytes();
        String tzId = "GMT" + ArrayUtils.subarray(bytes, ArrayUtils.lastIndexOf(bytes, (byte) ' '), bytes.length);
        TimeZone tz = TimeZone.getTimeZone(tzId);
        Calendar cal = Calendar.getInstance(tz);
        cal.setTimeInMillis(myOracleTime.longValue());
        return cal;
    }
    

提交回复
热议问题