- How to increment a date by one day in Java?
- Increment a date by one day by using plusDays() Method in Java
- Increment a date by one day by using Calendar Class in Java
- Format the date result
- Increment a date by one day by using plusSeconds() Method in Java
- Increment a date by one day by using Apache Commons Library in Java
- How to Add Days to Date in Java
- Using plusdays() method of LocalDate (Java 8)
- Add days to current date using LocalDate
- Add days to given date using LocalDate
- Using add() method Calendar class
- Add days to current date using Calendar
- Add days to given date using Calendar
- Was this post helpful?
- Share this
- Related Posts
- Author
- Related Posts
- Check if Date Is Between Two Dates in Java
- Find Difference Between Two Instant in Java
- Find Difference Between Two LocalDate in Java
- Get List of Weekday Names in Java
- Get List of Month Names in Java
- Get Unix Timestamp in Java
- Add One Day to a Date in Java
- plusDays() Method to Add One Day to a Date in Java
- Calendar Method to Add One Day to a Date in Java
- Add Milliseconds to Add One Day to a Date in Java
- Instant Class to Add One Day to a Date in Java
- Related Article — Java DateTime
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
How to Add Days to Date in Java
In this post, we will see how to add days to date in 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
Related Posts
Author
Related Posts
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 One Day to a Date in Java
- plusDays() Method to Add One Day to a Date in Java
- Calendar Method to Add One Day to a Date in Java
- Add Milliseconds to Add One Day to a Date in Java
- Instant Class to Add One Day to a Date in Java
In this tutorial, we will learn how to add days to a date in Java. It can be done using various approaches like the plusDays method, the Calendar class method, adding milliseconds to a Date object, and the Instant class method. If you are using Java 1.8 or greater, then the plusDays approach is recommended.
plusDays() Method to Add One Day to a Date in Java
In Java 1.8 onward new java.time classes i.e. LocalDate , LocalDateTime have plusDays and minusDays methods to add and subtract time unit from any time instance.
// java 1.8 package simpletesting; import java.time.LocalDateTime; public class SimpleTesting public static void main(String[] args) LocalDateTime today = LocalDateTime.now(); //Today LocalDateTime tomorrow = today.plusDays(1); //Plus 1 day LocalDateTime yesterday = today.minusDays(1); //Minus 1 day System.out.println("Today: "+today); System.out.println("Tomorrow: "+tomorrow); System.out.println("Yesterday: "+yesterday); > >
Today: 2020-03-22T19:01:00.728 Tomorrow: 2020-03-23T19:01:00.728 Yesterday: 2020-03-21T19:01:00.728
Calendar Method to Add One Day to a Date in Java
We can use the Calendar class to add one day to a Date in Java. It can be done by simply adding one day to Calendar class instance:
// java 1.8 package simpletesting; import java.util.Calendar; import java.util.Date; public class SimpleTesting public static void main(String[] args) Date dt = new Date(); System.out.println("Today: "+dt); Calendar c = Calendar.getInstance(); c.setTime(dt); c.add(Calendar.DATE, 1); dt = c.getTime(); System.out.println("Tomorrow: "+dt); > >
Today: Sun Mar 22 19:07:48 PKT 2020 Tomorrow: Mon Mar 23 19:07:48 PKT 2020
Add Milliseconds to Add One Day to a Date in Java
Date has a constructor using milliseconds. The getTime() method gives us that value. So adding the milliseconds for one day will add a day to Date .
// java 1.8 package simpletesting; import java.util.Date; public class SimpleTesting public static void main(String[] args) Date dt = new Date(); System.out.println("Today: " + dt); Date tomorrow = new Date(dt.getTime() + (1000 * 60 * 60 * 24)); System.out.println("Tomorrow: " + tomorrow); > >
Today: Sun Mar 22 19:15:27 PKT 2020 Tomorrow: Mon Mar 23 19:15:27 PKT 2020
Be careful; if we use a Calendar Timezone with daylight savings, it may not jump to the next day.
Instant Class to Add One Day to a Date in Java
The Instant class is close to being equivalent to Date . Instant resolves to nanoseconds. The instant.plus method adds the given days to Date .
// java 1.8 package simpletesting; import java.time.Instant; import java.time.temporal.ChronoUnit; import java.util.Date; public class SimpleTesting public static void main(String[] args) Date dt = new Date(); System.out.println("Today: " + dt); Instant instant = dt.toInstant(); Instant nextDay = instant.plus(1, ChronoUnit.DAYS); System.out.println("Tomorrow: " + nextDay); > >
Today: Sun Mar 22 19:19:58 PKT 2020 Tomorrow: 2020-03-23T14:19:58.072Z