将这个类完全复制
public class ZoneGetter {
private static final String TAG = "ZoneGetter";
public static final String KEY_ID = "id"; // value: String
@Deprecated
public static final String KEY_DISPLAYNAME = "name"; // value: String
public static final String KEY_DISPLAY_LABEL = "display_label"; // value: CharSequence
@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: CharSequence
private 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
将下面的方法放到主函数中,下边的方法调用上边的那个类
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 olsonIdsToDisplayList = ZoneGetter.readTimezonesToDisplay(mCtx);
// Load all the data needed to display time zones
zoneCount = 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;
}
}
}