📄 timeutil.java
字号:
tempMap.put("WGT", new String[] { "America/Godthab" }); tempMap.put("WMT", new String[] { "Europe/Vilnius", "Europe/Warsaw" }); tempMap.put("WST", new String[] { "Antarctica/Casey", "Pacific/Apia", "Australia/Perth" }); tempMap.put("YAKMT", new String[] { "Asia/Yakutsk" }); tempMap.put("YAKST", new String[] { "Asia/Yakutsk" }); tempMap.put("YAKT", new String[] { "Asia/Yakutsk" }); tempMap.put("YAPT", new String[] { "Pacific/Yap" }); tempMap.put("YDDT", new String[] { "America/Whitehorse", "America/Dawson" }); tempMap.put("YDT", new String[] { "America/Yakutat", "America/Whitehorse", "America/Dawson" }); tempMap.put("YEKMT", new String[] { "Asia/Yekaterinburg" }); tempMap.put("YEKST", new String[] { "Asia/Yekaterinburg" }); tempMap.put("YEKT", new String[] { "Asia/Yekaterinburg" }); tempMap.put("YERST", new String[] { "Asia/Yerevan" }); tempMap.put("YERT", new String[] { "Asia/Yerevan" }); tempMap.put("YST", new String[] { "America/Yakutat", "America/Whitehorse", "America/Dawson" }); tempMap.put("YWT", new String[] { "America/Yakutat" }); ABBREVIATED_TIMEZONES = Collections.unmodifiableMap(tempMap); } /** * Change the given times from one timezone to another * * @param conn * the current connection to the MySQL server * @param t * the times to change * @param fromTz * the timezone to change from * @param toTz * the timezone to change to * * @return the times changed to the timezone 'toTz' */ public static Time changeTimezone(Connection conn, Time t, TimeZone fromTz, TimeZone toTz, boolean rollForward) { if ((conn != null) && conn.getUseTimezone()) { // Convert the timestamp from GMT to the server's timezone Calendar fromCal = Calendar.getInstance(fromTz); fromCal.setTime(t); int fromOffset = fromCal.get(Calendar.ZONE_OFFSET) + fromCal.get(Calendar.DST_OFFSET); Calendar toCal = Calendar.getInstance(toTz); toCal.setTime(t); 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; } Time changedTime = new Time(toTime); return changedTime; } return t; } /** * Change the given timestamp from one timezone to another * * @param conn * the current connection to the MySQL server * @param tstamp * the timestamp to change * @param fromTz * the timezone to change from * @param toTz * the timezone to change to * * @return the timestamp changed to the timezone 'toTz' */ public static Timestamp changeTimezone(Connection conn, Timestamp tstamp, TimeZone fromTz, TimeZone toTz, boolean rollForward) { if ((conn != null) && conn.getUseTimezone()) { // Convert the timestamp from GMT to the server's timezone Calendar fromCal = Calendar.getInstance(fromTz); fromCal.setTime(tstamp); 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; } return tstamp; } // // WARN! You must externally synchronize these calendar instances // See ResultSet.fastDateCreate() for an example // final static Date fastDateCreate(Calendar cal, int year, int month, int day) { 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, 0, 0, 0); long dateAsMillis = 0; try { dateAsMillis = cal.getTimeInMillis(); } catch (IllegalAccessError iae) { // Must be on JDK-1.3.1 or older.... dateAsMillis = cal.getTime().getTime(); } return new Date(dateAsMillis); } final static Time fastTimeCreate(Calendar cal, int hour, int minute, int second) { 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(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); 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); 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; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -