Java Date and Calendar “add” examples

Java Date class "addition" FAQ: Can you show me how to add various times increments to a Java Date object?

I've been doing a lot of work with the Date class in Java lately (both java.util.Date and java.sql.Date), and before leaving this area for a while, I thought I'd put together some "Java Date add" examples that demonstrate various "date add" and "calendar add" techniques, including how to get the date and time one hour from now, tomorrow's date, next week, next month, and next year.

Java Date and Calendar addition example class

To help demonstrate these Date addition techniques I've put together the following JavaDateAddExamples class. I've documented the class source code reasonably well, so hopefully it will all make sense:

import java.util.*;

/**
 * A collection of Java "Date add" and "Calendar add" examples.
 * Shows how to get the next hour, next day, next week, next month, and next year.
 * 
 * @author alvin alexander, devdaily.com
 */
public class JavaDateAddExamples
{
  public static void main(String[] args)
  {
    // get a calendar instance at December 31, 2009, at 11:30 p.m.
    // this way we can test that we are rolling over to the next hour,
    // tomorrow, next week, and next year properly.
    Calendar calendar = new GregorianCalendar(2009, 11, 31, 23, 30, 0);
    
    // get a Date instance to represent "now" (the current date);
    // we'll need it to reset our calendar during the following date examples.
    Date currentDate = calendar.getTime();
    System.out.format("today:      %s\n", currentDate);
 
    // get the date/time one hour from now
    calendar.setTime(currentDate);
    calendar.add(Calendar.HOUR_OF_DAY, 1);
    Date oneHour = calendar.getTime();
    System.out.format("one hour:   %s\n", oneHour);
    
    // get tomorrow's date
    calendar.setTime(currentDate);
    calendar.add(Calendar.DAY_OF_YEAR, 1);
    Date tomorrow = calendar.getTime();
    System.out.format("tomorrow:   %s\n", tomorrow);
    
    // get next week's date
    // note: may want to use WEEK_OF_MONTH or WEEK_OF_YEAR
    calendar.setTime(currentDate);
    calendar.add(Calendar.DAY_OF_YEAR, 7);
    Date nextWeek = calendar.getTime();
    System.out.format("next week:  %s\n", nextWeek);
  
    // get next month
    calendar.setTime(currentDate);
    calendar.add(Calendar.MONTH, 1);
    Date nextMonth = calendar.getTime();
    System.out.format("next month: %s\n", nextMonth);
  
    // get next year
    calendar.setTime(currentDate);
    calendar.add(Calendar.YEAR, 1);
    Date nextYear = calendar.getTime();
    System.out.format("next year:  %s\n", nextYear);
  }
}

As you can see from the code, I'm intentionally setting the date and time of my Calendar object to 11:30 p.m. on New Year's Eve, so whenever I add anything to it I will automatically cross into another day, month, and year.

Once you've seen how to perform some of these Java Date and Calendar addition examples there isn't too much to say about them, but I will say that I'm not entirely comfortable with what I've done in my "next week" example. In your real-world code that approach may need to be a little more robust than what I've shown here.

Java date addition example - output

Here's the output from this Java date addition class:

today:      Thu Dec 31 23:30:00 EST 2009
one hour:   Fri Jan 01 00:30:00 EST 2010
tomorrow:   Fri Jan 01 23:30:00 EST 2010
next week:  Thu Jan 07 23:30:00 EST 2010
next month: Sun Jan 31 23:30:00 EST 2010
next year:  Fri Dec 31 23:30:00 EST 2010

I haven't tried many other dates and times with this code, but for the times I have tried all the results look correct. Again, if the "next week" addition is really important to you, you might want to make that code more robust, but that's the only glaring weakness I see right away.

Java Date and Calendar references

Here are a few links to the official Javadoc for the Date and Calendar classes used in these "Java Date add" examples: