In this article, we will learn how to alter Hour or Minute or Second or Nanosecond fields of LocalTime using different methods provided in Java 1.8 version
Altering Nano/Second/Minute/Hour fields of LocalTime:
- It is very simple to alter Nano or Second or Minute or Hour fields of LocalTime using below methods,
- withNano() – Returns a copy of this
LocalTime
with the nano-of-second altered - withSecond() – Returns a copy of this
LocalTime
with the second-of-minute altered - withMinute() – Returns a copy of this
LocalTime
with the minute-of-hour altered - withHour() – Returns a copy of this
LocalTime
with the hour-of-day altered
- withNano() – Returns a copy of this
- In the below illustration, we are going to do alter operations with current LocalTime,
- Alter/change/replace Nano field of current LocalTime to 125 using withNano() method
- Alter/change/replace Second field of current LocalTime to 47 using withSecond() method
- Alter/change/replace Minute field of current LocalTime to 19 using withMinute() method
- Alter/change/replace Hour field of current LocalTime to 5 using withHour() method
- Finally, print LocalTime to the console
AlterLocalTime.java
package in.bench.resources.java8.localtime.examples;
import java.time.LocalTime;
public class AlterLocalTime {
public static void main(String[] args) {
// 1. get current system Time
LocalTime localTime = LocalTime.now();
System.out.println("Current System Time in ISO_LOCAL_TIME format is = "
+ localTime);
// 1.1 alter nanosecond part to current system Date/time
LocalTime nanoAltered = localTime.withNano(125);
System.out.println("\n1. Nanoseconds (125) altered in current system Time is = "
+ nanoAltered);
// 1.2 alter second part to current system Date/time
LocalTime secondAltered = localTime.withSecond(47);
System.out.println("2. Seconds (47) altered in current system Time is = "
+ secondAltered);
// 1.3 alter minute part to current system Date/time
LocalTime minuteAltered = localTime.withMinute(19);
System.out.println("3. Minutes (19) altered in current system Time is = "
+ minuteAltered);
// 1.4 alter hour part to current system Time
LocalTime hourAltered = localTime.withHour(5);
System.out.print("4. Hours (5) altered in current system Time is = "
+ hourAltered);
}
}
Output:
Current System Time in ISO_LOCAL_TIME format is = 10:07:34.427627200
1. Nanoseconds (125) altered in current system Time is = 10:07:34.000000125
2. Seconds (47) altered in current system Time is = 10:07:47.427627200
3. Minutes (19) altered in current system Time is = 10:19:34.427627200
4. Hours (5) altered in current system Time is = 05:07:34.427627200
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 !!