Add one date to date in java

How to Add Days to Date in Java

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

Читайте также:  Html list style type color

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); 

Источник

How to increment a date by one day in Java?

In Java, if you want to increment a date by one day, you don’t need to worry because we have several solutions for you. In this article, we will use the latest Java 8 date time API, util date, and Calender class to increment a date by one day. Let’s get started.

Latest Java 8+ Solution:

Increment a date by one day by using plusDays() Method in Java

In this code, we used LocalDate class and now() method to get the current date. The plusDays() method is used to increment one day in the current date. We can add any number of days to the date , but for now, we add 1.

import java.time.LocalDate; /* * Code example to increment a day to date in Java */ public class JExercise < public static void main(String[] args) < // Current Date LocalDate localDate = LocalDate.now(); System.out.println("Current Date: "+localDate); // Add Weeks LocalDate newLocalDate = localDate.plusDays(1); System.out.println("Date After incrementing a Day: "+newLocalDate); >>

Current Date: 2021-06-12
Date After incrementing a Day: 2021-06-13

Increment a date by one day by using Calendar Class in Java

If you are working with the Calender class and want to add a day to the date, simply use add() method of it that takes two arguments, the first is a Date constant, and the second is the number of days you want to increment. See the code example below.

import java.text.ParseException; import java.util.Calendar; import java.util.Date; /* * Code example to increment a day to date in Java */ public class JExercise < public static void main(String[] args) throws ParseException < Calendar cal = Calendar.getInstance(); Date date = new Date(); System.out.println("Date: "+date); cal.setTime(date); cal.add(Calendar.DATE, 1); Date newDate = cal.getTime(); System.out.println("Date After incrementing a Day: "+newDate); >>

Date: Sat Jun 12 22:49:33 IST 2021
Date After incrementing a Day: Sun Jun 13 22:49:33 IST 2021

Format the date result

Since the above date result is a little lengthy, you can use SimpleDateFormat class to format the date as per your required format.

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); String formattedDate = format.format(newDate); System.out.println(formattedDate);

Increment a date by one day by using plusSeconds() Method in Java

This is another solution where you can increment a date by adding the seconds. The plusSeconds() method can be used if you have time in seconds. We used the Instant class of Java 8 date time API to get the current date in Java. See the code below.

import java.text.ParseException; import java.time.Instant; import java.util.Date; /* * Code example to increment a day to a date in Java */ public class JExercise < public static void main(String[] args) throws ParseException < System.out.println("Current Date :"+Instant.now()); Date date = Date.from(Instant.now().plusSeconds(86400)); System.out.println("Date After incrementing a Day: "+date); >>

Current Date :2021-06-12T17:33:03.222598Z
Date After incrementing a Day: Sun Jun 13 23:03:03 IST 2021

Increment a date by one day by using Apache Commons Library in Java

If you are working with Apache library and want to increment a date by one day, then use the addDays() method of DateUtils class. This method takes two arguments, the first is the date, and the second is the number of days you want to increment. It returns a date object after adding the day. See the code below.

import java.text.ParseException; import java.util.Date; import org.apache.commons.lang3.time.DateUtils; /* * Code example to increment a day to a date in Java */ public class JExercise < public static void main(String[] args) throws ParseException < Date date = new Date(); System.out.println("Date :"+date); Date nextDate = DateUtils.addDays(date, 1); System.out.println(nextDate); >>

Date :Sat Jun 12 23:12:47 IST 2021
Sun Jun 13 23:12:47 IST 2021

Источник

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.

Источник

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