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

📄 fastdateformat.java

📁 基于Jabber协议的即时消息服务器
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/* ==================================================================== * Trove - Copyright (c) 1997-2001 Walt Disney Internet Group * ==================================================================== * The Tea Software License, Version 1.1 * * Copyright (c) 2000 Walt Disney Internet Group. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright *    notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright *    notice, this list of conditions and the following disclaimer in *    the documentation and/or other materials provided with the *    distribution. * * 3. The end-user documentation included with the redistribution, *    if any, must include the following acknowledgment: *       "This product includes software developed by the *        Walt Disney Internet Group (http://opensource.go.com/)." *    Alternately, this acknowledgment may appear in the software itself, *    if and wherever such third-party acknowledgments normally appear. * * 4. The names "Tea", "TeaServlet", "Kettle", "Trove" and "BeanDoc" must *    not be used to endorse or promote products derived from this *    software without prior written permission. For written *    permission, please contact opensource@dig.com. * * 5. Products derived from this software may not be called "Tea", *    "TeaServlet", "Kettle" or "Trove", nor may "Tea", "TeaServlet", *    "Kettle", "Trove" or "BeanDoc" appear in their name, without prior *    written permission of the Walt Disney Internet Group. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED.  IN NO EVENT SHALL THE WALT DISNEY INTERNET GROUP OR ITS * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * ==================================================================== * * For more information about Tea, please see http://opensource.go.com/. */package org.jivesoftware.util;import java.util.Date;import java.util.Calendar;import java.util.GregorianCalendar;import java.util.Locale;import java.util.TimeZone;import java.util.List;import java.util.ArrayList;import java.util.Map;import java.util.HashMap;import java.text.DateFormatSymbols;import java.text.DateFormat;import java.text.SimpleDateFormat;/** * <p>Similar to {@link java.text.SimpleDateFormat}, but faster and thread-safe. * Only formatting is supported, but all patterns are compatible with * SimpleDateFormat.</p> * * <p>Note, this class is from the open source Tea project (http://sourceforge.net/projects/teatrove/).</p> * * @author Brian S O'Neill */public class FastDateFormat {    /** Style pattern */    public static final Object        FULL = new Integer(SimpleDateFormat.FULL),        LONG = new Integer(SimpleDateFormat.LONG),        MEDIUM = new Integer(SimpleDateFormat.MEDIUM),        SHORT = new Integer(SimpleDateFormat.SHORT);    private static final double LOG_10 = Math.log(10);    private static String cDefaultPattern;    private static TimeZone cDefaultTimeZone = TimeZone.getDefault();    private static Map cTimeZoneDisplayCache = new HashMap();    private static Map cInstanceCache = new HashMap(7);    private static Map cDateInstanceCache = new HashMap(7);    private static Map cTimeInstanceCache = new HashMap(7);    private static Map cDateTimeInstanceCache = new HashMap(7);    public static FastDateFormat getInstance() {        return getInstance(getDefaultPattern(), null, null, null);    }    /**     * @param pattern {@link java.text.SimpleDateFormat} compatible pattern     */    public static FastDateFormat getInstance(String pattern)        throws IllegalArgumentException    {        return getInstance(pattern, null, null, null);    }    /**     * @param pattern {@link java.text.SimpleDateFormat} compatible pattern     * @param timeZone optional time zone, overrides time zone of formatted     * date     */    public static FastDateFormat getInstance        (String pattern, TimeZone timeZone) throws IllegalArgumentException    {        return getInstance(pattern, timeZone, null, null);    }    /**     * @param pattern {@link java.text.SimpleDateFormat} compatible pattern     * @param locale optional locale, overrides system locale     */    public static FastDateFormat getInstance        (String pattern, Locale locale) throws IllegalArgumentException    {        return getInstance(pattern, null, locale, null);    }    /**     * @param pattern {@link java.text.SimpleDateFormat} compatible pattern     * @param symbols optional date format symbols, overrides symbols for     * system locale     */    public static FastDateFormat getInstance        (String pattern, DateFormatSymbols symbols)        throws IllegalArgumentException    {        return getInstance(pattern, null, null, symbols);    }    /**     * @param pattern {@link java.text.SimpleDateFormat} compatible pattern     * @param timeZone optional time zone, overrides time zone of formatted     * date     * @param locale optional locale, overrides system locale     */    public static FastDateFormat getInstance        (String pattern, TimeZone timeZone, Locale locale)        throws IllegalArgumentException    {        return getInstance(pattern, timeZone, locale, null);    }    /**     * @param pattern {@link java.text.SimpleDateFormat} compatible pattern     * @param timeZone optional time zone, overrides time zone of formatted     * date     * @param locale optional locale, overrides system locale     * @param symbols optional date format symbols, overrides symbols for     * provided locale     */    public static synchronized FastDateFormat getInstance        (String pattern, TimeZone timeZone, Locale locale,         DateFormatSymbols symbols)        throws IllegalArgumentException    {        Object key = pattern;        if (timeZone != null) {            key = new Pair(key, timeZone);        }        if (locale != null) {            key = new Pair(key, locale);        }        if (symbols != null) {            key = new Pair(key, symbols);        }        FastDateFormat format = (FastDateFormat)cInstanceCache.get(key);        if (format == null) {            if (locale == null) {                locale = Locale.getDefault();            }            if (symbols == null) {                symbols = new DateFormatSymbols(locale);            }            format = new FastDateFormat(pattern, timeZone, locale, symbols);            cInstanceCache.put(key, format);        }        return format;    }    /**     * @param style date style: FULL, LONG, MEDIUM, or SHORT     * @param timeZone optional time zone, overrides time zone of formatted     * date     * @param locale optional locale, overrides system locale     */    public static synchronized FastDateFormat getDateInstance        (Object style, TimeZone timeZone, Locale locale)        throws IllegalArgumentException    {        Object key = style;        if (timeZone != null) {            key = new Pair(key, timeZone);        }        if (locale == null) {            key = new Pair(key, locale);        }        FastDateFormat format = (FastDateFormat)cDateInstanceCache.get(key);        if (format == null) {            int ds;            try {                ds = ((Integer)style).intValue();            }            catch (ClassCastException e) {                throw new IllegalArgumentException                    ("Illegal date style: " + style);            }            if (locale == null) {                locale = Locale.getDefault();            }            try {                String pattern = ((SimpleDateFormat)DateFormat.getDateInstance(ds, locale)).toPattern();                format = getInstance(pattern, timeZone, locale);                cDateInstanceCache.put(key, format);            }            catch (ClassCastException e) {                throw new IllegalArgumentException                    ("No date pattern for locale: " + locale);            }        }        return format;    }    /**     * @param style time style: FULL, LONG, MEDIUM, or SHORT     * @param timeZone optional time zone, overrides time zone of formatted     * date     * @param locale optional locale, overrides system locale     */    public static synchronized FastDateFormat getTimeInstance        (Object style, TimeZone timeZone, Locale locale)        throws IllegalArgumentException    {        Object key = style;        if (timeZone != null) {            key = new Pair(key, timeZone);        }        if (locale != null) {            key = new Pair(key, locale);        }        FastDateFormat format = (FastDateFormat)cTimeInstanceCache.get(key);        if (format == null) {            int ts;            try {                ts = ((Integer)style).intValue();            }            catch (ClassCastException e) {                throw new IllegalArgumentException                    ("Illegal time style: " + style);            }            if (locale == null) {                locale = Locale.getDefault();            }            try {                String pattern = ((SimpleDateFormat)DateFormat.getTimeInstance(ts, locale)).toPattern();                format = getInstance(pattern, timeZone, locale);                cTimeInstanceCache.put(key, format);            }            catch (ClassCastException e) {                throw new IllegalArgumentException                    ("No date pattern for locale: " + locale);            }        }        return format;    }    /**     * @param dateStyle date style: FULL, LONG, MEDIUM, or SHORT     * @param timeStyle time style: FULL, LONG, MEDIUM, or SHORT     * @param timeZone optional time zone, overrides time zone of formatted     * date     * @param locale optional locale, overrides system locale     */    public static synchronized FastDateFormat getDateTimeInstance        (Object dateStyle, Object timeStyle, TimeZone timeZone, Locale locale)        throws IllegalArgumentException    {        Object key = new Pair(dateStyle, timeStyle);        if (timeZone != null) {            key = new Pair(key, timeZone);        }        if (locale != null) {            key = new Pair(key, locale);        }        FastDateFormat format =            (FastDateFormat)cDateTimeInstanceCache.get(key);        if (format == null) {            int ds;            try {                ds = ((Integer)dateStyle).intValue();            }            catch (ClassCastException e) {                throw new IllegalArgumentException                    ("Illegal date style: " + dateStyle);            }            int ts;            try {                ts = ((Integer)timeStyle).intValue();            }            catch (ClassCastException e) {                throw new IllegalArgumentException                    ("Illegal time style: " + timeStyle);            }            if (locale == null) {                locale = Locale.getDefault();            }            try {                String pattern = ((SimpleDateFormat)DateFormat.getDateTimeInstance(ds, ts, locale)).toPattern();                format = getInstance(pattern, timeZone, locale);                cDateTimeInstanceCache.put(key, format);            }            catch (ClassCastException e) {                throw new IllegalArgumentException                    ("No date time pattern for locale: " + locale);            }        }        return format;    }    static synchronized String getTimeZoneDisplay(TimeZone tz,                                                  boolean daylight,                                                  int style,                                                  Locale locale) {        Object key = new TimeZoneDisplayKey(tz, daylight, style, locale);        String value = (String)cTimeZoneDisplayCache.get(key);        if (value == null) {            // This is a very slow call, so cache the results.            value = tz.getDisplayName(daylight, style, locale);            cTimeZoneDisplayCache.put(key, value);        }        return value;    }    private static synchronized String getDefaultPattern() {        if (cDefaultPattern == null) {            cDefaultPattern = new SimpleDateFormat().toPattern();        }        return cDefaultPattern;    }    /**     * Returns a list of Rules.     */    private static List parse(String pattern, TimeZone timeZone, Locale locale,                              DateFormatSymbols symbols) {        List rules = new ArrayList();        String[] ERAs = symbols.getEras();        String[] months = symbols.getMonths();        String[] shortMonths = symbols.getShortMonths();        String[] weekdays = symbols.getWeekdays();        String[] shortWeekdays = symbols.getShortWeekdays();        String[] AmPmStrings = symbols.getAmPmStrings();        int length = pattern.length();        int[] indexRef = new int[1];        for (int i=0; i<length; i++) {            indexRef[0] = i;            String token = parseToken(pattern, indexRef);            i = indexRef[0];            int tokenLen = token.length();            if (tokenLen == 0) {

⌨️ 快捷键说明

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