How to get tomorrow’s date in Java

Java Date FAQ: How do I determine tomorrow’s date in Java?

Many times when you're working with a Java Date, you need to be able to add something to a date, i.e., to get tomorrow's date, or the next week, or the next year. In this short tutorial, we'll demonstrate how to easily add one day to today to get tomorrow's date.

Java Date add example

The following Java class shows how to get tomorrow's date in just a few lines of code. Not counting all the boilerplate code and comments, you can get tomorrow's date in four lines of code, or fewer. Here's the source code for our class that shows how to get "tomorrow" by adding one day to today:

import java.util.*;

/**
 * A Java Date and Calendar example that shows how to
 * get tomorrow's date (i.e., the next day).
 * 
 * @author alvin alexander, devdaily.com
 */
public class JavaDateAddExample
{

  public static void main(String[] args)
  {
    // get a calendar instance, which defaults to "now"
    Calendar calendar = Calendar.getInstance();
    
    // get a date to represent "today"
    Date today = calendar.getTime();
    System.out.println("today:    " + today);
 
    // add one day to the date/calendar
    calendar.add(Calendar.DAY_OF_YEAR, 1);
    
    // now get "tomorrow"
    Date tomorrow = calendar.getTime();

    // print out tomorrow's date
    System.out.println("tomorrow: " + tomorrow);
  }

}

When I run this test class as I'm writing this article, the output from this class looks like this:

today:    Tue Sep 22 08:13:29 EDT 2009
tomorrow: Wed Sep 23 08:13:29 EDT 2009

As you can, tomorrow's date is 24 hours in the future from today's date (i.e., "now").

Java Date add - discussion

In this example date/calendar code, I first get today's date using these two lines of code:

Calendar calendar = Calendar.getInstance();
Date today = calendar.getTime();

I then move our Java Calendar instance one day into the future by adding one day to it by calling the Calendar add method with this line of code:

calendar.add(Calendar.DAY_OF_YEAR, 1);

With our Calendar now pointing one day in the future, I now get a Date instance that represents tomorrow with this line of code:

Date tomorrow = calendar.getTime();

Java Date add - summary

As you can see from the example source code, there are probably two main keys to knowing how to add a day to today's date to get a Date instance that refers to "tomorrow":

  • Knowing how to get a Date instance from the Java Calendar class.
  • Knowing how to perform Date math with the Calendar add method.

Comments

Permalink

Date myDate = new Date();
myDate.setTime(myDate.getTime() + 86400000);

(86400000 milliseconds = 1 day)

Very nice, thanks. I had gotten used to most of the Java Date class constructors being deprecated, and somewhere along the line I quit using the no-args constructor, which is not deprecated, and just started using the Calendar class.