Java 8 – How to add Date and Time fields to LocalDateTime ?

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

Adding Date & Time fields to LocalDateTime :

  • Adding Date & Time fields to LocalDateTime is quite simple using different methods provided
  • Use below methods to add Day or Week or Month or Year fields to LocalDateTime
    1. plusDays() – Returns a copy of invoking LocalDateTime with the specified number of days added
    2. plusWeeks() – Returns a copy of invoking LocalDateTime with the specified number of weeks added
    3. plusMonths() – Returns a copy of invoking LocalDateTime with the specified number of months added
    4. plusYears() – Returns a copy of invoking LocalDateTime with the specified number of years added
  • Likewise, use below methods to add Nanosecond or Second or Minute or Hour fields to LocalDateTime
    1. plusNanos() – Returns a copy of invoking LocalDateTime with the specified number of nanoseconds added
    2. plusSeconds() – Returns a copy of invoking LocalDateTime with the specified number of seconds added
    3. plusMinutes() – Returns a copy of invoking LocalDateTime with the specified number of minutes added
    4. plusHours() – Returns a copy of invoking LocalDateTime with the specified number of hours added
  • In the below illustration, we are going to do below operations with current LocalDateTime,
    1. Add 5 Days to current LocalDateTime using plusDays() method
    2. Add 2 Weeks to current LocalDateTime using plusWeeks() method
    3. Add 3 Months to current LocalDateTime using plusMonths() method
    4. Add 1 Year to current LocalDateTime using plusYears() method
    5. Add 125 Nanos to current system LocalDateTime using plusNanos() method
    6. Add 37 Seconds to current system LocalDateTime using plusSeconds() method
    7. Add 19 Minutes to current system LocalDateTime using plusMinutes() method
    8. Add 5 Hours to current system LocalDateTime using plusHours() method
  • Finally, print LocalDateTime after each operation to the console

AddToLocalDateTime.java

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

import java.time.LocalDateTime;

public class AddToLocalDateTime {

	public static void main(String[] args) {

		// get current system date/time
		LocalDateTime localDateTime = LocalDateTime.now();
		System.out.println("Current System Date/time is :- \n" + localDateTime);


		// 1. Adding Day/Week/Month/Year to LocalDateTime
		System.out.println("\n\nAdding Day/Week/Month/Year to LocalDateTime :-\n");


		// 1.1 add 5 days with current system date
		LocalDateTime add_5_Days = localDateTime.plusDays(5);
		System.out.println("1. After adding 5 Days to Current System Date/time is :- "
				+ add_5_Days);


		// 1.2 add 2 weeks to current system date
		LocalDateTime add_2_Weeks = localDateTime.plusWeeks(2);
		System.out.println("2. After adding 2 Weeks to Current System Date/time is :- "
				+ add_2_Weeks);


		// 1.3 add 3 months to current system date
		LocalDateTime add_3_Months = localDateTime.plusMonths(3);
		System.out.println("3. After adding 3 Months to Current System Date/time is :- "
				+ add_3_Months);


		// 1.4 add 1 year to current system date
		LocalDateTime add_1_Year = localDateTime.plusYears(1);
		System.out.println("4. After adding 1 Year to Current System Date/time is :- "
				+ add_1_Year);



		// 2. Adding Day/Week/Month/Year to LocalDateTime
		System.out.println("\n\nAdding Nano/Second/Minute/Hour to LocalDateTime :-\n");


		// 2.1 add 125 NanoSeconds to current system time
		LocalDateTime add_125_Nanos = localDateTime.plusNanos(125);
		System.out.println("1. After adding 125 Nano Seconds to Current System Date/time is - "
				+ add_125_Nanos);


		// 2.2 add 37 Seconds to current system time
		LocalDateTime add_37_Seconds = localDateTime.plusSeconds(37);
		System.out.println("2. After adding 37 Seconds to Current System Date/time is - "
				+ add_37_Seconds);


		// 2.3 add 19 Minutes to current system time
		LocalDateTime add_19_Minutes = localDateTime.plusMinutes(19);
		System.out.println("3. After adding 19 Minutes to Current System Date/time is - "
				+ add_19_Minutes);


		// 2.4 add 5 Hours to current system time
		LocalDateTime add_5_Hours = localDateTime.plusHours(5);
		System.out.print("4. After adding 5 Hours to Current System Date/time is - "
				+ add_5_Hours);
	}
}

Output:

Current System Date/time is :- 
2022-08-10T12:55:13.524493300


Adding Day/Week/Month/Year to LocalDateTime :-

1. After adding 5 Days to Current System Date/time is :- 2022-08-15T12:55:13.524493300
2. After adding 2 Weeks to Current System Date/time is :- 2022-08-24T12:55:13.524493300
3. After adding 3 Months to Current System Date/time is :- 2022-11-10T12:55:13.524493300
4. After adding 1 Year to Current System Date/time is :- 2023-08-10T12:55:13.524493300


Adding Nano/Second/Minute/Hour to LocalDateTime :-

1. After adding 125 Nano Seconds to Current System Date/time is - 2022-08-10T12:55:13.524493425
2. After adding 37 Seconds to Current System Date/time is - 2022-08-10T12:55:50.524493300
3. After adding 19 Minutes to Current System Date/time is - 2022-08-10T13:14:13.524493300
4. After adding 5 Hours to Current System Date/time is - 2022-08-10T17:55:13.524493300

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java 8 – How to subtract Date and Time fields from LocalDateTime ?
Java 8 – How to convert java.util.Date to LocalDateTime in different ways ?