Search in sources :

Example 86 with TimeZone

use of java.util.TimeZone in project jetty.project by eclipse.

the class JSONTest method testConvertor.

/* ------------------------------------------------------------ */
@Test
public void testConvertor() {
    // test case#1 - force timezone to GMT
    JSON json = new JSON();
    json.addConvertor(Date.class, new JSONDateConvertor("MM/dd/yyyy HH:mm:ss zzz", TimeZone.getTimeZone("GMT"), false));
    json.addConvertor(Object.class, new JSONObjectConvertor());
    Woggle w0 = new Woggle();
    Gizmo g0 = new Gizmo();
    w0.name = "woggle0";
    w0.nested = g0;
    w0.number = 100;
    g0.name = "woggle1";
    g0.nested = null;
    g0.number = -101;
    g0.tested = true;
    HashMap map = new HashMap();
    Date dummyDate = new Date(1);
    map.put("date", dummyDate);
    map.put("w0", w0);
    StringBuffer buf = new StringBuffer();
    json.append(buf, map);
    String js = buf.toString();
    assertTrue(js.indexOf("\"date\":\"01/01/1970 00:00:00 GMT\"") >= 0);
    assertTrue(js.indexOf("org.eclipse.jetty.util.ajax.JSONTest$Woggle") >= 0);
    assertTrue(js.indexOf("org.eclipse.jetty.util.ajax.JSONTest$Gizmo") < 0);
    assertTrue(js.indexOf("\"tested\":true") >= 0);
    // test case#3
    TimeZone tzone = TimeZone.getTimeZone("JST");
    String tzone3Letter = tzone.getDisplayName(false, TimeZone.SHORT);
    String format = "EEE MMMMM dd HH:mm:ss zzz yyyy";
    Locale l = new Locale("ja", "JP");
    if (l != null) {
        json.addConvertor(Date.class, new JSONDateConvertor(format, tzone, false, l));
        buf = new StringBuffer();
        json.append(buf, map);
        js = buf.toString();
        //assertTrue(js.indexOf("\"date\":\"木 1月 01 09:00:00 JST 1970\"")>=0);
        assertTrue(js.indexOf(" 01 09:00:00 JST 1970\"") >= 0);
        assertTrue(js.indexOf("org.eclipse.jetty.util.ajax.JSONTest$Woggle") >= 0);
        assertTrue(js.indexOf("org.eclipse.jetty.util.ajax.JSONTest$Gizmo") < 0);
        assertTrue(js.indexOf("\"tested\":true") >= 0);
    }
    // test case#4
    json.addConvertor(Date.class, new JSONDateConvertor(true));
    w0.nested = null;
    buf = new StringBuffer();
    json.append(buf, map);
    js = buf.toString();
    assertTrue(js.indexOf("\"date\":\"Thu Jan 01 00:00:00 GMT 1970\"") < 0);
    assertTrue(js.indexOf("org.eclipse.jetty.util.ajax.JSONTest$Woggle") >= 0);
    assertTrue(js.indexOf("org.eclipse.jetty.util.ajax.JSONTest$Gizmo") < 0);
    map = (HashMap) json.parse(new JSON.StringSource(js));
    assertTrue(map.get("date") instanceof Date);
    assertTrue(map.get("w0") instanceof Woggle);
}
Also used : Locale(java.util.Locale) TimeZone(java.util.TimeZone) HashMap(java.util.HashMap) Date(java.util.Date) Test(org.junit.Test)

Example 87 with TimeZone

use of java.util.TimeZone in project tomcat by apache.

the class SSIMediator method formatDate.

protected String formatDate(Date date, TimeZone timeZone) {
    String retVal;
    if (timeZone != null) {
        //we temporarily change strftime. Since SSIMediator is inherently
        // single-threaded, this
        //isn't a problem
        TimeZone oldTimeZone = strftime.getTimeZone();
        strftime.setTimeZone(timeZone);
        retVal = strftime.format(date);
        strftime.setTimeZone(oldTimeZone);
    } else {
        retVal = strftime.format(date);
    }
    return retVal;
}
Also used : TimeZone(java.util.TimeZone)

Example 88 with TimeZone

use of java.util.TimeZone in project android-betterpickers by code-troopers.

the class TimeZoneData method loadTzs.

void loadTzs(Context context) {
    mTimeZones = new ArrayList<TimeZoneInfo>();
    HashSet<String> processedTimeZones = loadTzsInZoneTab(context);
    String[] tzIds = TimeZone.getAvailableIDs();
    if (DEBUG) {
        Log.e(TAG, "Available time zones: " + tzIds.length);
    }
    for (String tzId : tzIds) {
        if (processedTimeZones.contains(tzId)) {
            continue;
        }
        /*
             * Dropping non-GMT tzs without a country code. They are not really
             * needed and they are dups but missing proper country codes. e.g.
             * WET CET MST7MDT PST8PDT Asia/Khandyga Asia/Ust-Nera EST
             */
        if (!tzId.startsWith("Etc/GMT")) {
            continue;
        }
        final TimeZone tz = TimeZone.getTimeZone(tzId);
        if (tz == null) {
            Log.e(TAG, "Timezone not found: " + tzId);
            continue;
        }
        TimeZoneInfo tzInfo = new TimeZoneInfo(tz, null);
        if (getIdenticalTimeZoneInTheCountry(tzInfo) == -1) {
            if (DEBUG) {
                Log.e(TAG, "# Adding time zone from getAvailId: " + tzInfo.toString());
            }
            mTimeZones.add(tzInfo);
        } else {
            if (DEBUG) {
                Log.e(TAG, "# Dropping identical time zone from getAvailId: " + tzInfo.toString());
            }
            continue;
        }
    //
    // TODO check for dups
    // checkForNameDups(tz, tzInfo.mCountry, false /* dls */,
    // TimeZone.SHORT, groupIdx, !found);
    // checkForNameDups(tz, tzInfo.mCountry, false /* dls */,
    // TimeZone.LONG, groupIdx, !found);
    // if (tz.useDaylightTime()) {
    // checkForNameDups(tz, tzInfo.mCountry, true /* dls */,
    // TimeZone.SHORT, groupIdx,
    // !found);
    // checkForNameDups(tz, tzInfo.mCountry, true /* dls */,
    // TimeZone.LONG, groupIdx,
    // !found);
    // }
    }
    // Don't change the order of mTimeZones after this sort
    Collections.sort(mTimeZones);
    mTimeZonesByCountry = new LinkedHashMap<String, ArrayList<Integer>>();
    mTimeZonesByOffsets = new SparseArray<ArrayList<Integer>>(mHasTimeZonesInHrOffset.length);
    mTimeZonesById = new HashMap<String, TimeZoneInfo>(mTimeZones.size());
    for (TimeZoneInfo tz : mTimeZones) {
        // /////////////////////
        // Lookup map for id -> tz
        mTimeZonesById.put(tz.mTzId, tz);
    }
    populateDisplayNameOverrides(mContext.getResources());
    Date date = new Date(mTimeMillis);
    Locale defaultLocal = Locale.getDefault();
    int idx = 0;
    for (TimeZoneInfo tz : mTimeZones) {
        // Populate display name
        if (tz.mDisplayName == null) {
            tz.mDisplayName = tz.mTz.getDisplayName(tz.mTz.inDaylightTime(date), TimeZone.LONG, defaultLocal);
        }
        // /////////////////////
        // Grouping tz's by country for search by country
        ArrayList<Integer> group = mTimeZonesByCountry.get(tz.mCountry);
        if (group == null) {
            group = new ArrayList<Integer>();
            mTimeZonesByCountry.put(tz.mCountry, group);
        }
        group.add(idx);
        // /////////////////////
        // Grouping tz's by GMT offsets
        indexByOffsets(idx, tz);
        // Skip all the GMT+xx:xx style display names from search
        if (!tz.mDisplayName.endsWith(":00")) {
            mTimeZoneNames.add(tz.mDisplayName);
        } else if (DEBUG) {
            Log.e(TAG, "# Hiding from pretty name search: " + tz.mDisplayName);
        }
        idx++;
    }
// printTimeZones();
}
Also used : Locale(java.util.Locale) ArrayList(java.util.ArrayList) Date(java.util.Date) TimeZone(java.util.TimeZone)

Example 89 with TimeZone

use of java.util.TimeZone in project android-betterpickers by code-troopers.

the class TimeZoneData method loadTzsInZoneTab.

private HashSet<String> loadTzsInZoneTab(Context context) {
    HashSet<String> processedTimeZones = new HashSet<String>();
    AssetManager am = context.getAssets();
    InputStream is = null;
    /*
         * The 'backward' file contain mappings between new and old time zone
         * ids. We will explicitly ignore the old ones.
         */
    try {
        is = am.open("backward");
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        String line;
        while ((line = reader.readLine()) != null) {
            // Skip comment lines
            if (!line.startsWith("#") && line.length() > 0) {
                // 0: "Link"
                // 1: New tz id
                // Last: Old tz id
                String[] fields = line.split("\t+");
                String newTzId = fields[1];
                String oldTzId = fields[fields.length - 1];
                final TimeZone tz = TimeZone.getTimeZone(newTzId);
                if (tz == null) {
                    Log.e(TAG, "Timezone not found: " + newTzId);
                    continue;
                }
                processedTimeZones.add(oldTzId);
                if (DEBUG) {
                    Log.e(TAG, "# Dropping identical time zone from backward: " + oldTzId);
                }
                // Remember the cooler/newer time zone id
                if (mDefaultTimeZoneId != null && mDefaultTimeZoneId.equals(oldTzId)) {
                    mAlternateDefaultTimeZoneId = newTzId;
                }
            }
        }
    } catch (IOException ex) {
        Log.e(TAG, "Failed to read 'backward' file.");
    } finally {
        try {
            if (is != null) {
                is.close();
            }
        } catch (IOException ignored) {
        }
    }
    /*
         * zone.tab contains a list of time zones and country code. They are
         * "sorted first by country, then an order within the country that (1)
         * makes some geographical sense, and (2) puts the most populous zones
         * first, where that does not contradict (1)."
         */
    try {
        String lang = Locale.getDefault().getLanguage();
        is = am.open("zone.tab");
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        String line;
        while ((line = reader.readLine()) != null) {
            if (!line.startsWith("#")) {
                // Skip comment lines
                // 0: country code
                // 1: coordinates
                // 2: time zone id
                // 3: comments
                final String[] fields = line.split("\t");
                final String timeZoneId = fields[2];
                final String countryCode = fields[0];
                final TimeZone tz = TimeZone.getTimeZone(timeZoneId);
                if (tz == null) {
                    Log.e(TAG, "Timezone not found: " + timeZoneId);
                    continue;
                }
                /*
                     * Dropping non-GMT tzs without a country code. They are not
                     * really needed and they are dups but missing proper
                     * country codes. e.g. WET CET MST7MDT PST8PDT Asia/Khandyga
                     * Asia/Ust-Nera EST
                     */
                if (countryCode == null && !timeZoneId.startsWith("Etc/GMT")) {
                    processedTimeZones.add(timeZoneId);
                    continue;
                }
                // Remember the mapping between the country code and display
                // name
                String country = mCountryCodeToNameMap.get(countryCode);
                if (country == null) {
                    country = getCountryNames(lang, countryCode);
                    mCountryCodeToNameMap.put(countryCode, country);
                }
                // Find the country of the default tz
                if (mDefaultTimeZoneId != null && mDefaultTimeZoneCountry == null && timeZoneId.equals(mAlternateDefaultTimeZoneId)) {
                    mDefaultTimeZoneCountry = country;
                    TimeZone defaultTz = TimeZone.getTimeZone(mDefaultTimeZoneId);
                    if (defaultTz != null) {
                        mDefaultTimeZoneInfo = new TimeZoneInfo(defaultTz, country);
                        int tzToOverride = getIdenticalTimeZoneInTheCountry(mDefaultTimeZoneInfo);
                        if (tzToOverride == -1) {
                            if (DEBUG) {
                                Log.e(TAG, "Adding default time zone: " + mDefaultTimeZoneInfo.toString());
                            }
                            mTimeZones.add(mDefaultTimeZoneInfo);
                        } else {
                            mTimeZones.add(tzToOverride, mDefaultTimeZoneInfo);
                            if (DEBUG) {
                                TimeZoneInfo tzInfoToOverride = mTimeZones.get(tzToOverride);
                                String tzIdToOverride = tzInfoToOverride.mTzId;
                                Log.e(TAG, "Replaced by default tz: " + tzInfoToOverride.toString());
                                Log.e(TAG, "Adding default time zone: " + mDefaultTimeZoneInfo.toString());
                            }
                        }
                    }
                }
                // Add to the list of time zones if the time zone is unique
                // in the given country.
                TimeZoneInfo timeZoneInfo = new TimeZoneInfo(tz, country);
                int identicalTzIdx = getIdenticalTimeZoneInTheCountry(timeZoneInfo);
                if (identicalTzIdx == -1) {
                    if (DEBUG) {
                        Log.e(TAG, "# Adding time zone: " + timeZoneId + " ## " + tz.getDisplayName());
                    }
                    mTimeZones.add(timeZoneInfo);
                } else {
                    if (DEBUG) {
                        Log.e(TAG, "# Dropping identical time zone: " + timeZoneId + " ## " + tz.getDisplayName());
                    }
                }
                processedTimeZones.add(timeZoneId);
            }
        }
    } catch (IOException ex) {
        Log.e(TAG, "Failed to read 'zone.tab'.");
    } finally {
        try {
            if (is != null) {
                is.close();
            }
        } catch (IOException ignored) {
        }
    }
    return processedTimeZones;
}
Also used : TimeZone(java.util.TimeZone) AssetManager(android.content.res.AssetManager) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) HashSet(java.util.HashSet)

Example 90 with TimeZone

use of java.util.TimeZone in project android-betterpickers by code-troopers.

the class TimeZonePickerUtils method getGmtDisplayName.

/**
     * Given a timezone id (e.g. America/Los_Angeles), returns the corresponding timezone display name (e.g. Pacific
     * Time GMT-7).
     *
     * @param context Context in case the override labels need to be re-cached.
     * @param id The timezone id
     * @param millis The time (daylight savings or not)
     * @param grayGmt Whether the "GMT+x" part of the returned string should be colored gray.
     * @return The display name of the timezone.
     */
public CharSequence getGmtDisplayName(Context context, String id, long millis, boolean grayGmt) {
    TimeZone timezone = TimeZone.getTimeZone(id);
    if (timezone == null) {
        return null;
    }
    final Locale defaultLocale = Locale.getDefault();
    if (!defaultLocale.equals(mDefaultLocale)) {
        // If the IDs and labels haven't been set yet, or if the locale has been changed
        // recently, we'll need to re-cache them.
        mDefaultLocale = defaultLocale;
        cacheOverrides(context);
    }
    return buildGmtDisplayName(timezone, millis, grayGmt);
}
Also used : Locale(java.util.Locale) TimeZone(java.util.TimeZone)

Aggregations

TimeZone (java.util.TimeZone)577 Date (java.util.Date)140 Calendar (java.util.Calendar)137 SimpleTimeZone (java.util.SimpleTimeZone)89 Test (org.junit.Test)85 SimpleDateFormat (java.text.SimpleDateFormat)69 Locale (java.util.Locale)69 GregorianCalendar (java.util.GregorianCalendar)56 ArrayList (java.util.ArrayList)32 DateFormat (java.text.DateFormat)24 ParseException (java.text.ParseException)23 IOException (java.io.IOException)20 Map (java.util.Map)15 HashSet (java.util.HashSet)13 PreparedStatement (java.sql.PreparedStatement)11 HashMap (java.util.HashMap)11 DateTimeZone (org.joda.time.DateTimeZone)11 Support_TimeZone (tests.support.Support_TimeZone)10 Resources (android.content.res.Resources)9 SQLException (java.sql.SQLException)9