Android 時間 日期 時區

日期 時間

    private void setTime( int hourOfDay, int minute) {
        Calendar c = Calendar.getInstance();

        c.set(Calendar.HOUR_OF_DAY, hourOfDay);
        c.set(Calendar.MINUTE, minute);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        long when = c.getTimeInMillis();

        if (when / 1000 < Integer.MAX_VALUE) {
            ((AlarmManager) getSystemService(Context.ALARM_SERVICE)).setTime(when);
        }
    }
    
    
    private void setDate(int year, int month, int day) {
        Calendar c = Calendar.getInstance();

        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, month);
        c.set(Calendar.DAY_OF_MONTH, day);
        long when = c.getTimeInMillis();

        if (when / 1000 < Integer.MAX_VALUE) {
            ((AlarmManager) getSystemService(Context.ALARM_SERVICE)).setTime(when);
        }
    }

需要注意Calendar.MONTH 比實際月份小1,比如1月值是0

 

 

時區

   
    public static String getTimeZoneText(TimeZone tz, boolean includeName) {
        Date now = new Date();

        // Use SimpleDateFormat to format the GMT+00:00 string.
        SimpleDateFormat gmtFormatter = new SimpleDateFormat("ZZZZ");
        gmtFormatter.setTimeZone(tz);
        String gmtString = gmtFormatter.format(now);

        // Ensure that the "GMT+" stays with the "00:00" even if the digits are RTL.
        BidiFormatter bidiFormatter = BidiFormatter.getInstance();
        Locale l = Locale.getDefault();
        boolean isRtl = TextUtils.getLayoutDirectionFromLocale(l) == View.LAYOUT_DIRECTION_RTL;
        gmtString = bidiFormatter.unicodeWrap(gmtString,
                isRtl ? TextDirectionHeuristics.RTL : TextDirectionHeuristics.LTR);

        if (!includeName) {
            return gmtString;
        }

        // Optionally append the time zone name.
        SimpleDateFormat zoneNameFormatter = new SimpleDateFormat("zzzz");
        zoneNameFormatter.setTimeZone(tz);
        String zoneNameString = zoneNameFormatter.format(now);

        // We don't use punctuation here to avoid having to worry about localizing that too!
        return gmtString + " " + zoneNameString;
    }
    
    
    static class ZoneGetter {
        private final List<HashMap<String, Object>> mZones =
                new ArrayList<HashMap<String, Object>>();
        private final HashSet<String> mLocalZones = new HashSet<String>();
        private final Date mNow = Calendar.getInstance().getTime();
        private final SimpleDateFormat mZoneNameFormatter = new SimpleDateFormat("zzzz");

        private List<HashMap<String, Object>> getZones(Context context) {
            for (String olsonId : TimeZoneNames.forLocale(Locale.getDefault())) {
                mLocalZones.add(olsonId);
            }
            try {
                XmlResourceParser xrp = context.getResources().getXml(R.xml.timezones);
                while (xrp.next() != XmlResourceParser.START_TAG) {
                    continue;
                }
                xrp.next();
                while (xrp.getEventType() != XmlResourceParser.END_TAG) {
                    while (xrp.getEventType() != XmlResourceParser.START_TAG) {
                        if (xrp.getEventType() == XmlResourceParser.END_DOCUMENT) {
                            return mZones;
                        }
                        xrp.next();
                    }
                    if (xrp.getName().equals(XMLTAG_TIMEZONE)) {
                        String olsonId = xrp.getAttributeValue(0);
                        addTimeZone(olsonId);
                    }
                    while (xrp.getEventType() != XmlResourceParser.END_TAG) {
                        xrp.next();
                    }
                    xrp.next();
                }
                xrp.close();
            } catch (XmlPullParserException xppe) {
                Log.e(TAG, "Ill-formatted timezones.xml file");
            } catch (java.io.IOException ioe) {
                Log.e(TAG, "Unable to read timezones.xml file");
            }
            return mZones;
        }

        private void addTimeZone(String olsonId) {
            // We always need the "GMT-07:00" string.
            final TimeZone tz = TimeZone.getTimeZone(olsonId);

            // For the display name, we treat time zones within the country differently
            // from other countries' time zones. So in en_US you'd get "Pacific Daylight Time"
            // but in de_DE you'd get "Los Angeles" for the same time zone.
            String displayName;
            if (mLocalZones.contains(olsonId)) {
                // Within a country, we just use the local name for the time zone.
                mZoneNameFormatter.setTimeZone(tz);
                displayName = mZoneNameFormatter.format(mNow);
            } else {
                // For other countries' time zones, we use the exemplar location.
                final String localeName = Locale.getDefault().toString();
                displayName = TimeZoneNames.getExemplarLocation(localeName, olsonId);
            }

            final HashMap<String, Object> map = new HashMap<String, Object>();
            map.put(KEY_ID, olsonId);
            map.put(KEY_DISPLAYNAME, displayName);
            map.put(KEY_GMT, getTimeZoneText(tz, false));
            map.put(KEY_OFFSET, tz.getOffset(mNow.getTime()));

            mZones.add(map);
        }
    }
    
    private static class MyComparator implements Comparator<HashMap<?, ?>> {
        private String mSortingKey;

        public MyComparator(String sortingKey) {
            mSortingKey = sortingKey;
        }

        public void setSortingKey(String sortingKey) {
            mSortingKey = sortingKey;
        }

        public int compare(HashMap<?, ?> map1, HashMap<?, ?> map2) {
            Object value1 = map1.get(mSortingKey);
            Object value2 = map2.get(mSortingKey);

            /*
             * This should never happen, but just in-case, put non-comparable
             * items at the end.
             */
            if (!isComparable(value1)) {
                return isComparable(value2) ? 1 : 0;
            } else if (!isComparable(value2)) {
                return -1;
            }

            return ((Comparable) value1).compareTo(value2);
        }

        private boolean isComparable(Object value) {
            return (value != null) && (value instanceof Comparable);
        }
    }

 

設置時區

AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        alarm.setTimeZone(tzId);

 

時區timezones.xml文件

<timezones>
    <timezone id="Pacific/Majuro"></timezone>
    <timezone id="Pacific/Midway"></timezone>
    <timezone id="Pacific/Honolulu"></timezone>
    <timezone id="America/Anchorage"></timezone>
    <timezone id="America/Los_Angeles"></timezone>
    <timezone id="America/Tijuana"></timezone>
    <timezone id="America/Phoenix"></timezone>
    <timezone id="America/Chihuahua"></timezone>
    <timezone id="America/Denver"></timezone>
    <timezone id="America/Costa_Rica"></timezone>
    <timezone id="America/Chicago"></timezone>
    <timezone id="America/Mexico_City"></timezone>
    <timezone id="America/Regina"></timezone>
    <timezone id="America/Bogota"></timezone>
    <timezone id="America/New_York"></timezone>
    <timezone id="America/Caracas"></timezone>
    <timezone id="America/Barbados"></timezone>
    <timezone id="America/Halifax"></timezone>
    <timezone id="America/Manaus"></timezone>
    <timezone id="America/Santiago"></timezone>
    <timezone id="America/St_Johns"></timezone>
    <timezone id="America/Sao_Paulo"></timezone>
    <timezone id="America/Buenos_Aires"></timezone>
    <timezone id="America/Godthab"></timezone>
    <timezone id="America/Montevideo"></timezone>
    <timezone id="Atlantic/South_Georgia"></timezone>
    <timezone id="Atlantic/Azores"></timezone>
    <timezone id="Atlantic/Cape_Verde"></timezone>
    <timezone id="Africa/Casablanca"></timezone>
    <timezone id="Europe/London"></timezone>
    <timezone id="Europe/Amsterdam"></timezone>
    <timezone id="Europe/Belgrade"></timezone>
    <timezone id="Europe/Brussels"></timezone>
    <timezone id="Europe/Sarajevo"></timezone>
    <timezone id="Africa/Windhoek"></timezone>
    <timezone id="Africa/Brazzaville"></timezone>
    <timezone id="Asia/Amman"></timezone>
    <timezone id="Europe/Athens"></timezone>
    <timezone id="Asia/Beirut"></timezone>
    <timezone id="Africa/Cairo"></timezone>
    <timezone id="Europe/Helsinki"></timezone>
    <timezone id="Asia/Jerusalem"></timezone>
    <timezone id="Europe/Minsk"></timezone>
    <timezone id="Africa/Harare"></timezone>
    <timezone id="Asia/Baghdad"></timezone>
    <timezone id="Europe/Moscow"></timezone>
    <timezone id="Asia/Kuwait"></timezone>
    <timezone id="Africa/Nairobi"></timezone>
    <timezone id="Asia/Tehran"></timezone>
    <timezone id="Asia/Baku"></timezone>
    <timezone id="Asia/Tbilisi"></timezone>
    <timezone id="Asia/Yerevan"></timezone>
    <timezone id="Asia/Dubai"></timezone>
    <timezone id="Asia/Kabul"></timezone>
    <timezone id="Asia/Karachi"></timezone>
    <timezone id="Asia/Oral"></timezone>
    <timezone id="Asia/Yekaterinburg"></timezone>
    <timezone id="Asia/Calcutta"></timezone>
    <timezone id="Asia/Colombo"></timezone>
    <timezone id="Asia/Katmandu"></timezone>
    <timezone id="Asia/Almaty"></timezone>
    <timezone id="Asia/Rangoon"></timezone>
    <timezone id="Asia/Krasnoyarsk"></timezone>
    <timezone id="Asia/Bangkok"></timezone>
    <timezone id="Asia/Jakarta"></timezone>
    <timezone id="Asia/Shanghai"></timezone>
    <timezone id="Asia/Hong_Kong"></timezone>
    <timezone id="Asia/Irkutsk"></timezone>
    <timezone id="Asia/Kuala_Lumpur"></timezone>
    <timezone id="Australia/Perth"></timezone>
    <timezone id="Asia/Taipei"></timezone>
    <timezone id="Asia/Seoul"></timezone>
    <timezone id="Asia/Tokyo"></timezone>
    <timezone id="Asia/Yakutsk"></timezone>
    <timezone id="Australia/Adelaide"></timezone>
    <timezone id="Australia/Darwin"></timezone>
    <timezone id="Australia/Brisbane"></timezone>
    <timezone id="Australia/Hobart"></timezone>
    <timezone id="Australia/Sydney"></timezone>
    <timezone id="Asia/Vladivostok"></timezone>
    <timezone id="Pacific/Guam"></timezone>
    <timezone id="Asia/Magadan"></timezone>
    <timezone id="Pacific/Auckland"></timezone>
    <timezone id="Pacific/Fiji"></timezone>
    <timezone id="Pacific/Tongatapu"></timezone>
    <timezone id="Asia/Manila">Manila</timezone>
</timezones>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章