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,
- minusNanos() – Returns a copy of invoking
LocalTime
with the specified number of nanoseconds subtracted - minusSeconds() – Returns a copy of invoking
LocalTime
with the specified number of seconds subtracted - minusMinutes() – Returns a copy of invoking
LocalTime
with the specified number of minutes subtracted - minusHours() – Returns a copy of invoking
LocalTime
with the specified number of hours subtracted
- minusNanos() – Returns a copy of invoking
- In the below illustration, we are going to do below operations with current LocalTime,
- Subtract 125 Nanos from current system LocalTime using minusNanos() method
- Subtract 37 Seconds from current system LocalTime using minusSeconds() method
- Subtract 19 Minutes from current system LocalTime using minusMinutes() method
- 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:
- Java 8 – LocalTime with method details and examples
- Java 8 – How to get Hour, Minute and Second fields from LocalTime ?
- Java 8 – How to form LocalTime passing Hour, Minute and Second fields ?
- Java 8 – How to parse LocalTime in String form ?
- Java 8 – How to convert String to LocalTime ?
- Java 8 – How to convert LocalTime to String ?
- Java 8 – How to convert LocalTime in different formats ?
- Java 8 – How to convert LocalTime in different Format Style ?
- Java 8 – How to convert LocalTime to LocalDateTime ?
- Java 8 – How to convert LocalTime to ZonedDateTime ?
- Java 8 – How to convert LocalTime to an OffsetDateTime ?
- Java 8 – How to convert LocalTime to an Instant ?
- Java 8 – How to convert LocalTime to an OffsetTime ?
- Java 8 – How to convert LocalTime to Seconds and vice-versa ?
- Java 8 – How to convert LocalTime to Nanoseconds and vice-versa ?
- Java 8 – How to convert LocalTime to java.util.Date and vice-versa ?
- Java 8 – How to convert LocalTime to java.sql.Timestamp and vice-versa ?
- Java 8 – How to convert LocalTime to Calendar and vice-versa ?
- Java 8 – How to convert LocalTime to GregorianCalendar and vice-versa ?
- Java 8 – How to convert LocalTime to XMLGregorianCalendar and vice-versa ?
- Java 8 – How to convert java.util.Date to LocalTime in different ways ?
- Java 8 – How to add Hour, Minute and Second fields to LocalTime ?
- Java 8 – How to subtract Hour, Minute and Second fields from LocalTime ?
- Java 8 – How to alter Hour, Minute and Second fields of LocalTime ?
- Java 8 – How to check whether a LocalTime is Before another LocalTime ?
- Java 8 – How to check whether a LocalTime is After another LocalTime ?
- Java 8 – How to compare two LocalTime instances ?
- Java 8 – How to find time duration between two LocalTime instances ?
- Java 8 – What are all the Temporal Fields supported by LocalTime ?
- Java 8 – What are all the Temporal Units supported by LocalTime ?
- Java 9 – Find difference between two LocalTime instances upto nanosecond precision ?
- More Java 8 Date/Time API examples
References:
- https://docs.oracle.com/javase/8/docs/api/java/time/LocalTime.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Date.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html
Happy Coding !!
Happy Learning !!