Date | How Can I Increment A Date By One Day In Java?

In this tutorial, we will discuss How Can I Increment A Date By One Day In Java, we can do so using three different ways like pulsday(), add(), and jodaTime().

Date

Ways To Increment The Date

There are three different ways to increment the date by one and here we will discuss them one by one.

pulsday()

It is an inbuild function in the package of java.time that updates the date and we can add the number of dates as per our requirements we can give the number of days in digits and the date will get updated and the updated date will be displed on over.

To learn to update the date with plusday() method follow the example and demonstration given below.

public static String addOneDay(String date) {
    return LocalDate
      .parse(date)
      .plusDays(1)
      .toString();
}

Here is an example where we can see how the given with the dates an argument in a function and we get an updated date o to add ing one day to it with the help of the function.

Using java.util.Calendar

A calendar is also a way to add the days to an existing date so that we can update the date as per our requirements. It is somewhat the same as the plusday() but here we use different inbuild functions of the different packages to perform the same action here.

So let’s see how it works with one example given below.

public static String addOneDayCalendar(String date) 
  throws ParseException {
 
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    Calendar c = Calendar.getInstance();
    c.setTime(sdf.parse(date));
    c.add(Calendar.DATE, 1);
    return sdf.format(c.getTime());
}

It is as simple as we use add() inbuild function on place of plusday() so we need to import a different package to perform it.

To learn more about Date | How Can I Increment A Date By One Day In Java at Date and day updating using different in build functions.

And also visit Java Tutorials To get the list of solutions in java and also visit How To Reverse A String In Python? To learn more about python and other programming languages

Leave a Comment

%d bloggers like this: