In this article, we will learn how to add Date (day/week/month/year) & Time (nano/second/minute/hour) fields to ZonedDateTime using different methods provided in the Java 1.8 version
Adding Date & Time fields to ZonedDateTime :
- Adding Date & Time fields to ZonedDateTime is quite simple using different methods provided
- Use below methods to add Day or Week or Month or Year fields to ZonedDateTime
- plusDays() – Returns a copy of invoking
ZonedDateTime
with the specified number of days added - plusWeeks() – Returns a copy of invoking
ZonedDateTime
with the specified number of weeks added - plusMonths() – Returns a copy of invoking
ZonedDateTime
with the specified number of months added - plusYears() – Returns a copy of invoking
ZonedDateTime
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 ZonedDateTime
- plusNanos() – Returns a copy of invoking
ZonedDateTime
with the specified number of nanoseconds added - plusSeconds() – Returns a copy of invoking
ZonedDateTime
with the specified number of seconds added - plusMinutes() – Returns a copy of invoking
ZonedDateTime
with the specified number of minutes added - plusHours() – Returns a copy of invoking
ZonedDateTime
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 default ZonedDateTime,
- Add 5 Days to ZonedDateTime using plusDays() method
- Add 2 Weeks to ZonedDateTime using plusWeeks() method
- Add 3 Months to ZonedDateTime using plusMonths() method
- Add 1 Year to ZonedDateTime using plusYears() method
- Add 125 Nanos to ZonedDateTime using plusNanos() method
- Add 37 Seconds to ZonedDateTime using plusSeconds() method
- Add 19 Minutes to ZonedDateTime using plusMinutes() method
- Add 5 Hours to ZonedDateTime using plusHours() method
- Finally, print ZonedDateTime after each operation to the console
AddToZonedDateTime.java
package in.bench.resources.java8.zoneddatetime.examples;
import java.time.ZonedDateTime;
public class AddToZonedDateTime {
public static void main(String[] args) {
// get Zoned date/time
ZonedDateTime zonedDateTime = ZonedDateTime.now();
System.out.println("Zoned Date/time is :- " + zonedDateTime);
// 1. Adding Day/Week/Month/Year to ZonedDateTime
System.out.println("\nAdding Day/Week/Month/Year to ZonedDateTime :-\n");
// 1.1 add 5 days with Zoned date
ZonedDateTime add_5_Days = zonedDateTime.plusDays(5);
System.out.println("1. After adding 5 Days to Zoned Date/time is :- "
+ add_5_Days);
// 1.2 add 2 weeks to Zoned date
ZonedDateTime add_2_Weeks = zonedDateTime.plusWeeks(2);
System.out.println("2. After adding 2 Weeks to Zoned Date/time is :- "
+ add_2_Weeks);
// 1.3 add 3 months to Zoned date
ZonedDateTime add_3_Months = zonedDateTime.plusMonths(3);
System.out.println("3. After adding 3 Months to Zoned Date/time is :- "
+ add_3_Months);
// 1.4 add 1 year to Zoned date
ZonedDateTime add_1_Year = zonedDateTime.plusYears(1);
System.out.println("4. After adding 1 Year to Zoned Date/time is :- "
+ add_1_Year);
// 2. Adding Nano/Second/Minute/Hour to ZonedDateTime
System.out.println("\nAdding Nano/Second/Minute/Hour to ZonedDateTime :-\n");
// 2.1 add 125 NanoSeconds to Zoned time
ZonedDateTime add_125_Nanos = zonedDateTime.plusNanos(125);
System.out.println("1. After adding 125 Nano Seconds to Zoned Date/time is - "
+ add_125_Nanos);
// 2.2 add 37 Seconds to Zoned time
ZonedDateTime add_37_Seconds = zonedDateTime.plusSeconds(37);
System.out.println("2. After adding 37 Seconds to Zoned Date/time is - "
+ add_37_Seconds);
// 2.3 add 19 Minutes to Zoned time
ZonedDateTime add_19_Minutes = zonedDateTime.plusMinutes(19);
System.out.println("3. After adding 19 Minutes to Zoned Date/time is - "
+ add_19_Minutes);
// 2.4 add 5 Hours to Zoned time
ZonedDateTime add_5_Hours = zonedDateTime.plusHours(5);
System.out.print("4. After adding 5 Hours to Zoned Date/time is - "
+ add_5_Hours);
}
}
Output:
Zoned Date/time is :- 2022-08-14T19:40:28.109941800+05:30[Asia/Calcutta]
Adding Day/Week/Month/Year to ZonedDateTime :-
1. After adding 5 Days to Zoned Date/time is :- 2022-08-19T19:40:28.109941800+05:30[Asia/Calcutta]
2. After adding 2 Weeks to Zoned Date/time is :- 2022-08-28T19:40:28.109941800+05:30[Asia/Calcutta]
3. After adding 3 Months to Zoned Date/time is :- 2022-11-14T19:40:28.109941800+05:30[Asia/Calcutta]
4. After adding 1 Year to Zoned Date/time is :- 2023-08-14T19:40:28.109941800+05:30[Asia/Calcutta]
Adding Nano/Second/Minute/Hour to ZonedDateTime :-
1. After adding 125 Nano Seconds to Zoned Date/time is- 2022-08-14T19:40:28.109941925+05:30[Asia/Calcutta]
2. After adding 37 Seconds to Zoned Date/time is - 2022-08-14T19:41:05.109941800+05:30[Asia/Calcutta]
3. After adding 19 Minutes to Zoned Date/time is - 2022-08-14T19:59:28.109941800+05:30[Asia/Calcutta]
4. After adding 5 Hours to Zoned Date/time is - 2022-08-15T00:40:28.109941800+05:30[Asia/Calcutta]
Related Articles:
- Java 8 – ZonedDateTime with method details and examples
- Java 8 – How to get Date, Time and Zone fields from ZonedDateTime ?
- Java 8 – How to form ZonedDateTime passing Date, Time and Zone fields ?
- Java 8 – How to form ZonedDateTime passing LocalDate, LocalTime and ZoneId ?
- Java 8 – How to form ZonedDateTime passing LocalDateTime and ZoneId ?
- Java 8 – How to form ZonedDateTime passing Instant and ZoneId ?
- Java 8 – How to parse ZonedDateTime in String form ?
- Java 8 – How to convert String to ZonedDateTime ?
- Java 8 – How to convert ZonedDateTime to String ?
- Java 8 – How to convert ZonedDateTime in different formats ?
- Java 8 – How to convert ZonedDateTime in different Format Style ?
- Java 8 – How to convert ZonedDateTime to LocalDateTime ?
- Java 8 – How to convert ZonedDateTime to an OffsetDateTime ?
- Java 8 – How to convert ZonedDateTime to an Instant ?
- Java 8 – How to extract LocalDate and LocalTime and LocalDateTime from ZonedDateTime ?
- Java 8 – How to convert ZonedDateTime to java.util.Date and vice-versa ?
- Java 8 – How to convert ZonedDateTime to java.sql.Timestamp and vice-versa ?
- Java 8 – How to convert ZonedDateTime to Calendar and vice-versa ?
- Java 8 – How to convert ZonedDateTime to GregorianCalendar and vice-versa ?
- Java 8 – How to convert ZonedDateTime to XMLGregorianCalendar and vice-versa ?
- Java 8 – How to convert java.util.Date to ZonedDateTime in different ways ?
- Java 8 – How to add Date and Time fields to ZonedDateTime ?
- Java 8 – How to subtract Date and Time fields from ZonedDateTime ?
- Java 8 – How to alter Date, Time and Zone fields of ZonedDateTime ?
- Java 8 – How to check whether a ZonedDateTime is Before another ZonedDateTime ?
- Java 8 – How to check whether a ZonedDateTime is After another ZonedDateTime ?
- Java 8 – How to compare two ZonedDateTime instances ?
- Java 8 – How to find difference between two ZonedDateTime using Period & Duration ?
- More Java 8 Date/Time API examples
References:
- https://docs.oracle.com/javase/8/docs/api/java/time/ZonedDateTime.html
- https://docs.oracle.com/javase/8/docs/api/java/time/ZoneId.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 !!