Localdate plus days java

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.

Источник

Читайте также:  Page Title

Java 8 – Adding Days to the LocalDate

In this tutorial we will see how to add days to the LocalDate.

Java LocalDate Example 1: Adding Days to the current Date

In this example, we are adding one day to the current date. We are using now() method of LocalDate class to get the current date and then using the plusDays() method to add the specified number of days to the LocalDate.

import java.time.LocalDate; public class Example < public static void main(String[] args) < //current date LocalDate today = LocalDate.now(); //adding one day to the localdate LocalDate tomorrow = today.plusDays(1); System.out.println("Tomorrow's Date: "+tomorrow); >>

Example 2: Adding Days to the given LocalDate

In this example, we have the given date, which we are converting to LocalDate instance using the of() method and then we are adding the days to it using plusDays() method, similar to what we have seen in the above example.

import java.time.LocalDate; public class Example < public static void main(String[] args) < //given date LocalDate localDate = LocalDate.of(2017, 06, 22); //adding 5 days to the given localdate LocalDate newDate = localDate.plusDays(5); System.out.println("Date after addition: "+newDate); >>
Date after addition: 2017-06-27

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.

Источник

How to Add Days to Date in Java

In this post, we will see how to add days to date in java.

Читайте также:  Java метод принимает массив

There are multiple ways to add days to date in java. Let’s go through them.

Using plusdays() method of LocalDate (Java 8)

If you want to add days to date without Calendar class, this is recommended way to add days to Date in java. It will work for Java 8 or later.

Let’s see with the help of example:

Add days to current date using LocalDate

We can use LocalDate.now() to get current date and use plusDays() method to add days to LocalDate.

Add days to given date using LocalDate

We can use LocalDate.now() to get current date and use plusDays() method to add days to LocalDate.

Using add() method Calendar class

You can use add() method of Calendar class to add days to date, but this is not recommended in case you are using java 8 or later version.

Let’s see with the help of example:

Add days to current date using Calendar

We can use Calendar’s setTime() method to set current date and use add() method to add days to LocalDate.

Current date: Tue Dec 22 13:08:11 IST 2020
Adding 1 days to current date: Wed Dec 23 13:08:11 IST 2020
Adding 7 days to current date: Tue Dec 29 13:08:11 IST 2020

Add days to given date using Calendar

We can use Calendar’s setTime() method to set current date and use add() method to add days to LocalDate.

Current date: Tue Dec 22 13:08:11 IST 2020
Given date: Wed Jan 10 13:14:18 IST 2018
Adding 1 days to current date: Thu Jan 11 13:14:18 IST 2018

That’s all about how to add days to date in java.

Was this post helpful?

Share this

Author

Check if Date Is Between Two Dates in Java

Table of ContentsIntroductionDate & Local Date Class in JavaCheck if The Date Is Between Two Dates in JavaUsing the isAfter() and isBefore() Methods of the Local Date Class in JavaUsing the compareTo() Method of the Local Date Class in JavaUsing the after() and before() Methods of the Date Class in JavaUsing the compare To() Method […]

Find Difference Between Two Instant in Java

Table of ContentsWays to find difference between two Instant in JavaUsing Instant’s until() methodUsing ChronoUnit EnumUsing Duration’s between() method In this post, we will see how to find difference between two Instant in Java. Ways to find difference between two Instant in Java There are multiple ways to find difference between two Instant in various […]

Find Difference Between Two LocalDate in Java

Table of ContentsWays to find difference between two LocalDate in JavaUsing LocalDate’s until() methodUsing ChronoUnit EnumUsing Duration’s between() methodUsing Period’s between() methodFrequently asked questionsHow to find difference between two LocalDate in Java in Days?How to find difference between two LocalDate in Java in Years?How to find difference between two LocalDate in Java in Months?How to […]

Get List of Weekday Names in Java

Table of ContentsUsing DateFormatSymbols’s getWeekdays() methodUsing DateFormatSymbols’s getShortWeekdays() method [For short names]Using DateFormatSymbols’s getWeekdays() with Locale In this post, we will see how to get list of weekday names in Java. Using DateFormatSymbols’s getWeekdays() method We can use DateFormatSymbols’s getWeekdays() to get list of weekday names in Java. It will provide complete weekday names like […]

Читайте также:  Площадь треугольника формула герона питон

Get List of Month Names in Java

Table of ContentsUsing DateFormatSymbols’s getMonth() methodUsing DateFormatSymbols’s getShortMonths() method [ For short names]Using DateFormatSymbols’s getMonth() with Locale In this post, we will see how to get list of months name in Java. Using DateFormatSymbols’s getMonth() method We can use DateFormatSymbols’s getMonth() to get list of months in Java. It will provide complete month names like […]

Get Unix Timestamp in Java

Table of ContentsWays to get unix timestamp in JavaUsing Instant classUsing System.currentTimeMillis()Using Date’s getTime() methodConvert Date to unix timestamp in JavaUsing Instant classUsing Date’s getTime() method In this post, we will get unix timestamp in Java. As per wikipedia Unix time is a system for describing a point in time. It is the number of […]

Источник

Add or Subtract Days, Months & Years to Date in Java

Java examples to add or subtract days, months or years from a given date using various date-time classes. If your requirement is to add or subtract only the business days then read the linked article.

1. Add or Subtract Days, Months, Years to Date since Java 8

This recommended approach if we are using JDK 1.8 or later.

New java.time classes LocalDate , LocalDateTime and ZonedDateTime have following plus methods to add days to a date.

  • plusDays(long n) – adds n days to date.
  • plusWeeks(long n) – adds n weeks to date.
  • plusMonths(long n) – adds n months to date.
  • plusYears(long n) – adds n years to date.

Similarly, use the following minus methods to subtract days from a date.

  • minusDays(long n) – subtracts n days from date.
  • minusWeeks(long n) – subtracts n weeks from date.
  • minusMonths(long n) – subtracts n months from date.
  • minusYears(long n) – subtracts n years from date.

Before returning the modified date, these methods modify the other date fields as well to ensure that the result date is a valid date.

These methods throw DateTimeException if the result exceeds the supported date range.

//1. Add and substract 1 day from LocalDate LocalDate today = LocalDate.now(); //Today LocalDate tomorrow = today.plusDays(1); //Plus 1 day LocalDate yesterday = today.minusDays(1); //Minus 1 day //2. Add and substract 1 month from LocalDateTime LocalDateTime now = LocalDateTime.now(); //Current Date and Time LocalDateTime sameDayNextMonth = now.plusMonths(1); //2018-08-14 LocalDateTime sameDayLastMonth = now.minusMonths(1); //2018-06-14 //3. Add and substract 1 year from LocalDateTime LocalDateTime sameDayNextYear = now.plusYears(1); //2019-07-14 LocalDateTime sameDayLastYear = now.minusYears(1); //2017-07-14

2. Add or Subtract Days from java.util.Date

Till Java 7, the only good way to add days to Date was using Calendar class.

The calendar.add(int field, int amount) method takes two arguments, i.e., field type and the field value. We can use this method to add days, months or any time unit in the underlying Date class.

  • To add a time unit, pass a positive number in the method.
  • To subtract a time unit, pass a negative number in the method.
Date today = new Date(); System.out.println(today); Calendar cal = Calendar.getInstance(); cal.setTime(today); // Adding time cal.add(Calendar.YEAR, 2); cal.add(Calendar.MONTH, 2); cal.add(Calendar.DATE, 2); cal.add(Calendar.DAY_OF_MONTH, 2); // Subtracting time cal.add(Calendar.YEAR, -3); cal.add(Calendar.MONTH, -3); cal.add(Calendar.DATE, -3); cal.add(Calendar.DAY_OF_MONTH, -3); // convert calendar to date Date modifiedDate = cal.getTime(); System.out.println(modifiedDate); 

Источник

Оцените статью