Java calendar plus days

How to add days to a date in Java

In an earlier article, we looked at different ways to get the current date and time in Java. In this article, you’ll learn how to add days, months, and years to date using both Java 8 new date and time API and legacy Date and Calendar API.

A new date and time API was introduced in Java 8 to fix the flaws in the old java.util.Date and java.util.Calendar APIs. The new API provides utility methods like plusDays() and minusDays() to manipulate LocalDate , LocalDateTime , ZonedDateTime , and OffsetDateTime .

The LocalDate class represents a date without time in ISO-8601 format (yyyy-MM-dd). The following example shows how you can add days, years, and months to an instance of LocalDate :

// increment days by 7 LocalDate date = LocalDate.now(); System.out.println("Current Date: " + date); date = date.plusDays(7); System.out.println("Date after Increment: " + date); // increment month, day, year LocalDate date2 = LocalDate.of(2016, Month.AUGUST, 15); System.out.println("Original Date: " + date2); date2 = date2.plusDays(5).plusMonths(3).plusYears(1); System.out.println("Date after Increment: " + date2); // minus days, month LocalDate date3 = LocalDate.parse("2019-12-25"); System.out.println("Original Date: " + date3); date3 = date3.minusDays(5).minusMonths(5); System.out.println("Date after Decrement: " + date3); 
Current Date: 2019-12-26 Date after Increment: 2020-01-02 Original Date: 2016-08-15 Date after Increment: 2017-11-20 Original Date: 2019-12-25 Date after Decrement: 2019-07-20 

A LocalDateTime represents date and time without timezone information in ISO-8601 format. To add or subtract days from an instance of LocalDateTime , you can do the following:

// increment days by 15 LocalDateTime datetime = LocalDateTime.now(); System.out.println("Current Date & Time: " + datetime); datetime = datetime.plusDays(15); System.out.println("Date & Time after Increment: " + datetime); // increment month, day, year LocalDateTime datetime2 = LocalDateTime.of(2015, Month.AUGUST, 5, 12, 45); System.out.println("Original Date & Time: " + datetime2); datetime2 = datetime2.plusDays(20).plusMonths(10).plusYears(3); System.out.println("Date & Time after Increment: " + datetime2); // minus days, month LocalDateTime datetime3 = LocalDateTime.parse("2019-12-25T15:18:25"); System.out.println("Original Date & Time: " + datetime3); datetime3 = datetime3.minusDays(5).minusMonths(5); System.out.println("Date & Time after Decrement: " + datetime3); 
Current Date & Time: 2019-12-26T18:07:15.607 Date & Time after Increment: 2020-01-10T18:07:15.607 Original Date & Time: 2015-08-05T12:45 Date & Time after Increment: 2019-06-25T12:45 Original Date & Time: 2019-12-25T15:18:25 Date & Time after Decrement: 2019-07-20T15:18:25 

The ZonedDateTime represents a date and time with a timezone in ISO-8601 format (e.g 2016-12-15T10:15:30+01:00[Europe/Paris]). Here is an example that shows how to add and minus days from an instance of ZonedDateTime :

// increment days by 25 ZonedDateTime datetime = ZonedDateTime.now(ZoneId.systemDefault()); System.out.println("Current Date & Time: " + datetime); datetime = datetime.plusDays(25); System.out.println("Date & Time after Increment: " + datetime); // increment month, day, year ZonedDateTime datetime2 = ZonedDateTime.of(2010, 5, 5, 12, 45, 0, 0, ZoneId.of("Asia/Karachi")); System.out.println("Original Date & Time: " + datetime2); datetime2 = datetime2.plusDays(5).plusMonths(2).plusYears(2); System.out.println("Date & Time after Increment: " + datetime2); // minus days, month ZonedDateTime datetime3 = ZonedDateTime.parse("2016-12-15T10:15:30+01:00[Europe/Paris]"); System.out.println("Original Date & Time: " + datetime3); datetime3 = datetime3.minusDays(15).minusMonths(8); System.out.println("Date & Time after Decrement: " + datetime3); 
Current Date & Time: 2019-12-26T18:12:28.358+05:00[Asia/Karachi] Date & Time after Increment: 2020-01-20T18:12:28.358+05:00[Asia/Karachi] Original Date & Time: 2010-05-05T12:45+05:00[Asia/Karachi] Date & Time after Increment: 2012-07-10T12:45+05:00[Asia/Karachi] Original Date & Time: 2016-12-15T10:15:30+01:00[Europe/Paris] Date & Time after Decrement: 2016-03-30T10:15:30+02:00[Europe/Paris] 

OffsetDateTime is another class from Java 8 new date and time API that represents a date and time with an offset from UTC/Greenwich in the ISO-8601 format (e.g. 2017-12-30T23:15:30-05:00). The following example demonstrates how you can add or minus days, months, and years from an object of OffsetDateTime :

// increment days by 14 OffsetDateTime datetime = OffsetDateTime.now(); System.out.println("Current Date & Time: " + datetime); datetime = datetime.plusDays(14); System.out.println("Date & Time after Increment: " + datetime); // increment month, day, year OffsetDateTime datetime2 = OffsetDateTime.of(2014, 12, 15, 23, 45, 55, 0, ZoneOffset.of("+03:30")); System.out.println("Original Date & Time: " + datetime2); datetime2 = datetime2.plusDays(15).plusMonths(5).plusYears(4); System.out.println("Date & Time after Increment: " + datetime2); // minus days, month OffsetDateTime datetime3 = OffsetDateTime.parse("2017-12-30T23:15:30-05:00"); System.out.println("Original Date & Time: " + datetime3); datetime3 = datetime3.minusDays(10).minusMonths(6); System.out.println("Date & Time after Decrement: " + datetime3); 
Current Date & Time: 2019-12-26T18:18:43.725+05:00 Date & Time after Increment: 2020-01-09T18:18:43.725+05:00 Original Date & Time: 2014-12-15T23:45:55+03:30 Date & Time after Increment: 2019-05-30T23:45:55+03:30 Original Date & Time: 2017-12-30T23:15:30-05:00 Date & Time after Decrement: 2017-06-20T23:15:30-05:00 

Before Java 8, java.util.Date and java.util.Calendar classes were used for handling dates and times. To add or minus days from an instance of Date , you can use the Calendar.add() method as shown below:

// date pattern SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); // increment current date by 7 days Date date = new Date(); System.out.println("Current Date: " + formatter.format(date)); // use `Calendar` to add days Calendar c = Calendar.getInstance(); c.setTime(date); c.add(Calendar.DATE, 7); // print date after the increment System.out.println("Date after Increment: " + formatter.format(c.getTime())); 
Current Date: 2019-12-26 Date after Increment: 2020-01-02 

Here is another example that shows how you can parse a string to a date and then add or minus days, months, years, seconds, and hours from an instance of Date :

try  // date pattern SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss"); // parse string to date Date date = formatter.parse("17-Jul-2018 05:15:45"); System.out.println("Original Date: " + formatter.format(date)); // use `Calendar` to add days, months, years Calendar c = Calendar.getInstance(); c.setTime(date); c.add(Calendar.DATE, 7); c.add(Calendar.MONTH, -2); // minus months c.add(Calendar.YEAR, 1); c.add(Calendar.HOUR, 2); c.add(Calendar.MINUTE, 12); c.add(Calendar.SECOND, 45); // print date after the increment System.out.println("Date after Increment: " + formatter.format(c.getTime())); > catch (ParseException ex)  ex.printStackTrace(); > 
Original Date: 17-Jul-2018 05:15:45 Date after Increment: 24-May-2019 07:28:30 

In this guide, we looked at different ways to add and subtract different date and time units like days, months, and years from a date. Java 8 new date and time API provides plenty of utility methods for manipulating dates and times. We learned to use plusDays() and minusDays() methods to add or minus days from an instance of LocalDate , LocalDateTime , ZonedDateTime , and OffsetDateTime . Finally, we also looked at how to use the Calendar.add() method to add or subtract a specified amount of time from an instance of the legacy Date class. ✌️ Like this article? Follow me on Twitter and LinkedIn. You can also subscribe to RSS Feed.

You might also like.

Источник

Java – Add days to Date

In this tutorial we will see how to add Days to the date in Java.

Table of contents

1. Adding Days to the given Date using Calendar class

In this example we have given a date “2017-01-29” and we are adding the days to it using Calendar class.

import java.text.SimpleDateFormat; import java.util.Calendar; import java.text.ParseException; class Example< public static void main(String args[])< //Given Date in String format String oldDate = "2017-01-29"; System.out.println("Date before Addition: "+oldDate); //Specifying date format that matches the given date SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Calendar c = Calendar.getInstance(); try< //Setting the date to the given date c.setTime(sdf.parse(oldDate)); >catch(ParseException e) < e.printStackTrace(); >//Number of Days to add c.add(Calendar.DAY_OF_MONTH, 7); //Date after adding the days to the given date String newDate = sdf.format(c.getTime()); //Displaying the new Date after addition of Days System.out.println("Date after Addition: "+newDate); > >
Date before Addition: 2017-01-29 Date after Addition: 2017-02-05

2. Adding Days to the current Date using Calendar class

This is similar to the above example except that here we are adding the days to the current date instead of given date.

import java.text.SimpleDateFormat; import java.util.Calendar; class Example < public static void main(String args[])< SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd"); //Getting current date Calendar cal = Calendar.getInstance(); //Displaying current date in the desired format System.out.println("Current Date: "+sdf.format(cal.getTime())); //Number of Days to add cal.add(Calendar.DAY_OF_MONTH, 7); //Date after adding the days to the current date String newDate = sdf.format(cal.getTime()); //Displaying the new Date after addition of Days to current date System.out.println("Date after Addition: "+newDate); >>
Current Date: 2017/10/18 Date after Addition: 2017/10/25

3. Java – Increment a Date by 1 Day

The reason I have mentioned this in a separate heading is because people ask this a lot. We have already seen how to add days to a date in Java in the above programs. To increment the date or current date by one, you just need to add 1 day to the date.

3.1 Add One Day to a given date

import java.text.SimpleDateFormat; import java.util.Calendar; import java.text.ParseException; class Example< public static void main(String args[])< String oldDate = "2017-01-29"; System.out.println("Date before Addition: "+oldDate); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Calendar c = Calendar.getInstance(); try< c.setTime(sdf.parse(oldDate)); >catch(ParseException e) < e.printStackTrace(); >//Incrementing the date by 1 day c.add(Calendar.DAY_OF_MONTH, 1); String newDate = sdf.format(c.getTime()); System.out.println("Date Incremented by One: "+newDate); > >
Date before Addition: 2017-01-29 Date Incremented by One: 2017-01-30

3.2 Add One Day to the current date

import java.text.SimpleDateFormat; import java.util.Calendar; class Example < public static void main(String args[])< SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd"); Calendar cal = Calendar.getInstance(); System.out.println("Current Date: "+sdf.format(cal.getTime())); //Adding 1 Day to the current date cal.add(Calendar.DAY_OF_MONTH, 1); //Date after adding one day to the current date String newDate = sdf.format(cal.getTime()); //Displaying the new Date after addition of 1 Day System.out.println("Incremnted current date by one: "+newDate); >>

4. Java 8 – Adding Days to Date

In java 8, it is so easy to add days to a date without Calendar class. Here we use the LocalDate class provided in the new package java.time which is introduced in Java 8.

import java.time.LocalDate; class Example < public static void main(String args[])< //Adding one Day to the current date LocalDate date = LocalDate.now().plusDays(1); System.out.println("Adding one day to current date: "+date); //Adding number of Days to the current date LocalDate date2 = LocalDate.now().plusDays(7); System.out.println("Adding days to the current date: "+date2); //Adding one Day to the given date LocalDate date3 = LocalDate.of(2016, 10, 14).plusDays(1); System.out.println("Adding one day to the given date: "+date3); //Adding number of Days to the given date LocalDate date4 = LocalDate.of(2016, 10, 14).plusDays(9); System.out.println("Adding days to the given date: "+date4); >>
Adding one day to current date: 2017-10-19 Adding days to the current date: 2017-10-25 Adding one day to the given date: 2016-10-15 Adding days to the given date: 2016-10-23

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

Источник

Читайте также:  Python numpy reverse array
Оцените статью