?? timeutil.java
字號:
int fromOffset = fromCal.get(Calendar.ZONE_OFFSET) + fromCal.get(Calendar.DST_OFFSET); Calendar toCal = Calendar.getInstance(toTz); toCal.setTime(tstamp); int toOffset = toCal.get(Calendar.ZONE_OFFSET) + toCal.get(Calendar.DST_OFFSET); int offsetDiff = fromOffset - toOffset; long toTime = toCal.getTime().getTime(); if (rollForward || (conn.isServerTzUTC() && !conn.isClientTzUTC())) { toTime += offsetDiff; } else { toTime -= offsetDiff; } Timestamp changedTimestamp = new Timestamp(toTime); return changedTimestamp; } else if (conn.getUseJDBCCompliantTimezoneShift()) { if (targetCalendar != null) { Timestamp adjustedTimestamp = new Timestamp( jdbcCompliantZoneShift(sessionCalendar, targetCalendar, tstamp)); adjustedTimestamp.setNanos(tstamp.getNanos()); return adjustedTimestamp; } } } return tstamp; } private static long jdbcCompliantZoneShift(Calendar sessionCalendar, Calendar targetCalendar, java.util.Date dt) { if (sessionCalendar == null) { sessionCalendar = new GregorianCalendar(); } // JDBC spec is not clear whether or not this // calendar should be immutable, so let's treat // it like it is, for safety java.util.Date origCalDate = targetCalendar.getTime(); java.util.Date origSessionDate = sessionCalendar.getTime(); try { sessionCalendar.setTime(dt); targetCalendar.set(Calendar.YEAR, sessionCalendar.get(Calendar.YEAR)); targetCalendar.set(Calendar.MONTH, sessionCalendar.get(Calendar.MONTH)); targetCalendar.set(Calendar.DAY_OF_MONTH, sessionCalendar.get(Calendar.DAY_OF_MONTH)); targetCalendar.set(Calendar.HOUR_OF_DAY, sessionCalendar.get(Calendar.HOUR_OF_DAY)); targetCalendar.set(Calendar.MINUTE, sessionCalendar.get(Calendar.MINUTE)); targetCalendar.set(Calendar.SECOND, sessionCalendar.get(Calendar.SECOND)); targetCalendar.set(Calendar.MILLISECOND, sessionCalendar.get(Calendar.MILLISECOND)); return targetCalendar.getTime().getTime(); } finally { sessionCalendar.setTime(origSessionDate); targetCalendar.setTime(origCalDate); } } // // WARN! You must externally synchronize these calendar instances // See ResultSet.fastDateCreate() for an example // final static Date fastDateCreate(boolean useGmtConversion, Calendar gmtCalIfNeeded, Calendar cal, int year, int month, int day) { Calendar dateCal = cal; if (useGmtConversion) { if (gmtCalIfNeeded == null) { gmtCalIfNeeded = Calendar.getInstance(TimeZone.getTimeZone("GMT")); } gmtCalIfNeeded.clear(); dateCal = gmtCalIfNeeded; } dateCal.clear(); // why-oh-why is this different than java.util.date, // in the year part, but it still keeps the silly '0' // for the start month???? dateCal.set(year, month - 1, day, 0, 0, 0); long dateAsMillis = 0; try { dateAsMillis = dateCal.getTimeInMillis(); } catch (IllegalAccessError iae) { // Must be on JDK-1.3.1 or older.... dateAsMillis = dateCal.getTime().getTime(); } return new Date(dateAsMillis); } final static Time fastTimeCreate(Calendar cal, int hour, int minute, int second) throws SQLException { if (hour < 0 || hour > 23) { throw SQLError.createSQLException("Illegal hour value '" + hour + "' for java.sql.Time type in value '" + timeFormattedString(hour, minute, second) + ".", SQLError.SQL_STATE_ILLEGAL_ARGUMENT); } if (minute < 0 || minute > 59) { throw SQLError.createSQLException("Illegal minute value '" + minute + "'" + "' for java.sql.Time type in value '" + timeFormattedString(hour, minute, second) + ".", SQLError.SQL_STATE_ILLEGAL_ARGUMENT); } if (second < 0 || second > 59) { throw SQLError.createSQLException("Illegal minute value '" + second + "'" + "' for java.sql.Time type in value '" + timeFormattedString(hour, minute, second) + ".", SQLError.SQL_STATE_ILLEGAL_ARGUMENT); } cal.clear(); // Set 'date' to epoch of Jan 1, 1970 cal.set(1970, 0, 1, hour, minute, second); long timeAsMillis = 0; try { timeAsMillis = cal.getTimeInMillis(); } catch (IllegalAccessError iae) { // Must be on JDK-1.3.1 or older.... timeAsMillis = cal.getTime().getTime(); } return new Time(timeAsMillis); } final static Timestamp fastTimestampCreate(boolean useGmtConversion, Calendar gmtCalIfNeeded, Calendar cal, int year, int month, int day, int hour, int minute, int seconds, int secondsPart) { cal.clear(); // why-oh-why is this different than java.util.date, // in the year part, but it still keeps the silly '0' // for the start month???? cal.set(year, month - 1, day, hour, minute, seconds); int offsetDiff = 0; if (useGmtConversion) { int fromOffset = cal.get(Calendar.ZONE_OFFSET) + cal.get(Calendar.DST_OFFSET); if (gmtCalIfNeeded == null) { gmtCalIfNeeded = Calendar.getInstance(TimeZone.getTimeZone("GMT")); } gmtCalIfNeeded.clear(); gmtCalIfNeeded.setTimeInMillis(cal.getTimeInMillis()); int toOffset = gmtCalIfNeeded.get(Calendar.ZONE_OFFSET) + gmtCalIfNeeded.get(Calendar.DST_OFFSET); offsetDiff = fromOffset - toOffset; } long tsAsMillis = 0; try { tsAsMillis = cal.getTimeInMillis(); } catch (IllegalAccessError iae) { // Must be on JDK-1.3.1 or older.... tsAsMillis = cal.getTime().getTime(); } Timestamp ts = new Timestamp(tsAsMillis + offsetDiff); ts.setNanos(secondsPart); return ts; } /** * Returns the 'official' Java timezone name for the given timezone * * @param timezoneStr * the 'common' timezone name * * @return the Java timezone name for the given timezone * * @throws IllegalArgumentException * DOCUMENT ME! */ public static String getCanoncialTimezone(String timezoneStr) { if (timezoneStr == null) { return null; } timezoneStr = timezoneStr.trim(); // Fix windows Daylight/Standard shift JDK doesn't map these (doh) int daylightIndex = StringUtils.indexOfIgnoreCase(timezoneStr, "DAYLIGHT"); if (daylightIndex != -1) { StringBuffer timezoneBuf = new StringBuffer(); timezoneBuf.append(timezoneStr.substring(0, daylightIndex)); timezoneBuf.append("Standard"); timezoneBuf.append(timezoneStr.substring(daylightIndex + "DAYLIGHT".length(), timezoneStr.length())); timezoneStr = timezoneBuf.toString(); } String canonicalTz = (String) TIMEZONE_MAPPINGS.get(timezoneStr); // if we didn't find it, try abbreviated timezones if (canonicalTz == null) { String[] abbreviatedTimezone = (String[]) ABBREVIATED_TIMEZONES .get(timezoneStr); if (abbreviatedTimezone != null) { // If there's only one mapping use that if (abbreviatedTimezone.length == 1) { canonicalTz = abbreviatedTimezone[0]; } else { StringBuffer errorMsg = new StringBuffer( "The server timezone value '"); errorMsg.append(timezoneStr); errorMsg .append("' represents more than one timezone. You must "); errorMsg .append("configure either the server or client to use a "); errorMsg .append("more specifc timezone value if you want to enable "); errorMsg.append("timezone support. The timezones that '"); errorMsg.append(timezoneStr); errorMsg.append("' maps to are: "); errorMsg.append(abbreviatedTimezone[0]); for (int i = 1; i < abbreviatedTimezone.length; i++) { errorMsg.append(", "); errorMsg.append(abbreviatedTimezone[i]); } throw new IllegalArgumentException(errorMsg.toString()); } } } return canonicalTz; } // we could use SimpleDateFormat, but it won't work when the time values // are out-of-bounds, and we're using this for error messages for exactly // that case // private static String timeFormattedString(int hours, int minutes, int seconds) { StringBuffer buf = new StringBuffer(8); if (hours < 10) { buf.append("0"); } buf.append(hours); buf.append(":"); if (minutes < 10) { buf.append("0"); } buf.append(minutes); buf.append(":"); if (seconds < 10) { buf.append("0"); } buf.append(seconds); return buf.toString(); }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -