格式化时区显示名称(例如 UTC + 01:00)
Formatting time zone display names (e.g. UTC + 01:00)
我有一个微调器,我可以从中选择所需的时区。当我select一个时区时,显示相关的当前时间。
在微调器中,我想在这个配置中设置时区:
例如America/Los_Angeles (UTC-07:00)。
相反,我看到以下内容:
America/Los_Angeles (UTC-28800000)。
这是代码:
String[]TZ = TimeZone.getAvailableIDs();
String NameAndUTC ="";
for(int i = 0; i < TZ.length; i++)
{
NameAndUTC = TimeZone.getTimeZone(TZ[i]).getID() + " (UTC" +
(TimeZone.getTimeZone(TZ[i]).getRawOffset() == 0 ? "+00:00" :
TimeZone.getTimeZone(TZ[i]).getRawOffset()) + ")";
}
我建议您从过时的 error-prone java.util
date-time API 切换到 modern java.time
date-time API. Learn more about the modern date-time API from Trail: Date Time.
如果您的 Android API 级别仍然不符合 Java8,请检查 and Java 8+ APIs available through desugaring。
使用 Java 现代 date-time API:
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
public class Main {
public static void main(String[] args) {
// Get the set of all time zone IDs.
Set<String> allZones = ZoneId.getAvailableZoneIds();
// Create a List using the set of zones and sort it. Now, you can display the
// sorted list in the spinner
List<String> zoneList = new ArrayList<String>(allZones);
Collections.sort(zoneList);
// Select a value from the spinner e.g.
String s = "America/Los_Angeles";
// Get the Zone Id using the selected value from the spinner
ZoneId zone = ZoneId.of(s);
// Date and time at the zone selected from the spinner
ZonedDateTime zdt = ZonedDateTime.now(zone);
// Define a formatter as per your display requirement e.g.
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE MMM dd yyyy HH:mm:ss VV '(UTC'XXX')'");
// Display the date time
System.out.println(zdt.format(formatter));
}
}
输出:
Thu Sep 03 2020 14:47:11 America/Los_Angeles (UTC-07:00)
我有一个微调器,我可以从中选择所需的时区。当我select一个时区时,显示相关的当前时间。 在微调器中,我想在这个配置中设置时区: 例如America/Los_Angeles (UTC-07:00)。 相反,我看到以下内容: America/Los_Angeles (UTC-28800000)。 这是代码:
String[]TZ = TimeZone.getAvailableIDs();
String NameAndUTC ="";
for(int i = 0; i < TZ.length; i++)
{
NameAndUTC = TimeZone.getTimeZone(TZ[i]).getID() + " (UTC" +
(TimeZone.getTimeZone(TZ[i]).getRawOffset() == 0 ? "+00:00" :
TimeZone.getTimeZone(TZ[i]).getRawOffset()) + ")";
}
我建议您从过时的 error-prone java.util
date-time API 切换到 modern java.time
date-time API. Learn more about the modern date-time API from Trail: Date Time.
如果您的 Android API 级别仍然不符合 Java8,请检查
使用 Java 现代 date-time API:
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
public class Main {
public static void main(String[] args) {
// Get the set of all time zone IDs.
Set<String> allZones = ZoneId.getAvailableZoneIds();
// Create a List using the set of zones and sort it. Now, you can display the
// sorted list in the spinner
List<String> zoneList = new ArrayList<String>(allZones);
Collections.sort(zoneList);
// Select a value from the spinner e.g.
String s = "America/Los_Angeles";
// Get the Zone Id using the selected value from the spinner
ZoneId zone = ZoneId.of(s);
// Date and time at the zone selected from the spinner
ZonedDateTime zdt = ZonedDateTime.now(zone);
// Define a formatter as per your display requirement e.g.
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE MMM dd yyyy HH:mm:ss VV '(UTC'XXX')'");
// Display the date time
System.out.println(zdt.format(formatter));
}
}
输出:
Thu Sep 03 2020 14:47:11 America/Los_Angeles (UTC-07:00)