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

前端 未结 7 1947
抹茶落季
抹茶落季 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:21

    you need two values: time utc in millis since 1970 and timezone offset fom utc.
    So store them as a pair and forward them as a pair.

    class DateWithTimeZone {
    long timestampUtcMillis;
    // offset in seconds
    int tzOffsetUtcSec;
    }
    

    A Date is a pair of numbers. It is not a String. So a machine interface should not contain a date represented by a iso string, although that is handy to debug. If even java cannot parse that iso date, how do you think that your clients can do?

    If you design an interface to your clients, think how they can parse that. And in advance write a code that shows that.

    0 讨论(0)
  • 2020-12-13 11:27

    Do you really care about sub-millisecond precision? If not converting from a UTC millisecond + timezone-offset to your required string is a one-liner using joda-time:

        int offsetMillis = rs.getInt(1);
        Date date = rs.getTimestamp(2);
    
        String iso8601String =
                ISODateTimeFormat
                        .dateTime()
                        .withZone(DateTimeZone.forOffsetMillis(offsetMillis))
                        .print(date.getTime());
    

    Prints, for example (current time in +9:00):

    2013-07-18T13:05:36.551+09:00
    

    Regarding the database: Two columns, one for the offset, one for the date. The date column could be an actual date type (thus making many, timezone-independent anyway, db date functions available). For time-zone dependent queries (such as the mentioned global hourly histogram) perhaps a view could expose columns: local_hour_of_day, local_minute_of_hour, etc.

    This is likely how one would have to do it if no TSTZ datatype was available--which, considering Oralce's poor support, is the nearly the case for practical purposes. Who wants to use an Oracle specific features anyway! :-)

    0 讨论(0)
  • 2020-12-13 11:31

    Since it looks like there's no magical way of doing this right, the simplest and the shortest method would be #1. Specifically, this is all the code needed:

    // convert Oracle's hard-coded: '2013-01-02 03:04:05.060708 +9:00'
    // to properly formatted ISO 8601: '2013-01-02T03:04:05.060708 +9:00'
    String iso = rs.getString(col).replaceFirst(" ", "T"); 
    

    it seems that just adding 'T' is enough, although a perfectionist would probably put more cosmetics (regex can optimized, of course), e.g.: rs.getString(col).replaceFirst(" ", "T").replaceAll(" ", "").replaceFirst("\+([0-9])\:", "+0$1:");

    B.

    0 讨论(0)
  • 2020-12-13 11:36

    A slight improvement to #2:

    CREATE OR REPLACE PACKAGE FORMAT AS
      FUNCTION TZ(T TIMESTAMP WITH TIME ZONE) RETURN VARCHAR2;
    END;
    /
    CREATE OR REPLACE PACKAGE BODY FORMAT AS
      FUNCTION TZ(T TIMESTAMP WITH TIME ZONE) RETURN VARCHAR2
      AS
      BEGIN
        RETURN TO_CHAR(T,'YYYYMMDD"T"HH24:MI:SS.FFTZHTZM');
      END;
    END;
    /
    

    The in SQL this becomes:

    SELECT FORMAT.TZ(tstz) EVENT_TIME ...
    

    It's more readable.
    If you ever need to change it, it's 1 place.
    The downside is it is an extra function call.

    0 讨论(0)
  • 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;
    }
    
    0 讨论(0)
  • 2020-12-13 11:42

    JDBC getObject(), or getTIMESTAMPTZ(), both return Oracle's TIMESTAMPTZ object, which is practically useless, because it doesn't have any conversion to Calendar (only Date, Time and Timestamp), so again, we lose TZ information.

    That would be my recommendation as the only reliable way to get the information you seek.

    If you are on Java SE 8 and have ojdbc8 then you can use getObject(int, OffsetDateTime.class). Be aware that when you use getObject(int, ZonedDateTime.class) you may be affected by bug 25792016.

    Use Oracle's TIMESTAMPTZ java class and extract relevant value manually from its internal (documented) byte array structure (i.e. implement my own toString() method which Oracle forgot to implement there). This is risky if Oracle changes internal structure (unlikely) and demands relatively complicated function to implement and maintain.

    This is what we ultimately went with until bug free JSR-310 support is available in the Oracle JDBC driver. We determined this was the only reliable way to get the information we want.

    0 讨论(0)
提交回复
热议问题