ZoneId ialah kelas di Java. pakej masa, yang telah diperkenalkan untuk menentukan peraturan yang diperlukan untuk penukaran antara Instant dan LocalDateTime. Kelas ini ialah subkelas kelas ZoneOffset dan juga boleh bersiri kerana ia melaksanakan antara muka Bersiri. Kelas ini menggunakan format tertentu yang digunakan untuk menyimpan offset daripada UTC/Greenwich sahaja. Sebagai kelas berasaskan nilai, penggunaan operasi sensitif identiti mungkin membawa kepada hasil yang tidak dapat diramalkan. Kelas ini mewakili kebanyakan ID ofset tetap yang menggunakan ofset yang sama untuk semua tarikh-masa setempat.
Mulakan Kursus Pembangunan Perisian Percuma Anda
Pembangunan web, bahasa pengaturcaraan, ujian perisian & lain-lain
Berikut ialah sintaks java zoneid:
Sintaks:
public abstract class ZoneId extends Object implements Serializable
Medan: awam statik akhir Peta
Peta ini adalah seperti berikut:
Kaedah java zoneid diberikan di bawah:
Fungsi ini digunakan untuk mendapatkan zon masa lalai sistem. Ini menggunakan fungsi TimeZone.getDefault() untuk mendapatkan semula nilai kepada zon masa lalai untuk sistem itu, yang bermaksud sebarang pengemaskinian yang dibuat kepada Zon Waktu lalai sistem akan ditunjukkan dalam keputusan. Output ditukar kepada format ZoneID. Di bawah 2 pengecualian yang dilemparkan oleh fungsi ini mesti dikendalikan:-
Kod:
import java.time.ZoneId; public class Main { public static void main(String[] args) { ZoneId zone = ZoneId.systemDefault(); System.out.println("Output of systemDefault()-" +zone); } }
Output:
Fungsi ini digunakan untuk mengembalikan semua ID berasaskan wilayah yang tersedia. Senarai itu dikembalikan sebagai set String. Rentetan yang dikembalikan boleh ditukar kepada format Zone-Id menggunakan fungsi Of(String). Offset-Id tidak disertakan dalam hasil set Rentetan.
Kod:
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) ); } } }
Output:
Fungsi ini mengembalikan ZoneID atau ZoneOffset untuk ID sah yang diluluskan sebagai rentetan sebagai output kepada fungsi. ZoneOffset dikembalikan berdasarkan aksara permulaan rentetan (jika ia adalah 'Z' atau '+' atau '-'). ZoneID yang dikembalikan sentiasa mengikut ZoneRules. Penukaran ini berlaku menggunakan algoritma penghuraian lengkap. Sekiranya format hujah tidak sah, DateTimeException dilemparkan dan dalam kes rantau, ID tidak dapat ditemui, maka ZoneRulesException dibuang.
Kod:
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()); } }
Output:
Kaedah ini digunakan untuk mendapatkan ZoneID apabila awalan dan ofset dihantar sebagai argumen. ID Zon dengan awalan dan ID Offset bukan sifar dikembalikan. Dalam kes ini, awalan ialah "" ZoneOffset dikembalikan.
Awalan mestilah ‘GMT’, ‘UTC’ atau ‘UT’ atau ‘’ jika tidak, IllegalArgumentException dilemparkan dengan kaedah tersebut.
Kod:
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:
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:
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:
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:
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:
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:
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:
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:
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:
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.
Atas ialah kandungan terperinci Java ZoneId. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!