Java : ZoneId con ejemplos
ZoneId (Java SE 21 & JDK 21) en Java con ejemplos.
Encontrará ejemplos de código en la mayoría de los métodos de ZoneId.
Nota :
- Este artículo puede utilizar software de traducción para su comodidad. Consulte también la versión original en inglés.
Summary
final var locale = Locale.getDefault();
System.out.println(locale.toLanguageTag()); // en-US
final var zone = ZoneId.systemDefault();
System.out.println(zone); // America/Los_Angeles
final var dateTime = ZonedDateTime.of(
LocalDate.of(2100, 1, 2),
LocalTime.of(0, 0),
zone);
System.out.println(dateTime); // 2100-01-02T00:00-08:00[America/Los_Angeles]
final var zone1 = ZoneId.of("America/Los_Angeles");
System.out.println(zone1); // America/Los_Angeles
final var zone2 = ZoneId.of("PST", ZoneId.SHORT_IDS);
System.out.println(zone2); // America/Los_Angeles
final var zone3 = ZoneId.of("Asia/Tokyo");
System.out.println(zone3); // Asia/Tokyo
Fields
static final Map<String,String> SHORT_IDS
System.out.println("-- short ids --");
ZoneId.SHORT_IDS.entrySet().stream().sorted(Map.Entry.comparingByKey()).forEach(
entry -> {
System.out.println(entry);
}
);
// Result
// ↓
//-- short ids --
//ACT=Australia/Darwin
//AET=Australia/Sydney
//AGT=America/Argentina/Buenos_Aires
//ART=Africa/Cairo
//AST=America/Anchorage
//BET=America/Sao_Paulo
//BST=Asia/Dhaka
//CAT=Africa/Harare
//CNT=America/St_Johns
//CST=America/Chicago
//CTT=Asia/Shanghai
//EAT=Africa/Addis_Ababa
//ECT=Europe/Paris
//EST=-05:00
//HST=-10:00
//IET=America/Indiana/Indianapolis
//IST=Asia/Kolkata
//JST=Asia/Tokyo
//MIT=Pacific/Apia
//MST=-07:00
//NET=Asia/Yerevan
//NST=Pacific/Auckland
//PLT=Asia/Karachi
//PNT=America/Phoenix
//PRT=America/Puerto_Rico
//PST=America/Los_Angeles
//SST=Pacific/Guadalcanal
//VST=Asia/Ho_Chi_Minh
Methods
boolean equals (Object obj)
final var zone1 = ZoneId.of("America/Los_Angeles");
System.out.println(zone1); // America/Los_Angeles
final var zone2 = ZoneId.of("PST", ZoneId.SHORT_IDS);
System.out.println(zone2); // America/Los_Angeles
System.out.println(zone1.equals(zone2)); // true
final var zone1 = ZoneId.ofOffset("GMT", ZoneOffset.ofHours(12));
System.out.println(zone1); // GMT+12:00
final var zone2 = ZoneId.ofOffset("GMT", ZoneOffset.ofHours(3));
System.out.println(zone2); // GMT+03:00
final var zone3 = ZoneId.of("GMT");
System.out.println(zone3); // GMT
System.out.println(zone1.equals(zone2)); // false
System.out.println(zone1.equals(zone3)); // false
System.out.println(zone1.equals(zone3)); // false
static ZoneId from (TemporalAccessor temporal)
final var zone = ZoneId.of("Etc/GMT+1");
System.out.println(zone); // Etc/GMT+1
final var temporal = ZonedDateTime.of(2100, 4, 7, 12, 30, 45, 0, zone);
System.out.println(temporal); // 2100-04-07T12:30:45-01:00[Etc/GMT+1]
System.out.println(ZoneId.from(temporal)); // Etc/GMT+1
static Set<String> getAvailableZoneIds ()
System.out.println("-- available ids --");
ZoneId.getAvailableZoneIds().stream().sorted().forEach(id -> {
System.out.println(id);
});
// Result
// ↓
//-- available ids --
//Africa/Abidjan
//Africa/Accra
//Africa/Addis_Ababa
//Africa/Algiers
//Africa/Asmara
//Africa/Asmera
//Africa/Bamako
// .
// .
// .
String getDisplayName (TextStyle style, Locale locale)
final var zone = ZoneId.systemDefault();
System.out.println(zone); // America/Los_Angeles
final var ret1 = zone.getDisplayName(TextStyle.FULL, Locale.ENGLISH);
System.out.println(ret1); // Pacific Time
final var ret2 = zone.getDisplayName(TextStyle.SHORT, Locale.ENGLISH);
System.out.println(ret2); // PT
final var ret3 = zone.getDisplayName(TextStyle.NARROW, Locale.ENGLISH);
System.out.println(ret3); // America/Los_Angeles
abstract String getId ()
final var zone1 = ZoneId.of("Etc/GMT+1");
System.out.println(zone1.getId()); // Etc/GMT+1
final var zone2 = ZoneId.of("PST", ZoneId.SHORT_IDS);
System.out.println(zone2.getId()); // America/Los_Angeles
abstract ZoneRules getRules ()
final var rules = ZoneId.of("America/Los_Angeles").getRules();
System.out.println(rules); // ZoneRules[currentStandardOffset=-08:00]
final var rules = ZoneId.of("Asia/Tokyo").getRules();
System.out.println(rules); // ZoneRules[currentStandardOffset=+09:00]
int hashCode ()
final var zone1 = ZoneId.of("Etc/GMT+1");
System.out.println(zone1.hashCode()); // -775703111
final var zone2 = ZoneId.of("PST", ZoneId.SHORT_IDS);
System.out.println(zone2.hashCode()); // -1536188513
ZoneId normalized ()
final var zone = ZoneId.ofOffset("GMT", ZoneOffset.ofHours(12));
System.out.println(zone); // GMT+12:00
System.out.println(zone.normalized()); // +12:00
final var zone = ZoneId.of("America/Los_Angeles");
System.out.println(zone); // America/Los_Angeles
System.out.println(zone.normalized()); // America/Los_Angeles
static ZoneId of (String zoneId)
System.out.println(ZoneId.of("America/Los_Angeles")); // America/Los_Angeles
System.out.println(ZoneId.of("Asia/Tokyo")); // Asia/Tokyo
System.out.println(ZoneId.of("+02:00")); // +02:00
System.out.println(ZoneId.of("-12:00")); // -12:00
System.out.println(ZoneId.of("Z")); // Z
System.out.println(ZoneId.of("UTC")); // UTC
System.out.println(ZoneId.of("GMT")); // GMT
System.out.println(ZoneId.of("UTC+09:00")); // UTC+09:00
static ZoneId of (String zoneId, Map<String,String> aliasMap)
final var zone1 = ZoneId.of("PST", ZoneId.SHORT_IDS);
System.out.println(zone1); // America/Los_Angeles
final var zone2 = ZoneId.of("JST", ZoneId.SHORT_IDS);
System.out.println(zone2); // Asia/Tokyo
try {
final var zone = ZoneId.of("JST");
} catch (ZoneRulesException e) {
System.out.println("ZoneRulesException! : " + e.getMessage());
}
// Result
// ↓
//ZoneRulesException! : Unknown time-zone ID: JST
static ZoneId ofOffset (String prefix, ZoneOffset offset)
final var zone1 = ZoneId.ofOffset("UTC", ZoneOffset.ofHours(4));
System.out.println(zone1); // UTC+04:00
final var zone2 = ZoneId.ofOffset("GMT", ZoneOffset.ofHours(-3));
System.out.println(zone2); // GMT-03:00
final var zone3 = ZoneId.ofOffset("UT", ZoneOffset.of("+09:30:15"));
System.out.println(zone3); // UT+09:30:15
static ZoneId systemDefault ()
final var locale = Locale.getDefault();
System.out.println(locale.toLanguageTag()); // en-US
final var zone = ZoneId.systemDefault();
System.out.println(zone); // America/Los_Angeles
String toString ()
final var str1 = ZoneId.of("America/Los_Angeles").toString();
System.out.println(str1); // America/Los_Angeles
final var str2 = ZoneId.ofOffset("GMT", ZoneOffset.ofHours(12)).toString();
System.out.println(str2); // GMT+12:00
Related posts
- Ejemplos de API
- Calendar
- ChronoLocalDate
- ChronoLocalDateTime
- ChronoZonedDateTime
- Clock
- Date
- DateTimeException
- DateTimeParseException
- DayOfWeek
- Duration
- Era
- Instant
- InstantSource
- JapaneseDate
- LocalDate
- LocalDateTime
- LocalTime
- Month
- MonthDay
- OffsetDateTime
- OffsetTime
- Period
- Temporal
- TemporalAccessor
- TemporalAdjuster
- TemporalAdjusters
- TimeZone
- Year
- YearMonth
- ZonedDateTime
- ZoneOffset