CIS 35A: Introduction to Java Programming

Home | Green Sheet | Lectures | Assignments | FAQ | Grades

Dates

Dates and Strings
Dates and times
Calendar

GregorianCalendar is a subclass of Calendar.

The Calendar class

java.util.Calendar;

Common fields of the Calendar class

ConstantDescription
YEARThe year portion of the calendar date
MONTHThe month portion of the calendar date
DATEThe day of the month
DAY_OF_YEARThe day number within the year
DAY_OF_MONTHThe day number within the month
DAY_OF_WEEKThe day of the week (Sun-1, Mon-2, etc.)
WEEK_OF_YEARThe week number within the year
WEEK_OF_MONTHThe week number within the month
AM_PMThe indicator for AM or PM (AM-0 and PM-1)
HOURThe hour in 12-hour notation
HOUR_OF_DAYThe hour in 24-hour notation
MINUTEThe minute within the hour
SECONDThe second within the minute
MONDAY...SUNDAYThe day of the week
JANUARY...DECEMBERThe month of the year

Common methods of the Calendar and GregorianCalendar classes

Method Description
set(intYear, intMonth, ...) Sets the values for year, month, day, hour, minute, and second just as they are set in the constructor for the GregorianCalendar class.
set(intField, intValue) Sets the specified field to the supplied value.
setTime(Date) Sets the date and time values based on the supplied Date object.
add(intField, intValue) Adds the supplied value to the specified field.
roll(intField, intValue) Adds the supplied value to the specified field, but doesn't affect other fields.
roll(intField, booleanValue) Increments the value of the specified field by 1 for true values and decrements the value of the field by 1 for false values.
get(intField) Returns the int value of the specified field.
getTime() Returns a Date object.

Code that changes a GregorianCalendar object

GregorianCalendar endDate = new GregorianCalendar(2005, 0, 1); // January 1, 2005
endDate.set(2005, 2, 30);               // March 30, 2005
endDate.set(2005, Calendar.MARCH, 30);  // March 30, 2005
endDate.set(Calendar.MONTH, Calendar.JANUARY); // January 30, 2005
endDate.add(Calendar.MONTH, 5);         // June 30, 2005
endDate.add(Calendar.MONTH, 14);        // August 30, 2006
endDate.roll(Calendar.MONTH, 14);       // October 30, 2006
endDate.roll(Calendar.MONTH, true);     // November 30, 2006
endDate.roll(Calendar.DAY_OF_MONTH, false); // November 29, 2006

Code that accesses fields in a GregorianCalendar object

GregorianCalendar birthday =
	new GregorianCalendar(2005, Calendar.FEBRUARY, 4); // Fri, Feb 4, 2005
int year = birthday.get(Calendar.YEAR);  // year is 2005
int month = birthday.get(Calendar.MONTH);      // month is 1
int day = birthday.get(Calendar.DAY_OF_MONTH); // day is 4
int dayOfWeek = birthday.get(Calendar.DAY_OF_WEEK); // dayOfWeek is 6
int dayOfYear = birthday.get(Calendar.DAY_OF_YEAR); // dayOfYear is 35
import java.util.*;

public class TestCalendar
{

	public static void main(String[] args)
	{
		GregorianCalendar cal = new GregorianCalendar();

		System.out.println(cal.getTime());
		System.out.println("");

		System.out.println("YEAR:		" + cal.get(Calendar.YEAR));
		System.out.println("MONTH:		" + cal.get(Calendar.MONTH));
		System.out.println("DATE:		" + cal.get(Calendar.DATE));

		System.out.println("DAY_OF_YEAR:" + cal.get(Calendar.DAY_OF_YEAR));
		System.out.println("DAY_OF_MONTH:" + cal.get(Calendar.DAY_OF_MONTH));
		System.out.println("DAY_OF_WEEK:" + cal.get(Calendar.DAY_OF_WEEK));
		System.out.println("WEEK_OF_YEAR:" + cal.get(Calendar.WEEK_OF_YEAR));
		System.out.println("WEEK_OF_MONTH:" + cal.get(Calendar.WEEK_OF_MONTH));

		System.out.println("AM_PM:		" + cal.get(Calendar.AM_PM));
		System.out.println("HOUR:		" + cal.get(Calendar.HOUR));
		System.out.println("HOUR_OF_DAY:" + cal.get(Calendar.HOUR_OF_DAY));
		System.out.println("MINUTE:		" + cal.get(Calendar.MINUTE));
	}
}

Previous | GregorianCalendar | Calendar | Date | DateFormat | DateUtils | Invoice | Next