subtractFrom()
is an instance method of the Duration
class which is used to subtract the Duration
object from the specified Temporal
object.
The subtractFrom
method is defined in the Duration
class. The Duration
class is defined in the java.time
package. To import the Duration
class, check the following import statement.
import java.time.Duration;
public Temporal subtractFrom(Temporal temporal)
Temporal temporal
: The Temporal
object that represents the amount to be modified/adjusted.This method returns the adjusted Temporal
object.
import java.time.Duration;import java.time.LocalDateTime;import java.time.temporal.Temporal;public class Main {public static void main(String[] args) {Duration duration = Duration.ofSeconds(143234, 4223);LocalDateTime currentLocalTime = LocalDateTime.now();Temporal adjustedTemporalObject = duration.subtractFrom(currentLocalTime);System.out.println("Original Temporal object - " + currentLocalTime);System.out.println("Adjusted Temporal object - " + adjustedTemporalObject);}}
Here is a line-by-line explanation of the above code:
Duration
object using the ofSeconds()
method.Temporal
object to adjust to. The LocalDateTime
class implements the Temporal
interface.Duration
object from the Temporal
object using the subtractFrom()
method.Temporal
object defined in line 10.Temporal
object obtained in line 12.