Tempoh dalam java ialah kelas yang digunakan untuk mengukur masa dalam saat dan nanosaat. Pakej kelas tempoh dalam java ialah java.time.Duration. Objek kelas Tempoh menentukan tempoh masa atau digunakan untuk menentukan perbezaan antara dua masa. Objek Tempoh tidak boleh diubah dan selamat untuk benang juga kerana objek Tempoh tidak berubah, jadi kami tidak boleh menukar nilainya sebaik sahaja ia dibuat. Tetapi, kita boleh mencipta objek Tempoh baharu berdasarkan objek Tempoh lain. Kelas Tempoh mewarisi kelas objek ( kerana objek ialah kelas super semua kelas dalam java) dan melaksanakan antara muka Sebanding.
Mulakan Kursus Pembangunan Perisian Percuma Anda
Pembangunan web, bahasa pengaturcaraan, ujian perisian & lain-lain
Sintaks
Sintaks pengisytiharan kelas Tempoh dalam java ialah:
public final class Duration extends Object implements Comparable < Duration >, TemporalAmount, Serializable { // variables and method of the class Duration}
Senarai kaedah kelas Tempoh diterangkan di bawah dengan contoh kod; kod contoh boleh digunakan lagi untuk kaedah yang serupa (seperti untuk setiap kod contoh kaedah yang tidak diberikan):
Kami memahami kaedah di atas dengan contoh kod java di bawah.
Kod:
package p1; import java.time.Duration; import java.time.LocalTime; import java.time.temporal.ChronoUnit; public class DurationClassDemo { public static void main(String[] args) { Duration d = Duration.between(LocalTime.MAX,LocalTime.MIN); System.out.println(d.get(ChronoUnit.SECONDS)); Duration absd = d.abs(); System.out.println(absd.get(ChronoUnit.SECONDS)); } }
Output:
Kami memahami kaedah di atas dengan contoh kod java di bawah:
Kod:
package p1; import java.time.Duration; import java.time.*; import java.time.temporal.ChronoUnit; public class DurationClassDemo { public static void main(String[] args) { Duration d = Duration.between(LocalTime.MAX,LocalTime.MIN); LocalDateTime date = LocalDateTime.now(); System.out.println(date); date = (LocalDateTime)d.addTo(date); System.out.println(date); Duration d1 = d.dividedBy(4); System.out.println(d1); System.out.println(d.getSeconds()); System.out.println(d1.getSeconds()); } }
Output:
Kami memahami kaedah di atas dengan contoh kod java di bawah:
Kod:
package p1; import java.time.Duration; import java.time.*; import java.time.temporal.ChronoUnit; public class DurationClassDemo { public static void main(String[] args) { Duration d = Duration.between(LocalTime.NOON,LocalTime.MAX); Duration d1 = Duration.between(LocalTime.NOON,LocalTime.MIN); System.out.println(d1.getSeconds()); System.out.println(d.compareTo(d1)); System.out.println(d1.compareTo(d1)); System.out.println(d1.compareTo(d)); System.out.println(d1.equals(d)); System.out.println(d1.isNegative()); System.out.println(d1.isZero()); } }
Output:
Kami memahami kaedah di atas dengan contoh kod java di bawah:
Kod:
package p1; import java.time.Duration; import java.time.*; import java.time.temporal.ChronoUnit; public class DurationClassDemo { public static void main(String[] args) { Duration d = Duration.from(ChronoUnit.DAYS.getDuration()); System.out.println(d.toMinutes()); } }
Output:
Kami memahami kaedah di atas dengan contoh kod java di bawah:
Kod:
package p1; import java.time.Duration; import java.time.*; import java.time.temporal.ChronoUnit; public class DurationClassDemo { public static void main(String[] args) { Duration d = Duration.between(LocalTime.NOON,LocalTime.MAX); System.out.println(d.getUnits()); System.out.println(d.toMinutes()); System.out.println(d.getSeconds()); System.out.println(d.getNano()); System.out.println(d.getClass()); } }
Output:
We understand the above methods with the below sample java code:
Code:
package p1; import java.time.Duration; import java.time.*; import java.time.temporal.ChronoUnit; public class DurationClassDemo { public static void main(String[] args) { Duration d = Duration.ofDays(6); System.out.println(d.getSeconds()); Duration d1 = d.minusDays(3); System.out.println(d1.getSeconds()); d = Duration.ofHours(6); System.out.println(d.getSeconds()); d1 = d.minusHours(2); System.out.println(d1.getSeconds()); } }
Output:
We understand the above methods with the below sample java code:
Code:
package p1; import java.time.Duration; import java.time.*; import java.time.temporal.ChronoUnit; public class DurationClassDemo { public static void main(String[] args) { Duration d = Duration.ofDays(6); System.out.println(d.getSeconds()); Duration d1 = d.plusDays(2); System.out.println(d1.getSeconds()); d = Duration.ofHours(6); System.out.println(d.getSeconds()); d1 = d.plusHours(2); System.out.println(d1.getSeconds()); } }
Output:
We understand the above methods with the below sample java code:
Code:
package p1; import java.time.Duration; import java.time.*; import java.time.temporal.ChronoUnit; public class DurationClassDemo { public static void main(String[] args) { Duration d = Duration.ofDays(6); System.out.println(d.toHours()); Duration d1 =Duration.ofHours(24) ; System.out.println(d1.toDays()); } }
Output:
We understand the above methods with the below sample java code:
Code:
package p1; import java.time.Duration; import java.time.*; import java.time.temporal.ChronoUnit; public class DurationClassDemo { public static void main(String[] args) { Duration d = Duration.ofDays(6); System.out.println(d.toString()); d = d.withSeconds(3000); System.out.println(d.toString()); } }
Output:
The Duration class is one of the built-in class in java, which is used to measure time in seconds and nanoseconds and add, subtract, and convert the duration, or, in simple words, the duration class allows performance operation on time or day duration. The duration class is available in java.time.Duration package of java.
Atas ialah kandungan terperinci Tempoh Java. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!