将这个类完全复制

public class ZoneGetter {private static final String TAG = "ZoneGetter";public static final String KEY_ID = "id";  // value: String/*** @deprecated Use {@link #KEY_DISPLAY_LABEL} instead.*/
@Deprecated
public static final String KEY_DISPLAYNAME = "name";  // value: Stringpublic static final String KEY_DISPLAY_LABEL = "display_label"; // value: CharSequence/*** @deprecated Use {@link #KEY_OFFSET_LABEL} instead.*/
@Deprecated
public static final String KEY_GMT = "gmt";  // value: String
public static final String KEY_OFFSET = "offset";  // value: int (Integer)
public static final String KEY_OFFSET_LABEL = "offset_label";  // value: CharSequenceprivate static final String XMLTAG_TIMEZONE = "timezone";
@RequiresApi(api = Build.VERSION_CODES.N)public static CharSequence getTimeZoneOffsetAndName(Context context, TimeZone tz, Date now) {Locale locale = context.getResources().getConfiguration().locale;TimeZoneFormat tzFormatter = TimeZoneFormat.getInstance(locale);CharSequence gmtText = getGmtOffsetText(tzFormatter, locale, tz, now);TimeZoneNames timeZoneNames = TimeZoneNames.getInstance(locale);String zoneNameString = getZoneLongName(timeZoneNames, tz, now);if (zoneNameString == null) {return gmtText;}// We don't use punctuation here to avoid having to worry about localizing that too!return TextUtils.concat(gmtText, " ", zoneNameString);}@RequiresApi(api = Build.VERSION_CODES.N)
public static List<Map<String, Object>> getZonesList(Context context) {final Locale locale = context.getResources().getConfiguration().locale;final Date now = new Date();final TimeZoneNames timeZoneNames = TimeZoneNames.getInstance(locale);final ZoneGetterData data = new ZoneGetterData(context);// Work out whether the display names we would show by default would be ambiguous.final boolean useExemplarLocationForLocalNames =shouldUseExemplarLocationForLocalNames(data, timeZoneNames);// Generate the list of zone entries to return.List<Map<String, Object>> zones = new ArrayList<Map<String, Object>>();for (int i = 0; i < data.zoneCount; i++) {TimeZone tz = data.timeZones[i];CharSequence gmtOffsetText = data.gmtOffsetTexts[i];CharSequence displayName = getTimeZoneDisplayName(data, timeZoneNames,useExemplarLocationForLocalNames, tz, data.olsonIdsToDisplay[i]);if (TextUtils.isEmpty(displayName)) {displayName = gmtOffsetText;}int offsetMillis = tz.getOffset(now.getTime());Map<String, Object> displayEntry =createDisplayEntry(tz, gmtOffsetText, displayName, offsetMillis);zones.add(displayEntry);}return zones;
}
private static Map<String, Object> createDisplayEntry(TimeZone tz, CharSequence gmtOffsetText, CharSequence displayName, int offsetMillis) {Map<String, Object> map = new HashMap<>();map.put(KEY_ID, tz.getID());map.put(KEY_DISPLAYNAME, displayName.toString());map.put(KEY_DISPLAY_LABEL, displayName);map.put(KEY_GMT, gmtOffsetText.toString());map.put(KEY_OFFSET_LABEL, gmtOffsetText);map.put(KEY_OFFSET, offsetMillis);return map;
}
public static List<String> readTimezonesToDisplay(Context context) {List<String> olsonIds = new ArrayList<String>();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 olsonIds;}xrp.next();}if (xrp.getName().equals(XMLTAG_TIMEZONE)) {String olsonId = xrp.getAttributeValue(0);olsonIds.add(olsonId);}while (xrp.getEventType() != XmlResourceParser.END_TAG) {xrp.next();}xrp.next();}} 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 olsonIds;
}
@RequiresApi(api = Build.VERSION_CODES.N)
private static boolean shouldUseExemplarLocationForLocalNames(ZoneGetterData data,TimeZoneNames timeZoneNames) {final Set<CharSequence> localZoneNames = new HashSet<>();final Date now = new Date();for (int i = 0; i < data.zoneCount; i++) {final String olsonId = data.olsonIdsToDisplay[i];if (data.localZoneIds.contains(olsonId)) {final TimeZone tz = data.timeZones[i];CharSequence displayName = getZoneLongName(timeZoneNames, tz, now);if (displayName == null) {displayName = data.gmtOffsetTexts[i];}final boolean nameIsUnique = localZoneNames.add(displayName);if (!nameIsUnique) {return true;}}}return false;
}
@RequiresApi(api = Build.VERSION_CODES.N)
private static CharSequence getTimeZoneDisplayName(ZoneGetterData data,TimeZoneNames timeZoneNames, boolean useExemplarLocationForLocalNames, TimeZone tz,String olsonId) {final Date now = new Date();final boolean isLocalZoneId = data.localZoneIds.contains(olsonId);final boolean preferLongName = isLocalZoneId && !useExemplarLocationForLocalNames;String displayName;if (preferLongName) {displayName = getZoneLongName(timeZoneNames, tz, now);} else {// Canonicalize the zone ID for ICU. It will only return valid strings for zone IDs// that match ICUs zone IDs (which are similar but not guaranteed the same as those// in timezones.xml). timezones.xml and related files uses the IANA IDs. ICU IDs are// stable and IANA IDs have changed over time so they have drifted.// See http://bugs.icu-project.org/trac/ticket/13070 / http://b/36469833.String canonicalZoneId = android.icu.util.TimeZone.getCanonicalID(tz.getID());if (canonicalZoneId == null) {canonicalZoneId = tz.getID();}displayName = timeZoneNames.getExemplarLocationName(canonicalZoneId);if (displayName == null || displayName.isEmpty()) {// getZoneExemplarLocation can return null. Fall back to the long name.displayName = getZoneLongName(timeZoneNames, tz, now);}}return displayName;
}/*** Returns the long name for the timezone for the given locale at the time specified.* Can return {@code null}.*/
@RequiresApi(api = Build.VERSION_CODES.N)
private static String getZoneLongName(TimeZoneNames names, TimeZone tz, Date now) {final TimeZoneNames.NameType nameType =tz.inDaylightTime(now) ? TimeZoneNames.NameType.LONG_DAYLIGHT: TimeZoneNames.NameType.LONG_STANDARD;return names.getDisplayName(tz.getID(), nameType, now.getTime());
}
private static void appendWithTtsSpan(SpannableStringBuilder builder, CharSequence content,TtsSpan span) {int start = builder.length();builder.append(content);builder.setSpan(span, start, builder.length(), 0);
}
// Input must be positive. minDigits must be 1 or 2.
private static String formatDigits(int input, int minDigits, String localizedDigits) {final int tens = input / 10;final int units = input % 10;StringBuilder builder = new StringBuilder(minDigits);if (input >= 10 || minDigits == 2) {builder.append(localizedDigits.charAt(tens));}builder.append(localizedDigits.charAt(units));return builder.toString();
}
@SuppressLint("NewApi")
@RequiresApi(api = Build.VERSION_CODES.N)
public static CharSequence getGmtOffsetText(TimeZoneFormat tzFormatter, Locale locale,TimeZone tz, Date now) {final SpannableStringBuilder builder = new SpannableStringBuilder();final String gmtPattern = tzFormatter.getGMTPattern();final int placeholderIndex = gmtPattern.indexOf("{0}");final String gmtPatternPrefix, gmtPatternSuffix;if (placeholderIndex == -1) {// Bad pattern. Replace with defaults.gmtPatternPrefix = "GMT";gmtPatternSuffix = "";} else {gmtPatternPrefix = gmtPattern.substring(0, placeholderIndex);gmtPatternSuffix = gmtPattern.substring(placeholderIndex + 3); // After the "{0}".}if (!gmtPatternPrefix.isEmpty()) {appendWithTtsSpan(builder, gmtPatternPrefix,new TtsSpan.TextBuilder(gmtPatternPrefix).build());}int offsetMillis = tz.getOffset(now.getTime());final boolean negative = offsetMillis < 0;final TimeZoneFormat.GMTOffsetPatternType patternType;if (negative) {offsetMillis = -offsetMillis;patternType = TimeZoneFormat.GMTOffsetPatternType.NEGATIVE_HM;} else {patternType = TimeZoneFormat.GMTOffsetPatternType.POSITIVE_HM;}final String gmtOffsetPattern = tzFormatter.getGMTOffsetPattern(patternType);final String localizedDigits = tzFormatter.getGMTOffsetDigits();final int offsetHours = (int) (offsetMillis / DateUtils.HOUR_IN_MILLIS);final int offsetMinutes = (int) (offsetMillis / DateUtils.MINUTE_IN_MILLIS);final int offsetMinutesRemaining = Math.abs(offsetMinutes) % 60;for (int i = 0; i < gmtOffsetPattern.length(); i++) {char c = gmtOffsetPattern.charAt(i);if (c == '+' || c == '-' || c == '\u2212' /* MINUS SIGN */) {final String sign = String.valueOf(c);appendWithTtsSpan(builder, sign, new TtsSpan.VerbatimBuilder(sign).build());} else if (c == 'H' || c == 'm') {final int numDigits;if (i + 1 < gmtOffsetPattern.length() && gmtOffsetPattern.charAt(i + 1) == c) {numDigits = 2;i++; // Skip the next formatting character.} else {numDigits = 1;}final int number;final String unit;if (c == 'H') {number = offsetHours;unit = "hour";} else { // c == 'm'number = offsetMinutesRemaining;unit = "minute";}appendWithTtsSpan(builder, formatDigits(number, numDigits, localizedDigits),new TtsSpan.MeasureBuilder().setNumber(number).setUnit(unit).build());} else {builder.append(c);}}if (!gmtPatternSuffix.isEmpty()) {appendWithTtsSpan(builder, gmtPatternSuffix,new TtsSpan.TextBuilder(gmtPatternSuffix).build());}CharSequence gmtText = new SpannableString(builder);// Ensure that the "GMT+" stays with the "00:00" even if the digits are RTL.final BidiFormatter bidiFormatter = BidiFormatter.getInstance();boolean isRtl = TextUtils.getLayoutDirectionFromLocale(locale) == View.LAYOUT_DIRECTION_RTL;//gmtText = bidiFormatter.unicodeWrap(gmtText, isRtl ? TextDirectionHeuristicsCompat.RTL : TextDirectionHeuristicsCompat.LTR);gmtText = bidiFormatter.unicodeWrap(gmtText, isRtl ? TextDirectionHeuristics.RTL : TextDirectionHeuristics.LTR);return gmtText;}@VisibleForTestingpublic static final class ZoneGetterData {public final String[] olsonIdsToDisplay;public final CharSequence[] gmtOffsetTexts;public final TimeZone[] timeZones;public final Set<String> localZoneIds;public final int zoneCount;@RequiresApi(api = Build.VERSION_CODES.N)public ZoneGetterData(Context context) {final Locale locale = context.getResources().getConfiguration().locale;final TimeZoneFormat tzFormatter = TimeZoneFormat.getInstance(locale);final Date now = new Date();final List<String> olsonIdsToDisplayList = readTimezonesToDisplay(context);// Load all the data needed to display time zoneszoneCount = olsonIdsToDisplayList.size();olsonIdsToDisplay = new String[zoneCount];timeZones = new TimeZone[zoneCount];gmtOffsetTexts = new CharSequence[zoneCount];for (int i = 0; i < zoneCount; i++) {final String olsonId = olsonIdsToDisplayList.get(i);olsonIdsToDisplay[i] = olsonId;final TimeZone tz = TimeZone.getTimeZone(olsonId);timeZones[i] = tz;gmtOffsetTexts[i] = getGmtOffsetText(tzFormatter, locale, tz, now);}// Create a lookup of local zone IDs.final List<String> zoneIds = lookupTimeZoneIdsByCountry(locale.getCountry());localZoneIds = new HashSet<>(zoneIds);}@VisibleForTestingpublic List<String> lookupTimeZoneIdsByCountry(String country) {return TimeZoneFinder.getInstance().lookupTimeZoneIdsByCountry(country);}}}

将下面的方法放到主函数中,下边的方法调用上边的那个类

String[] olsonIdsToDisplay;
CharSequence[] gmtOffsetTexts;
TimeZone[] timeZones;
int zoneCount;
@SuppressLint("NewApi")public void getTimeZone(String timeZone) {ZoneGetter.ZoneGetterData zoneGetterData = new ZoneGetter.ZoneGetterData(mCtx);final Locale locale = mCtx.getResources().getConfiguration().locale;final TimeZoneFormat tzFormatter = TimeZoneFormat.getInstance(locale);final Date now = new Date();final List<String> olsonIdsToDisplayList = ZoneGetter.readTimezonesToDisplay(mCtx);// Load all the data needed to display time zoneszoneCount = olsonIdsToDisplayList.size();olsonIdsToDisplay = new String[zoneCount];timeZones = new TimeZone[zoneCount];gmtOffsetTexts = new CharSequence[zoneCount];for (int i = 0; i < zoneCount; i++) {final String olsonId = olsonIdsToDisplayList.get(i);olsonIdsToDisplay[i] = olsonId;final TimeZone tz = TimeZone.getTimeZone(olsonId);timeZones[i] = tz;gmtOffsetTexts[i] = ZoneGetter.getGmtOffsetText(tzFormatter, locale, tz, now);System.out.println(timeZones[i].getID() + "---->" + gmtOffsetTexts[i]);if (timeZone.equals(gmtOffsetTexts[i].toString())) {((AlarmManager) mCtx.getSystemService(Context.ALARM_SERVICE)).setTimeZone(timeZones[i].getID());return;}}}

rk3399_9.0.1_mid 时区转换相关推荐

  1. java时区转换 夏令时_实现时区的转换--涉及到冬令时和夏令时的时候

    #时区转换 主要是用来转换时区用的,特别涉及到冬令时和夏令时的区域,这样会很烦,所以需要用个方法来转换 这个主要是创建一些模型 using System; using System.Collectio ...

  2. 东八区转为0时区_Java时区转换及时间格式

    本文介绍Java API 中 Date, Calendar, TimeZone和DateFormat的使用,以及不同时区时间相互转化的方法和原理. 问题描述: 向处于不同时区的服务器发请求时需要考虑时 ...

  3. 时区转换,元数据0区转东八8区

    /*** @Title: 时区转换0->+8区 东八区* @Description:* @author pengbin <pengbin>* @date 2020/4/10 12:1 ...

  4. 用时间戳判断两个时间是否在同一天和时区转换问题

    用时间戳判断两个时间是否在同一天和时区转换问题 //用时间戳判断两个时间是否在同一天和时区转换问题#include "stdafx.h" #include<stdio.h&g ...

  5. timestamp 转换 mysql_技术分享 | MySQL:timestamp 时区转换导致 CPU %sys 高的问题

    作者:高鹏 文章末尾有他著作的<深入理解 MySQL 主从原理 32 讲>,深入透彻理解 MySQL 主从,GTID 相关技术知识.本文为学习记录,可能有误请谅解. 这个问题是一个朋友遇到 ...

  6. date转timestamp格式_技术分享 | MySQL:timestamp 时区转换导致 CPU %sys 高的问题

    作者:高鹏 文章末尾有他著作的<深入理解 MySQL 主从原理 32 讲>,深入透彻理解 MySQL 主从,GTID 相关技术知识. 本文为学习记录,可能有误请谅解. 本文建议PC端观看, ...

  7. date转timestamp格式_技术分享 | MySQL:timestamp 时区转换导致 CPU %sy 高的问题

    作者:高鹏文章末尾有他著作的<深入理解 MySQL 主从原理 32 讲>,深入透彻理解 MySQL 主从,GTID 相关技术知识.本文为学习记录,可能有误请谅解. 本文建议PC端观看,效果 ...

  8. Bailian2966 时区转换【时区计算】

    2966:时区转换 总时间限制: 1000ms 内存限制: 65536kB 描述 直到19世纪,时间校准是一个纯粹的地方现象.每一个村庄当太阳升到最高点的时候把他们的时钟调到中午12点.一个钟表制造商 ...

  9. JavaScript 时间、时区转换

    一.时间格式转换 扩展JavaScript 的对象,添加字符串格式化函数 // 对Date的扩展,将 Date 转化为指定格式的String // 月(M).日(d).小时(h).分(m).秒(s). ...

  10. Android9.0 本地时区和本地时间的自动更新机制

    Android9.0 本地时区和本地时间的自动更新机制 简介 现在Android通过网络同步时间有两种方式:NITZ和NTP,它们使用的条件不同,可以获取的信息也不一样:勾选自动同步功能后,手机首先会 ...

最新文章

  1. 悠悠二十载,Nginx创始人Igor宣布离职
  2. 服务器空闲搭建什么网站,空闲的云服务器可以干什么
  3. MATLAB-基本语法
  4. 《学习之道》第七章总结
  5. java的自增自减_Java中自增和自减操作符(++/--)的那些事
  6. 2019昆明计算机会议,计算机 | ACSAC 2019等国际会议信息6条
  7. vim自带的练习教程(vimtutor)
  8. xadmin入门使用
  9. CentOS7文本模式下配置及安装KVM虚拟机
  10. 代码管理学:凡事养成登记的做法,比如依赖库
  11. java人员工作建议_给JAVA设计开发新手的一些建议和意见(1)
  12. 《游戏设计信条》【笔记】
  13. 数字延时网络混响算法研究(FDN)
  14. linux 使用VI命令怎么删除输入内容,linux系统vi编辑器常用命令及使用方法。
  15. 【UML建模案例】小型网上书店系统
  16. win7站点服务器配置,IIS 7.0安装配置方法图文教程(win7)
  17. Hdu-5769 Substring (SA后缀数组)
  18. 泛函分析在计算机科学中的应用,泛函分析 - 重庆师范大学数学科学学院.doc
  19. 植物大战僵尸修改向日葵无限吐阳光详细步骤~包含排除不符合条件的地址的讲解,以及如何观察地址情况等
  20. 2019成长复盘2020成长规划

热门文章

  1. 卷积神经网络表情识别,神经网络动作识别
  2. 【原创】关于2013、2014款Macbook Air安装Windows的那点事
  3. JS实现星星评分系统
  4. matlab编程求解一元高次方程
  5. php段子老板,程序员段子 那些关于程序员的段子
  6. android系统运行缓慢,安卓手机运行速度慢怎么办 手机运行速度慢如何解决 - WiFi共享大师...
  7. 启动计算机按住del不放,电脑黑屏bios界面都进不去怎么办
  8. 2021年全国职业院校技能大赛 “大数据技术与应用”—模拟赛题(四)
  9. php计算用户留存,留存率到底有几种计算方式
  10. 多变量微积分笔记(2)——多元函数及其微分