/** * 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); } }
/** * 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); } }
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 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); }