> Java > java지도 시간 > 본문

자바 영역 ID

PHPz
풀어 주다: 2024-08-30 15:50:07
원래의
684명이 탐색했습니다.

ZoneId는 Java의 클래스입니다. Instant와 LocalDateTime 간의 변환에 필요한 규칙을 지정하기 위해 도입된 time 패키지입니다. 이 클래스는 ZoneOffset 클래스의 하위 클래스이며 직렬화 가능 인터페이스를 구현하므로 직렬화 가능합니다. 이 클래스는 UTC/그리니치에서만 오프셋을 저장하는 데 사용되는 특정 형식을 사용합니다. 값 기반 클래스이므로 ID에 민감한 작업을 사용하면 예측할 수 없는 결과가 발생할 수 있습니다. 이 클래스는 모든 현지 날짜-시간에 대해 동일한 오프셋을 사용하는 대부분의 고정 오프셋 ID를 나타냅니다.

무료 소프트웨어 개발 과정 시작

웹 개발, 프로그래밍 언어, 소프트웨어 테스팅 등

Java ZoneId 구문

다음은 Java zoneid의 구문입니다.

구문:

public abstract class ZoneId
extends Object
implements Serializable
로그인 후 복사

필드: public static final Map SHORT_IDS - TZDB 2005r 이상 버전에 맞는 ID 매핑으로 구성된 수정 불가능한 맵입니다.

이 지도는 다음과 같습니다.

  • 동부: -05:00
  • HST: -10:00
  • MST: -07:00
  • ACT: 호주/다윈
  • AET: 호주/시드니
  • AGT: 아메리카/아르헨티나/부에노스아이레스
  • 예술: 아프리카/카이로
  • AST: 미국/앵커리지
  • BET: America/Sao_Paulo
  • BST: 아시아/다카
  • CAT: 아프리카/하라레
  • CNT: America/St_Johns
  • CST: 미국/시카고
  • CTT: 아시아/상하이
  • 음식: 아프리카/아디스아바바
  • ECT: 유럽/파리
  • IET: 아메리카/인디애나/인디애나폴리스
  • IST: 아시아/콜카타
  • JST: 아시아/도쿄
  • MIT:태평양/아피아
  • NET: 아시아/예레반
  • NST: 태평양/오클랜드
  • PLT: 아시아/카라치
  • PNT: 아메리카/피닉스
  • PRT: 아메리카/푸에르토리코
  • PST: 아메리카/로스엔젤레스
  • SST: 태평양/과달카날
  • VST: 아시아/호치민

Java ZoneId 메서드

Java zoneid의 방법은 다음과 같습니다.

1. 공개 정적 ZoneId systemDefault()

이 함수는 시스템 기본 시간대를 가져오는 데 사용됩니다. 이는 TimeZone.getDefault() 함수를 호출하여 해당 시스템의 기본 시간대에 대한 값을 검색합니다. 즉, 시스템 기본 시간대에 대한 모든 업데이트가 결과에 반영됩니다. 출력은 ZoneID 형식으로 변환됩니다. 이 함수에 의해 발생하는 아래 2가지 예외를 처리해야 합니다.-

  • DateTimeException: 이는 변환된 영역 ID의 형식이 잘못되었음을 나타냅니다.
  • ZoneRulesException: 이는 변환된 영역 지역 ID를 찾을 수 없음을 나타냅니다

코드:

import java.time.ZoneId;
public class Main {
public static void main(String[] args) {
ZoneId zone = ZoneId.systemDefault();
System.out.println("Output of systemDefault()-" +zone);
}
}
로그인 후 복사

출력:

자바 영역 ID

2. 공개 정적 Set getAvailableZoneIds()

이 함수는 사용 가능한 모든 지역 기반 ID를 반환하는 데 사용됩니다. 목록은 문자열 집합으로 반환됩니다. 반환된 문자열은 Of(String) 함수를 사용하여 Zone-Id 형식으로 변환될 수 있습니다. Offset-Id는 문자열 집합의 결과에 포함되지 않습니다.

코드:

import java.time.ZoneId;
import java.util.*;
public class Main {
public static void main(String[] args) {
Set<String> zoneIds = ZoneId.getAvailableZoneIds();
List<String> zoneList = new ArrayList<String>(zoneIds);
Collections.sort(zoneList);
for (int i = 0; i < 5; i++) {
System.out.println("ZoneId in list:" +zoneList.get(i) );
}
}
}
로그인 후 복사

출력:

자바 영역 ID

3. 공개 정적 ZoneId of(String zoneId)

이 함수는 함수에 대한 출력으로 문자열로 전달된 유효한 ID에 대해 ZoneID 또는 ZoneOffset을 반환합니다. ZoneOffset은 문자열의 시작 문자('Z', '+' 또는 '-'인 경우)를 기준으로 반환됩니다. 반환되는 ZoneID는 항상 ZoneRules를 따릅니다. 이 변환은 완전한 구문 분석 알고리즘을 사용하여 수행됩니다. 인수의 형식이 유효하지 않은 경우 DateTimeException이 발생하고, 해당 지역의 경우 ID를 찾을 수 없으면 ZoneRulesException이 발생합니다.

코드:

import java.time.*;
import java.util.*;
import java.time.format.TextStyle;
public class Main {
public static void main(String[] args) {
ZoneId zoneId  = ZoneId.of("Asia/Calcutta");
System.out.println("Output of zoneid obtained using of function: "+ zoneId.normalized());
}
}
로그인 후 복사

출력:

자바 영역 ID

4. 공개 정적 ZoneId ofOffset(문자열 접두사, ZoneOffset 오프셋)

이 방법은 접두사와 오프셋이 인수로 전달될 때 ZoneID를 얻는 데 사용됩니다. 접두사가 있고 0이 아닌 오프셋 ID가 있는 영역 ID가 반환됩니다. 이 경우 접두사는 “” ZoneOffset이 반환됩니다.

접두사는 'GMT', 'UTC' 또는 'UT'여야 하며, 그렇지 않으면 메소드에서 IllegalArgumentException이 발생합니다.

코드:

import java.time.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
ZoneId zoneId = ZoneId.ofOffset("GMT", ZoneOffset.MAX);
System.out.println("OfOffset on ZoneId: " + zoneId);
}
}
로그인 후 복사

Ouput:

자바 영역 ID

5. public static ZoneId from(TemporalAccessor temporal)

This method is used to convert a TemporalAccessor, an arbitrary set of date and time, object to an instance of ZoneId. This function works in a similar manner to TemporalQueries.zone() that extracts offset base zone id. Thus this method matches the signature of Temporal Query that is being used via ZoneId::from() function.

Code:

import java.time.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
ZonedDateTime zoneddatetime
= ZonedDateTime.parse("2020-02-25T23:12:31.123+02:00[Europe/Paris]");
ZoneId result = ZoneId.from(zoneddatetime);
System.out.println("Zone Id got from "+ "TemporalAccessor object \n"
+ zoneddatetime + "\nis " + result);
}
}
로그인 후 복사

Output:

자바 영역 ID

6. public abstract String getId()

This function is used to get a unique time-zone ID for a particular object. The format for an offset-based ID is defined by the getId() method in the Timezone class. The ID given as an output helps to uniquely define the object.

Code:

import java.time.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
ZoneId zoneId  = ZoneId.of("Asia/Calcutta");
System.out.println("Id using getID(): "+ zoneId.getId());
}
}
로그인 후 복사

Output:

자바 영역 ID

7. public String getDisplayName(TextStyle style,Locale locale)

This method helps to provide the textual representation of the zone. The style required as well as the locale of the output required is given as arguments. And thus, the suitable textual representation of the time-zone id is given as an output. In case no textual representation is present, a complete ID is returned as an output.

Code:

import java.time.*;
import java.util.*;
import java.time.format.TextStyle;
public class Main {
public static void main(String[] args) {
ZoneId zoneId  = ZoneId.of("Asia/Calcutta");
String result = zoneId.getDisplayName(TextStyle.SHORT, Locale.ENGLISH);
System.out.println("Name of ID using getDisplayName(): "+ result);
}
}
로그인 후 복사

Output:

자바 영역 ID

8. public abstract ZoneRules getRules()

This function is used to retrieve the rules that are applied for the given ID. These rules give an idea about the calculations to be performed as well as the functionality associated with time-zone like finding an offset for an instant. ZoneRulesProvider supplies these rules. An advanced provider can also make a dynamic update to these rules, but the results may change over time in this case. In case no rule is available or the rules of JRE is different from that of time-zone, a call made to this function may lead to ZoneRulesException.

Code:

import java.time.*;
import java.util.*;
import java.time.format.TextStyle;
public class Main {
public static void main(String[] args) {
ZoneId zoneId  = ZoneId.of("Asia/Calcutta");
System.out.println("Rules are: "+ zoneId.getRules());
}
}
로그인 후 복사

Output:

자바 영역 ID

9. public ZoneId normalized()

This method is used to check if the given zoneID contains a fixed Offset and if yes, a ZOneOffset equal to that fixed offset is returned; otherwise, this is returned. The returned ID will also have ZoneRules similar to the given offset, but the result we get from the getId() function is different from what we get here.

Code:

import java.time.*;
import java.util.*;
import java.time.format.TextStyle;
public class Main {
public static void main(String[] args) {
ZoneId zoneId  = ZoneId.of("Asia/Calcutta");
System.out.println("Normalized Id: "+ zoneId.normalized());
}
}
로그인 후 복사

Output:

자바 영역 ID

10. public boolean equals(Object obj)

This method helps to compare 2 different times ZoneId objects. This method overrides the equals method in the Object class, which is used to compare value stored in 2 objects. Similarly, here the value of these ZoneID is compared to see if 2 objects are equal or not. And, Accordingly, true and false is returned.

Code:

import java.time.*;
import java.util.*;
import java.time.format.TextStyle;
public class Main {
public static void main(String[] args) {
ZoneId zoneId  = ZoneId.of("Asia/Calcutta");
ZoneId zoneId2 = ZoneId.of("Europe/Paris");
System.out.println("Output of Equals: "+ zoneId.equals(zoneId2));
}
}
로그인 후 복사

Output:

자바 영역 ID

11. public int hashCode()

This function is used to get the hash code for a particular ZoneID object. This hashcode is returned in int format.

Code:

import java.time.*;
import java.util.*;
import java.time.format.TextStyle;
public class Main {
public static void main(String[] args) {
ZoneId zoneId  = ZoneId.of("Asia/Calcutta");
ZoneId zoneId2 = ZoneId.of("Europe/Paris");
System.out.println("Output of zoneid hashCode: "+ zoneId.hashCode());
System.out.println("Output of zoneid2 hashCode: "+ zoneId2.hashCode());
}
}
로그인 후 복사

Output:

자바 영역 ID

12. public String toString()

This function is used to get the string representation of the time -zone ID that is stored in the particular Offset ID. This function overrides the method toString() of Object class.

Code:

import java.time.*;
import java.util.*;
import java.time.format.TextStyle;
public class Main {
public static void main(String[] args) {
ZoneId zoneId  = ZoneId.of("Asia/Calcutta");
ZoneId zoneId2 = ZoneId.of("Europe/Paris");
System.out.println("Output of zoneid toString: "+ zoneId.toString());
System.out.println("Output of zoneid2 toString: "+ zoneId2.toString());
}
}
로그인 후 복사

Output:

자바 영역 ID

Conclusion

ZoneID is a serialized class that is used to store the IDs that identify different ZoneRules that the government frequently updates. Thus at the time of serialization, rules are used instead as they contain the entire data set. Also, calling normalized() on ZoneId returns fixed offset ID in the format of ZoneOffset.

위 내용은 자바 영역 ID의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
원천:php
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!