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
- plusDays() – Returns a copy of invoking
LocalDateTime
with the specified number of days added - plusWeeks() – Returns a copy of invoking
LocalDateTime
with the specified number of weeks added - plusMonths() – Returns a copy of invoking
LocalDateTime
with the specified number of months added - plusYears() – Returns a copy of invoking
LocalDateTime
with the specified number of years added
- plusDays() – Returns a copy of invoking
- Likewise, use below methods to add Nanosecond or Second or Minute or Hour fields to LocalDateTime
- plusNanos() – Returns a copy of invoking
LocalDateTime
with the specified number of nanoseconds added - plusSeconds() – Returns a copy of invoking
LocalDateTime
with the specified number of seconds added - plusMinutes() – Returns a copy of invoking
LocalDateTime
with the specified number of minutes added - plusHours() – Returns a copy of invoking
LocalDateTime
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 LocalDateTime,
- Add 5 Days to current LocalDateTime using plusDays() method
- Add 2 Weeks to current LocalDateTime using plusWeeks() method
- Add 3 Months to current LocalDateTime using plusMonths() method
- Add 1 Year to current LocalDateTime using plusYears() method
- Add 125 Nanos to current system LocalDateTime using plusNanos() method
- Add 37 Seconds to current system LocalDateTime using plusSeconds() method
- Add 19 Minutes to current system LocalDateTime using plusMinutes() method
- 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:
- Java 8 – LocalDateTime with method details and examples
- Java 8 – How to get Date and Time fields from LocalDateTime ?
- Java 8 – How to form LocalDateTime passing Date and Time fields ?
- Java 8 – How to form LocalDateTime passing LocalDate and LocalTime ?
- Java 8 – How to form LocalDateTime passing Instant and ZoneId ?
- Java 8 – How to form LocalDateTime passing Second/Nano and ZoneOffset ?
- Java 8 – How to parse LocalDateTime in String form ?
- Java 8 – How to convert String to LocalDateTime ?
- Java 8 – How to convert LocalDateTime to String ?
- Java 8 – How to convert LocalDateTime in different formats ?
- Java 8 – How to convert LocalDateTime in different Format Style ?
- Java 8 – How to convert LocalDateTime to ZonedDateTime ?
- Java 8 – How to convert LocalDateTime to an OffsetDateTime ?
- Java 8 – How to convert LocalDateTime to an Instant ?
- Java 8 – How to extract LocalDateTime and LocalTime from LocalDateTime ?
- Java 8 – How to convert LocalDateTime to java.util.Date and vice-versa ?
- Java 8 – How to convert LocalDateTime to java.sql.Timestamp and vice-versa ?
- Java 8 – How to convert LocalDateTime to Calendar and vice-versa ?
- Java 8 – How to convert LocalDateTime to GregorianCalendar and vice-versa ?
- Java 8 – How to convert LocalDateTime to XMLGregorianCalendar and vice-versa ?
- Java 8 – How to convert java.util.Date to LocalDateTime in different ways ?
- Java 8 – How to add Date and Time fields to LocalDateTime ?
- Java 8 – How to subtract Date and Time fields from LocalDateTime ?
- Java 8 – How to alter Date and Time fields of LocalDateTime ?
- Java 8 – How to check whether a LocalDateTime is Before another LocalDateTime ?
- Java 8 – How to check whether a LocalDateTime is After another LocalDateTime ?
- Java 8 – How to compare two LocalDateTime instances ?
- Java 8 – How to find difference between two LocalDateTime using Period & Duration ?
- Java 8 – What are all the Temporal Fields supported by LocalDateTime ?
- Java 8 – What are all the Temporal Units supported by LocalDateTime ?
- More Java 8 Date/Time API examples
References:
- https://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.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 !!