Java 8 – How to alter Date and Time fields of LocalDateTime ?

In this article, we will learn how to alter/modify/change Date (day/month/year) & Time (nano/second/minute/hour) fields of LocalDateTime using different methods provided in the Java 1.8 version

Altering Date & Time fields of LocalDateTime :

  • Altering/modifying Date & Time fields of LocalDateTime is quite simple using different methods provided
  • Use below methods to alter/change/modify Day or Month or Year fields of LocalDateTime
    1. withDayOfMonth() – Returns a copy of this LocalDateTime with the day-of-month altered
    2. withMonth() – Returns a copy of this LocalDateTime with the month-of-year altered
    3. withYear() – Returns a copy of this LocalDateTime with the year altered
  • Likewise, use below methods to alter/change/modify Nanosecond or Second or Minute or Hour fields of LocalDateTime
    1. withNano() – Returns a copy of this LocalDateTime with the nano-of-second altered
    2. withSecond() – Returns a copy of this LocalDateTime with the second-of-minute altered
    3. withMinute() – Returns a copy of this LocalDateTime with the minute-of-hour altered
    4. withHour() – Returns a copy of this LocalDateTime with the hour-of-day altered
  • In the below illustration, we are going to do below operations with current LocalDateTime,
    1. Alter/change/modify Day field of current LocalDateTime to 15 using withDayOfMonth() method
    2. Alter/change/modify Month field of current LocalDateTime to 9 using withMonth() method
    3. Alter/change/modify Year field of current LocalDateTime to 2023 using withYear() method
    4. Alter/change/replace Nano field of current LocalDateTime to 125 using withNano() method
    5. Alter/change/replace Second field of current LocalDateTime to 47 using withSecond() method
    6. Alter/change/replace Minute field of current LocalDateTime to 19 using withMinute() method
    7. Alter/change/replace Hour field of current LocalDateTime to 5 using withHour() method
  • Finally, print LocalDateTime after each operation to the console

AlterLocalDateTime.java

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

import java.time.LocalDateTime;

public class AlterLocalDateTime {

	public static void main(String[] args) {

		// get Current System Date/time
		LocalDateTime localDateTime = LocalDateTime.now();
		System.out.println("Current Date/time in ISO_LOCAL_DATE_TIME format is = "
				+ localDateTime);


		// 1. Altering Day/Month/Year parts of LocalDateTime
		System.out.println("\n\nAltering Day/Month/Year parts of LocalDateTime :- \n");


		// 1.1 alter/change/modify DAY part of Current System Date/time
		LocalDateTime dateAltered = localDateTime.withDayOfMonth(15);
		System.out.println("1. Day (15) altered in Current System Date/time is = "
				+ dateAltered);


		// 1.2 alter/change/modify MONTH part of Current System Date/time
		LocalDateTime monthAltered = localDateTime.withMonth(9);
		System.out.println("2. Month (9) altered in Current System Date/time is = "
				+ monthAltered);


		// 1.3 alter/change/modify YEAR part of Current System Date/time
		LocalDateTime yearAltered = localDateTime.withYear(2023);
		System.out.println("3. Year (2023) altered in Current System Date/time is = "
				+ yearAltered);



		// 2. Altering Nano/Second/Minute/Hour from LocalDateTime
		System.out.println("\n\nAltering Nano/Second/Minute/Hour parts of LocalDateTime :- \n");


		// 2.1 alter/change/modify HOUR part to Current System Date/time
		LocalDateTime hourAltered = localDateTime.withHour(5);
		System.out.println("1. Hours (5) altered in Current System Date/time is = " 
				+ hourAltered);


		// 2.2 alter/change/modify MINUTE part to current system Date/time
		LocalDateTime minuteAltered = localDateTime.withMinute(19); 
		System.out.println("2. Minutes (19) altered in Current System Date/time is = " 
				+ minuteAltered);


		// 2.3 alter/change/modify SECOND part to current system Date/time
		LocalDateTime secondAltered = localDateTime.withSecond(47);
		System.out.println("3. Seconds (47) altered in Current System Date/time is = " 
				+ secondAltered);


		// 2.4 alter/change/modify NANOSECOND part to current system Date/time
		LocalDateTime nanoAltered = localDateTime.withNano(125);
		System.out.print("4. Nanoseconds (125) altered in Current System Date/time is = "
				+ nanoAltered);
	}
}

Output:

Current Date/time in ISO_LOCAL_DATE_TIME format is = 2022-08-10T18:40:24.397501300


Altering Day/Month/Year parts of LocalDateTime :- 

1. Day (15) altered in Current System Date/time is = 2022-08-15T18:40:24.397501300
2. Month (9) altered in Current System Date/time is = 2022-09-10T18:40:24.397501300
3. Year (2023) altered in Current System Date/time is = 2023-08-10T18:40:24.397501300


Altering Nano/Second/Minute/Hour parts of LocalDateTime :- 

1. Hours (5) altered in Current System Date/time is = 2022-08-10T05:40:24.397501300
2. Minutes (19) altered in Current System Date/time is = 2022-08-10T18:19:24.397501300
3. Seconds (47) altered in Current System Date/time is = 2022-08-10T18:40:47.397501300
4. Nanoseconds (125) altered in Current System Date/time is = 2022-08-10T18:40:24.000000125

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java 8 – How to check whether a LocalDateTime is Before another LocalDateTime ?
Java 8 – How to subtract Date and Time fields from LocalDateTime ?