How to Add one Day to a Date in Java

  1. plusDays() method to add one day to a Date in Java
  2. Calendar method to add one day to a Date in Java
  3. Add Milliseconds to add one day to a Date in Java
  4. 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. This can be done by using various approaches like plusDays method, Calendar class method, adding milliseconds to a Date object, and Instant class method. If you are using Java 1.8 or greater, then 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.

Example Codes:

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

Output:

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

Output:

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.

Code:

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

Output:

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. instant.plus method adds the given days to Date.

Example Codes:

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

Output:

Today:    Sun Mar 22 19:19:58 PKT 2020
Tomorrow: 2020-03-23T14:19:58.072Z
comments powered by Disqus