Duration in java is a class used to measure time in seconds and nanoseconds. The package of the duration class in java is java.time.Duration. The Duration class object specifies the period of time or is used to determine the difference between two times. A Duration object is immutable and thread-safe also as the Duration object is immutable, so we cannot change its values once it is created. But, we can create new Duration objects based on another Duration object. The Duration class inherits an object class ( as an object is the superclass of all classes in java) and implements the Comparable interface.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
Syntax
The syntax of the Duration class declaration in java is:
public final class Duration extends Object implements Comparable < Duration >, TemporalAmount, Serializable { // variables and method of the class Duration}
The lists of Duration class methods are explained below with example code; an example code can be used further for similar methods (as for each method example code not given):
We understand the above methods with the below sample java code.
Code:
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:
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.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:
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.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:
We understand the above method 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.from(ChronoUnit.DAYS.getDuration()); System.out.println(d.toMinutes()); } }
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.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.
The above is the detailed content of Java Duration. For more information, please follow other related articles on the PHP Chinese website!