- java.time Package Classes
- java.time - Home
- java.time - Clock
- java.time - Duration
- java.time - Instant
- java.time - LocalDate
- java.time - LocalDateTime
- java.time - LocalTime
- java.time - MonthDay
- java.time - OffsetDateTime
- java.time - OffsetTime
- java.time - Period
- java.time - Year
- java.time - YearMonth
- java.time - ZonedDateTime
- java.time - ZoneId
- java.time - ZoneOffset
- java.time Package Enums
- java.time - Month
- java.time Useful Resources
- java.time - Discussion
java.time.Duration.of() Method Example
Description
The java.time.Duration.of(long amount, TemporalUnit unit) method obtains a Duration representing an amount in the specified unit.
Declaration
Following is the declaration for java.time.Duration.of(long amount, TemporalUnit unit) method.
public static Duration of(long amount, TemporalUnit unit)
Parameters
amount − the amount of the duration, measured in terms of the unit, positive or negative.
unit − the unit that the duration is measured in, must have an exact duration, not null.
Return Value
a Duration, not null.
Exception
DateTimeException − if the period unit has an estimated duration.
ArithmeticException − if numeric overflow occurs.
Example
The following example shows the usage of java.time.Duration.of(long amount, TemporalUnit unit) method.
package com.tutorialspoint; import java.time.Duration; import java.time.temporal.ChronoUnit; public class DurationDemo { public static void main(String[] args) { Duration duration = Duration.of(5, ChronoUnit.SECONDS); System.out.println(duration.getSeconds()); } }
Let us compile and run the above program, this will produce the following result −
5
Advertisements