In this article, we will learn how to add Hour or Minute or Second or Nanosecond fields to LocalTime using different methods provided in Java 1.8 version
Adding Nano/Second/Minute/Hour to LocalTime :
- It is very simple to add Nanosecond or Second or Minute or Hour fields to LocalTime using below methods,
- plusNanos() – Returns a copy of invoking
LocalTime
with the specified number of nanoseconds added - plusSeconds() – Returns a copy of invoking
LocalTime
with the specified number of seconds added - plusMinutes() – Returns a copy of invoking
LocalTime
with the specified number of minutes added - plusHours() – Returns a copy of invoking
LocalTime
with the specified number of hours added
- plusNanos() – Returns a copy of invoking
- In the below illustration, we are going to do below operations with current LocalTime,
- Add 125 Nanos to current system LocalTime using plusNanos() method
- Add 37 Seconds to current system LocalTime using plusSeconds() method
- Add 19 Minutes to current system LocalTime using plusMinutes() method
- Add 5 Hours to current system LocalTime using plusHours() method
- Finally, print LocalTime to the console for above operations
AddToLocalTime.java
package in.bench.resources.java8.localtime.examples;
import java.time.LocalTime;
public class AddToLocalTime {
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 add 125 NanoSeconds to current system time
LocalTime add_125_Nanos = localTime.plusNanos(125);
System.out.println("\n1. After adding 125 Nano Seconds to Current System Time is - "
+ add_125_Nanos);
// 1.2 add 37 Seconds to current system time
LocalTime add_37_Seconds = localTime.plusSeconds(37);
System.out.println("2. After adding 37 Seconds to Current System Time is - "
+ add_37_Seconds);
// 1.3 add 19 Minutes to current system time
LocalTime add_19_Minutes = localTime.plusMinutes(19);
System.out.println("3. After adding 19 Minutes to Current System Time is - "
+ add_19_Minutes);
// 1.4 add 5 Hours to current system time
LocalTime add_5_Hours = localTime.plusHours(5);
System.out.print("4. After adding 5 Hours to Current System Time is - "
+ add_5_Hours);
}
}
Output:
Current System Time is - 09:43:31.623506
1. After adding 125 Nano Seconds to Current System Time is - 09:43:31.623506125
2. After adding 37 Seconds to Current System Time is - 09:44:08.623506
3. After adding 19 Minutes to Current System Time is - 10:02:31.623506
4. After adding 5 Hours to Current System Time is - 14:43:31.623506
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 !!