日期 时间

    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> mZones =
                new ArrayList>();
        private final HashSet mLocalZones = new HashSet();
        private final Date mNow = Calendar.getInstance().getTime();
        private final SimpleDateFormat mZoneNameFormatter = new SimpleDateFormat("zzzz");

        private List> 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 map = new HashMap();
            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> {
        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文件


   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
    Manila

 

更多相关文章

  1. android 弹出日期滑动选择框,日期滚动滑动选择
  2. Android 手机获取时区
  3. android日期控件显示
  4. Android 时间、日期处理 DateUtils、SystemClock、DateFormat
  5. Android仿iPhone的日期时间选择器
  6. Android 自定义日期和时间和星期的弹窗

随机推荐

  1. android requestFocus的使用
  2. Android SyncManager 实现
  3. Android Audio and Video
  4. Android—锁定横屏遇到的问题
  5. Android 设置没有 actionBar的 样式
  6. android ListView 属性
  7. Android发送短信方法实例详解
  8. 安卓9.0 http请求数据失败解决办法
  9. [置顶] android orm映射框架(类似hibernat
  10. Android(安卓)MVVM理解以及运用