android 时间时区,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> mZones =

new ArrayList>();

private final HashSetmLocalZones = 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 HashMapmap = 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值