Rumah > Java > javaTutorial > Tempoh Java

Tempoh Java

王林
Lepaskan: 2024-08-30 15:52:12
asal
1020 orang telah melayarinya

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}
Salin selepas log masuk

Senarai Tempoh

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):

  • Tempoh abs(): Kaedah ini mengembalikan salinan tempoh ini dengan panjang positif.
  • dapat panjang(unit TemporalUnit): Kembalikan nilai unit yang diminta.
  • tempoh statik antara(Temporal startInclusive, Temporal endExclusive): Mengembalikan objek tempoh, iaitu tempoh antara dua objek temporal.

Contoh #1

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));
}
}
Salin selepas log masuk

Output:

Tempoh Java

  • Temporal addTo(Temporal temporal): Kembalikan objek tempoh, iaitu penambahan temporal dan objek tempoh ini.
  • Tempoh dibahagikanDengan(pembahagi panjang): Kembalikan objek tempoh, iaitu tempoh ini dibahagikan dengan pembahagi.

Contoh #2

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());
}
}
Salin selepas log masuk

Output:

Tempoh Java

  • int compareTo(Duration otherDuration): Tempoh ini dan tempoh yang ditentukan membandingkan.
  • boolean sama dengan(Objek lainDuration): Menyemak tempoh ini dengan Tempoh yang ditentukan dan mengembalikan Boolean.
  • boolean isNegative(): Kembalikan True jika tempoh ini negatif.
  • boolean isZero(): Kembalikan True jika panjang tempoh ini ialah sifar.

Contoh #3

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());
}
}
Salin selepas log masuk

Output:

Tempoh Java

  • tempoh statik daripada(amaun TempohJumlah): Mendapat contoh tempoh daripada amaun sementara.

Contoh #4

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());
}
}
Salin selepas log masuk

Output:

Tempoh Java

  • int getNano(): Tempoh pengembalian dalam nanosaat.
  • long getSeconds(): Tempoh pengembalian dalam beberapa saat.
  • Senaraikan getUnits(): Kembalikan set unit yang disokong oleh tempoh ini.
  • int hashCode(): Kembalikan kod cincang untuk tempoh ini.

Contoh #5

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());
}
}
Salin selepas log masuk

Output:

Tempoh Java

  • Duration minus(Duration duration):- Returns object which results from this duration subtracted with the specified duration.
  • Duration minus(long amountToSubtract, TemporalUnit unit): Returns object resulting from this duration subtracted with the specified duration.
  • Duration minusDays(long daysToSubtract): Returns object which results from this duration subtracted with the specified duration in standard 24-hour days.
  • Duration minusHours(long hoursToSubtract): Returns object resulting from this duration subtracted with the specified duration in hours.
  • Duration minusMillis(long millisToSubtract): Returns object resulting from this duration subtracted with the specified duration in milliseconds.
  • Duration minusMinutes(long minutesToSubtract): Returns object resulting from this duration subtracted with the specified duration in minutes.
  • Duration minusNanos(long nanosToSubtract): Returns object resulting from this duration subtracted with the specified duration in nanoseconds.
  • Duration minusSeconds(long secondsToSubtract): Returns object resulting from this duration subtracted with the specified duration in seconds.
  • Duration multipliedBy(long multiplicand): Returns object resulting from this duration multiplied by the scalar.
  • Duration negated() – Returns object which results from this duration with the length negated.
  • static duration of(long amount, TemporalUnit unit): Returns Duration object representing an amount in the specified unit.
  • static Duration ofDays(long days): Returns Duration object of standard 24-hour days.
  • static Duration ofHours(long hours): Returns Duration object of the hour.
  • static Duration ofMillis(long millis): Returns Duration object of milliseconds.
  • static Duration ofMinutes(long minutes): Returns Duration object of minutes.
  • static Duration ofNanos(long nanos): Returns Duration object of nanoseconds.
  • static Duration ofSeconds(long seconds): Returns Duration object of seconds.
  • static Duration ofSeconds(long seconds, long nanoAdjustment): Returns Duration object of seconds and nanoseconds adjustment.

Example #6

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());
}
}
Salin selepas log masuk

Output:

Tempoh Java

  • static Duration parse(CharSequence text): Return duration object from a text, for example, PnDTnHnMn.nS.
  • Duration plus(Duration duration): Return the duration object of this duration with added the specified duration.
  • Duration plus(long amountToAdd, TemporalUnit unit): Return the duration object of this duration with add the specified duration.
  • Duration plusDays(long daysToAdd): Return the duration object of this duration with add the specified duration in 24-hour days.
  • Duration plusHours(long hoursToAdd): Return the duration object of this duration with add the specified duration in hours.
  • Duration plusMillis(long millisToAdd): Return the duration object of this duration with add the specified duration in milliseconds.
  • Duration plusMinutes(long minutesToAdd): Return the duration object of this duration with the add specified duration in minutes.
  • Duration plusNanos(long nanosToAdd): Return the duration object of this duration with add the specified duration in nanoseconds.
  • Duration plusSeconds(long secondsToAdd): Return the duration object of this duration with the specified duration in seconds.

Example #7

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());
}
}
Salin selepas log masuk

Output:

Tempoh Java

  • Temporal subtractFrom(Temporal temporal): Return Subtraction of this duration from the temporal object.
  • long toDays(): Return the number of days in this duration.
  • long toHours(): Return the number of hours in this duration.
  • long toMillis(): Return the number of milliseconds in this duration.
  • long toMinutes(): return the number of minutes in this duration.
  • long toNanos(): return the number of nanoseconds in this duration.
  • String toString(): Return this duration in string representation, such as PT8H6M12.345S.

Example #8

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());
}
}
Salin selepas log masuk

Output:

Tempoh Java

  • Duration withNanos(int nanoOfSecond): Returns duration object with the specified nanoofsecond.
  • Duration withSeconds(long seconds): Returns duration object of this duration with the seconds of the specified amount.

Example #9

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());
}
}
Salin selepas log masuk

Output:

Tempoh Java

Conclusion

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!

Label berkaitan:
sumber:php
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan