⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 timeutil.java

📁 在资料浩瀚的互联网中
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
        tempMap.put("VLAMT", new String[] { "Asia/Vladivostok" });        tempMap.put("VLAST", new String[] { "Asia/Vladivostok" });        tempMap.put("VLAT", new String[] { "Asia/Vladivostok" });        tempMap.put("VUST", new String[] { "Pacific/Efate" });        tempMap.put("VUT", new String[] { "Pacific/Efate" });        tempMap.put("WAKT", new String[] { "Pacific/Wake" });        tempMap.put("WARST", new String[] { "America/Jujuy", "America/Mendoza" });        tempMap.put("WART", new String[] { "America/Jujuy", "America/Mendoza" });        tempMap.put("WAST",            new String[] { "Africa/Ndjamena", "Africa/Windhoek" });        tempMap.put("WAT",            new String[] {                "Africa/Luanda", "Africa/Porto-Novo", "Africa/Douala",                "Africa/Bangui", "Africa/Ndjamena", "Africa/Kinshasa",                "Africa/Brazzaville", "Africa/Malabo", "Africa/Libreville",                "Africa/Banjul", "Africa/Conakry", "Africa/Bissau",                "Africa/Bamako", "Africa/Nouakchott", "Africa/El_Aaiun",                "Africa/Windhoek", "Africa/Niamey", "Africa/Lagos",                "Africa/Dakar", "Africa/Freetown"            });        tempMap.put("WEST",            new String[] {                "Atlantic/Faeroe", "Atlantic/Azores", "Atlantic/Madeira",                "Atlantic/Canary", "Europe/Brussels", "Europe/Luxembourg",                "Europe/Monaco", "Europe/Lisbon", "Europe/Madrid",                "Africa/Algiers", "Africa/Casablanca", "Africa/Ceuta"            });        tempMap.put("WET",            new String[] {                "Atlantic/Faeroe", "Atlantic/Azores", "Atlantic/Madeira",                "Atlantic/Canary", "Europe/Andorra", "Europe/Brussels",                "Europe/Luxembourg", "Europe/Monaco", "Europe/Lisbon",                "Europe/Madrid", "Africa/Algiers", "Africa/Casablanca",                "Africa/El_Aaiun", "Africa/Ceuta"            });        tempMap.put("WFT", new String[] { "Pacific/Wallis" });        tempMap.put("WGST", new String[] { "America/Godthab" });        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);    }    /**     * 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;    }    /**     * 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;    }    /**     * 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;    }    //    // 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;    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -