/** * This conversion required the use of the literal string to get the same * functionality as the native SQL to_timestamp() approach. */ public static TIMESTAMPTZ buildTIMESTAMPTZ(Calendar cal, Connection con, boolean shouldPrintCalendar) throws SQLException { //Bug5614674. It used to be a driver bug and Helper.printCalendar(cal, false) was used to make it work. It has been fixed in 11. Separate the newer version from the old ones. if (shouldPrintCalendar) { return new TIMESTAMPTZ(con, Helper.printCalendar(cal, false), cal); } else { return new TIMESTAMPTZ(con, new Timestamp(cal.getTimeInMillis()), cal); } }
/** * INTERNAL: * Get a TIMESTAMPTZ value from a result set. */ public Object getTIMESTAMPTZFromResultSet(ResultSet resultSet, int columnNumber, int type, AbstractSession session) throws java.sql.SQLException { TIMESTAMPTZ tsTZ = (TIMESTAMPTZ)resultSet.getObject(columnNumber); //Need to call timestampValue once here with the connection to avoid null point //exception later when timestampValue is called in converObject() if ((tsTZ != null) && (tsTZ.getLength() != 0)) { Connection connection = getConnection(session, resultSet.getStatement().getConnection()); //Bug#4364359 Add a wrapper to overcome TIMESTAMPTZ not serializable as of jdbc 9.2.0.5 and 10.1.0.2. //It has been fixed in the next version for both streams Timestamp timestampToWrap = tsTZ.timestampValue(connection); TimeZone timezoneToWrap = TIMESTAMPHelper.extractTimeZone(tsTZ.toBytes()); return new TIMESTAMPTZWrapper(timestampToWrap, timezoneToWrap, this.isTimestampInGmt); } return null; }
/** * Converts {@link TIMESTAMPTZ} to {@link ZonedDateTime}. * * @param dbData the data from the database to be converted, possibly {@code null} * @return the converted value, possibly {@code null} */ public static ZonedDateTime timestamptzToZonedDateTime(TIMESTAMPTZ dbData) { if (dbData == null) { return null; } byte[] bytes = dbData.toBytes(); OffsetDateTime utc = extractUtc(bytes); if (isFixedOffset(bytes)) { ZoneOffset offset = extractOffset(bytes); return utc.atZoneSameInstant(offset); } else { ZoneId zoneId = extractZoneId(bytes); return utc.atZoneSameInstant(zoneId); } }
protected Object fromOracleTimeClasses(Column column, Object data) { try { if (data instanceof TIMESTAMP) { data = ((TIMESTAMP) data).timestampValue(); } else if (data instanceof DATE) { data = ((DATE) data).timestampValue(); } else if (data instanceof TIMESTAMPTZ) { final TIMESTAMPTZ ts = (TIMESTAMPTZ)data; data = ZonedDateTime.ofInstant(ts.timestampValue(connection.connection()).toInstant(), ts.getTimeZone().toZoneId()); } else if (data instanceof TIMESTAMPLTZ) { // JDBC driver throws an exception // final TIMESTAMPLTZ ts = (TIMESTAMPLTZ)data; // data = ts.offsetDateTimeValue(connection.connection()); return null; } } catch (SQLException e) { throw new RuntimeException("Couldn't convert value for column " + column.name(), e); } return data; }
/** * Converts {@link TIMESTAMPTZ} to {@link ZonedDateTime}. * * @param dbData the data from the database to be converted, possibly {@code null} * @return the converted value, possibly {@code null} */ public static ZonedDateTime timestamptzToZonedDateTime(TIMESTAMPTZ dbData) { if (dbData == null) { return null; } byte[] bytes = dbData.toBytes(); OffsetDateTime utc = extractUtc(bytes); if (isFixedOffset(bytes)) { ZoneOffset offset = extractOffset(bytes); return utc.atZoneSameInstant(offset); } else { ZoneId zoneId = extractZoneId(bytes); return utc.atZoneSameInstant(zoneId); } }
/** * INTERNAL: * Get a TIMESTAMPTZ value from a result set. */ public Object getTIMESTAMPTZFromResultSet(ResultSet resultSet, int columnNumber, int type, AbstractSession session) throws java.sql.SQLException { TIMESTAMPTZ tsTZ = (TIMESTAMPTZ)resultSet.getObject(columnNumber); //Need to call timestampValue once here with the connection to avoid null point //exception later when timestampValue is called in converObject() if ((tsTZ != null) && (tsTZ.getLength() != 0)) { Connection connection = getConnection(session, resultSet.getStatement().getConnection()); //Bug#4364359 Add a wrapper to overcome TIMESTAMPTZ not serializable as of jdbc 9.2.0.5 and 10.1.0.2. //It has been fixed in the next version for both streams Timestamp timestampToWrap = tsTZ.timestampValue(connection); TimeZone timezoneToWrap = TIMESTAMPHelper.extractTimeZone(tsTZ.toBytes()); return new TIMESTAMPTZWrapper(timestampToWrap, timezoneToWrap, this.isTimestampInGmt); } return null; }
/** * This conversion required the use of the literal string to get the same * functionality as the native SQL to_timestamp() approach. */ public static TIMESTAMPTZ buildTIMESTAMPTZ(Calendar cal, Connection con, boolean shouldPrintCalendar) throws SQLException { //Bug5614674. It used to be a driver bug and Helper.printCalendar(cal, false) was used to make it work. It has been fixed in 11. Separate the newer version from the old ones. if (shouldPrintCalendar) { return new TIMESTAMPTZ(con, Helper.printCalendar(cal, false), cal); } else { return new TIMESTAMPTZ(con, new Timestamp(cal.getTimeInMillis()), cal); } }
/** * Converts {@link TIMESTAMPTZ} to {@link OffsetDateTime}. * * @param dbData the data from the database to be converted, possibly {@code null} * @return the converted value, possibly {@code null} */ public static OffsetDateTime timestamptzToOffsetDateTime(TIMESTAMPTZ dbData) { if (dbData == null) { return null; } byte[] bytes = dbData.toBytes(); OffsetDateTime utc = extractUtc(bytes); if (isFixedOffset(bytes)) { ZoneOffset offset = extractOffset(bytes); return utc.withOffsetSameInstant(offset); } else { ZoneId zoneId = extractZoneId(bytes); return utc.atZoneSameInstant(zoneId).toOffsetDateTime(); } }
SimpleDateFormat dateFormatTZGeneral = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS z"); SimpleDateFormat dateFormatTZISO = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS XXX"); SimpleDateFormat dateFormatWithoutTZ = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); String[][] zoneStrings = DateFormatSymbols.getInstance().getZoneStrings(); Date date = null; String[] timeStampSplits = timestamp.split(" "); if(timeStampSplits.length>2) { String timezone = timeStampSplits[2]; //First Case Asia/Calcutta if(Character.isAlphabetic(timezone.charAt(timezone.length()-1))) { for(String[] zoneString: zoneStrings) { if(zoneString[0].equalsIgnoreCase(timezone)) { timeStampSplits[2] = zoneString[2]; break; } } timestamp = createString(timeStampSplits," "); date = getDate(timestamp, dateFormatTZGeneral); } else { //Second Case 8:00 timeStampSplits[2] = formatTimeZone(timeStampSplits[2]); timestamp = createString(timeStampSplits," "); date = getDate(timestamp, dateFormatTZISO); } } else { // Third Case without timezone date = getDate(timestamp, dateFormatWithoutTZ); } System.out.println(date); TIMESTAMPTZ oraTimeStamp = new TIMESTAMPTZ(<connection object>,new java.sql.Timestamp(date.getTime());
/** * Converts {@link TIMESTAMPTZ} to {@link OffsetDateTime}. * * @param dbData the data from the database to be converted, possibly {@code null} * @return the converted value, possibly {@code null} */ public static OffsetDateTime timestamptzToOffsetDateTime(TIMESTAMPTZ dbData) { if (dbData == null) { return null; } byte[] bytes = dbData.toBytes(); OffsetDateTime utc = extractUtc(bytes); if (isFixedOffset(bytes)) { ZoneOffset offset = extractOffset(bytes); return utc.withOffsetSameInstant(offset); } else { ZoneId zoneId = extractZoneId(bytes); return utc.atZoneSameInstant(zoneId).toOffsetDateTime(); } }
/** * Converts {@link OffsetDateTime} to {@link TIMESTAMPTZ}. * * @param attribute the value to be converted, possibly {@code null} * @return the converted data, possibly {@code null} */ public static TIMESTAMPTZ offsetDateTimeToTimestamptz(OffsetDateTime attribute) { if (attribute == null) { return null; } byte[] bytes = newTimestamptzBuffer(); ZonedDateTime utc = attribute.atZoneSameInstant(UTC); writeDateTime(bytes, utc.toLocalDateTime()); ZoneOffset offset = attribute.getOffset(); writeZoneOffset(bytes, offset); return new TIMESTAMPTZ(bytes); }
/** * Converts {@link OffsetDateTime} to {@link TIMESTAMPTZ}. * * @param attribute the value to be converted, possibly {@code null} * @return the converted data, possibly {@code null} */ public static TIMESTAMPTZ offsetDateTimeToTimestamptz(OffsetDateTime attribute) { if (attribute == null) { return null; } byte[] bytes = newTimestamptzBuffer(); ZonedDateTime utc = attribute.atZoneSameInstant(UTC); writeDateTime(bytes, utc.toLocalDateTime()); ZoneOffset offset = attribute.getOffset(); writeZoneOffset(bytes, offset); return new TIMESTAMPTZ(bytes); }
/** * Converts {@link ZonedDateTime} to {@link TIMESTAMPTZ}. * * @param attribute the value to be converted, possibly {@code null} * @return the converted data, possibly {@code null} */ public static TIMESTAMPTZ zonedDateTimeToTimestamptz(ZonedDateTime attribute) { if (attribute == null) { return null; } byte[] bytes = newTimestamptzBuffer(); ZonedDateTime utc = attribute.withZoneSameInstant(UTC); writeDateTime(bytes, utc.toLocalDateTime()); String zoneId = attribute.getZone().getId(); int regionCode = ZONEIDMAP.getID(zoneId); if (isValidRegionCode(regionCode)) { writeZoneId(bytes, regionCode); } else { writeZoneOffset(bytes, attribute.getOffset()); } return new TIMESTAMPTZ(bytes); }
/** * Converts {@link ZonedDateTime} to {@link TIMESTAMPTZ}. * * @param attribute the value to be converted, possibly {@code null} * @return the converted data, possibly {@code null} */ public static TIMESTAMPTZ zonedDateTimeToTimestamptz(ZonedDateTime attribute) { if (attribute == null) { return null; } byte[] bytes = newTimestamptzBuffer(); ZonedDateTime utc = attribute.withZoneSameInstant(UTC); writeDateTime(bytes, utc.toLocalDateTime()); String zoneId = attribute.getZone().getId(); int regionCode = ZONEIDMAP.getID(zoneId); if (isValidRegionCode(regionCode)) { writeZoneId(bytes, regionCode); } else { writeZoneOffset(bytes, attribute.getOffset()); } return new TIMESTAMPTZ(bytes); }