Calculating Java dates

Take the time to learn how to create and use dates

1 2 Page 2
Page 2 of 2

After the program is run, gc1 and gc2 are changed to the year 2001 (because both objects are pointing to the same underlying date representation, which has been changed). The object gc3 is pointing to a separate underlying date representation, which has not been changed.

Calculating review dates

At this point, you have a program based on a real-world example. This particular program will calculate the dates for reviewing material. For example, while reading this article, you might come across some points you want to remember. Unless you have photographic memory, you will likely find that periodically reviewing the new material will aid in remembering it. One reviewing system, discussed in Kurt Hanks and Gerreld L. Pulsipher's Five Secrets to Personal Productivity, suggests briefly covering the material immediately after it is first seen, then after one day, one week, one month, three months, and one year. For this article, you would give it a quick review immediately, another tomorrow, then one week, one month, three months, and one year from now. Our program will calculate the dates.

To be most useful, the program would be incorporated in PIM (Personal Information Manager) software, which would run the program and schedule the reviews. The getDates() method in the following ReviewDates program would be useful for integrating with electronic software as it returns an array of dates (the review dates). Additionally, you can return individual dates using the methods getFirstDay(), getOneDay(), getOneWeek(), getOneMonth(), and getOneYear(). While it is beyond the scope of this article to integrate the ReviewDates class with a PIM, the ReviewDates class does show how to calculate dates based on elapsed time. Now you can easily modify it for other operations that require elapsed time, such as library loans, tape rentals, and mortgage calculations. First, the ReviewDates class is shown below:

import java.util.*;
import java.text.*;
public class ReviewDates {
   private GregorianCalendar firstDay, oneDay, oneWeek, oneMonth, oneQuarter, oneYear;
   final int dateArraySize = 6;
   ReviewDates(GregorianCalendar gcDate) {
      int year = gcDate.get(GregorianCalendar.YEAR);
      int month = gcDate.get(GregorianCalendar.MONTH);
      int date = gcDate.get(GregorianCalendar.DATE);
      firstDay = new GregorianCalendar(year, month, date);
      oneDay = new GregorianCalendar(year, month, date);
      oneWeek = new GregorianCalendar(year, month, date);
      oneMonth = new GregorianCalendar(year, month, date);
      oneQuarter = new GregorianCalendar(year, month, date);
      oneYear = new GregorianCalendar(year, month, date);
      oneDay.add(GregorianCalendar.DATE, 1);
      oneWeek.add(GregorianCalendar.DATE, 7);
      oneMonth.add(GregorianCalendar.MONTH, 1);
      oneQuarter.add(GregorianCalendar.MONTH, 3);
      oneYear.add(GregorianCalendar.YEAR, 1);
   ReviewDates() {
      this(new GregorianCalendar());
   public void listDates() {
      DateFormat df = DateFormat.getDateInstance(DateFormat.LONG); 
      Date startDate = firstDay.getTime();
      Date date1 = oneDay.getTime();
      Date date2 = oneWeek.getTime();
      Date date3 = oneMonth.getTime();
      Date date4 = oneQuarter.getTime();
      Date date5 = oneYear.getTime();
      String ss =  df.format(startDate);
      String ss1 = df.format(date1);
      String ss2 = df.format(date2);
      String ss3 = df.format(date3);
      String ss4 = df.format(date4);
      String ss5 = df.format(date5);
      System.out.println("Start date is " + ss);
      System.out.println("Following review dates are:");
   public GregorianCalendar[] getDates() {
      GregorianCalendar[] memoryDates = new GregorianCalendar[dateArraySize];
      memoryDates[0] = firstDay;
      memoryDates[1] = oneDay;
      memoryDates[2] = oneWeek;
      memoryDates[3] = oneMonth;
      memoryDates[4] = oneQuarter;
      memoryDates[5] = oneYear;
      return memoryDates;
   public GregorianCalendar getFirstDay() {
      return this.firstDay;
   public GregorianCalendar getOneDay() {
      return this.oneDay;
   public GregorianCalendar getOneWeek() {
      return this.oneWeek;
   public GregorianCalendar getOneMonth() {
      return this.oneMonth;
   public GregorianCalendar getOneQuarter() {
      return this.oneQuarter;
   public GregorianCalendar getOneYear() {
      return this.oneYear;

An example of a program that uses the ReviewDates class to make a simple listing of review dates is shown below:

import java.util.*;
public class ShowDates {
   public static void main(String[] args) {
      ReviewDates rd = new ReviewDates();
      GregorianCalendar gc = new GregorianCalendar(2001, Calendar.JANUARY, 15);
      ReviewDates jan15 = new ReviewDates(gc);


This article has introduced three important classes for working with dates: Date, DateFormat, and GregorianCalendar. These classes let you create dates, change them into Strings, and make elementary calculations with dates. In terms of working with dates in Java, this article has only scratched the surface. However, the classes and methods that I have introduced here not only serve as springboard for more advanced learning, but in themselves can also handle many common date-related tasks.

Robert Nielsen is a Sun-Certified Java 2 Programmer. He holds a master's degree in education with a specialty in computer-assisted instruction, and has taught for several years. He has also published computer-related articles in a variety of magazines.

Learn more about this topic

This story, "Calculating Java dates" was originally published by JavaWorld.


Copyright © 2000 IDG Communications, Inc.

1 2 Page 2
Page 2 of 2
How to choose a low-code development platform