Java 8 – How to subtract Hour, Minute and Second fields from LocalTime ?

In this article, we will learn how to subtract Hour or Minute or Second or Nanosecond fields from LocalTime using different methods provided in Java 1.8 version

Subtracting Nano/Second/Minute/Hour from LocalTime :

  • It is very simple to subtract Nanosecond or Second or Minute or Hour fields from LocalTime using below methods,
    1. minusNanos() – Returns a copy of invoking LocalTime with the specified number of nanoseconds subtracted
    2. minusSeconds() – Returns a copy of invoking LocalTime with the specified number of seconds subtracted
    3. minusMinutes() – Returns a copy of invoking LocalTime with the specified number of minutes subtracted
    4. minusHours() – Returns a copy of invoking LocalTime with the specified number of hours subtracted
  • In the below illustration, we are going to do below operations with current LocalTime,
    1. Subtract 125 Nanos from current system LocalTime using minusNanos() method
    2. Subtract 37 Seconds from current system LocalTime using minusSeconds() method
    3. Subtract 19 Minutes from current system LocalTime using minusMinutes() method
    4. Subtract 5 Hours from current system LocalTime using minusHours() method
  • Finally, print LocalTime to the console for above operations

SubtractFromLocalTime.java

package in.bench.resources.java8.localtime.examples;

import java.time.LocalTime;

public class SubtractFromLocalTime {

	public static void main(String[] args) {

		// 1. get current system time
		LocalTime localTime = LocalTime.now();
		System.out.println("Current System Time is - " + localTime);


		// 1.1 subtract 125 NanoSeconds from current system time
		LocalTime sub_125_Nanos = localTime.minusNanos(125);
		System.out.println("\n1. After subtracting 125 Nanoseconds from Current System Time is - " 
				+ sub_125_Nanos);


		// 1.2 subtract 37 Seconds from current system time
		LocalTime sub_37_Seconds = localTime.minusSeconds(37);
		System.out.println("2. After subtracting 37 Seconds from Current System Time is - " 
				+ sub_37_Seconds);


		// 1.3 subtract 19 Minutes from current system time
		LocalTime sub_19_Minutes = localTime.minusMinutes(19);
		System.out.println("3. After subtracting 19 Minutes from Current System Time is - " 
				+ sub_19_Minutes);


		// 1.4 subtract 5 Hours from current system time
		LocalTime sub_5_Hours = localTime.minusHours(5);
		System.out.print("4. After subtracting 5 Hours from Current System Time is - " 
				+ sub_5_Hours);
	}
}

Output:

Current System Time is - 09:52:25.926930200

1. After subtracting 125 Nanoseconds from Current System Time is - 09:52:25.926930075
2. After subtracting 37 Seconds from Current System Time is - 09:51:48.926930200
3. After subtracting 19 Minutes from Current System Time is - 09:33:25.926930200
4. After subtracting 5 Hours from Current System Time is - 04:52:25.926930200

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java 8 – How to alter Hour, Minute and Second fields of LocalTime ?
Java 8 – How to add Hour, Minute and Second fields to LocalTime ?