minusSeconds() Method with Example in Java

In this tutorial, we will learn about the minusSeconds() Method with an example in Java. This minusSeconds() method basically subtracts the given number of seconds from a time value that may be of an Instant or LocalTime. For using this method, one has to import the java.time package.

LocalTime minusSeconds() Method in Java

The LocalTime minusSeconds() method deducts the given number of seconds from the LocalTime. Hereafter, it returns the result as the respective class object i.e LocalTime object.
It takes a single parameter i.e. the number of seconds to subtract.

The syntax of the method is:

public LocalTime minusSeconds(long given_seconds_to_deduct)

Note: The parameter can be negative. Thus, it will add the given seconds to the LocalTime.

LocalTime minusSeconds() Example

This is a sample code for the implementation of the LocalTime minusSeconds() method.

import java.time.*; 

class minusSecLT{ 
  public static void main(String[] args) 
  {  
    LocalTime time 
      = LocalTime.parse("12:34:56.78"); 

    System.out.println("Unaltered LocalTime: "
            + time); 

    LocalTime lt = time.minusSeconds(240); 

    System.out.println("LocalTime after minusSeconds() method: "
            + lt); 
  } 
}

The output is as follows.

Unaltered Localtime: 12:34:56.780
LocalTime after minusSeconds() method: 12:30:56.780

Note: We can add time by giving a negative parameter.

import java.time.*; 

class minusSecLT{ 
  public static void main(String[] args) 
  {  
    LocalTime time 
      = LocalTime.parse("12:34:56.78"); 

    System.out.println("Unaltered LocalTime: "
            + time); 

    LocalTime lt = time.minusSeconds(-480); 

    System.out.println("LocalTime after minusSeconds() method: "
            + lt); 
  } 
}

This is the output:

Unaltered Localtime: 12:34:56.780
LocalTime after minusSeconds() method: 12:42:56.780

Instant minusSeconds() Method in Java

The Instant class minusSeconds() method deducts the given number of seconds from this instant. It gives the result as an object of Instant class after subtraction. It accepts one parameter which is the value to be deducted from the instant Time.

The syntax of this method is

public Instant minusSeconds(long given_seconds_to_deduct)

Note: This method can throw two exceptions:

  1. DateTimeException: when the result exceeds the max or min instant
  2. ArithmeticException: in case of arithmetic overflow

Instant minusSeconds() Example

I have provided a sample code for the implementation of the Instant minusSeconds() method in Java.

import java.time.*; 

class Main{ 
  public static void main(String[] args) 
  {  
  Instant inst = Instant.parse("2020-05-30T12:34:56.78Z"); 
   
        System.out.println("Instant: " + inst); 
   
        Instant val = inst.minusSeconds(4800); 
  
        System.out.println("Instant after minusSeconds() method: " + val);  
  } 
}

This is the output of the above program.

Instant: 2020-05-30T12:34:56.780Z
Instant after minusSeconds() method: 2020-05-30T11:14:56.780Z

Similarly to the LocalTime class, the minusSeconds() Method in Instant class can also take a negative parameter. Subsequently, it will add the provided value.

I hope this post was helpful.

Leave a Reply

Your email address will not be published. Required fields are marked *