How to use the GregorianCalendar class to set dates and times.

The associated package:

  • java.util.GregorianCalenar;

Common constructors of the GregorianCalenar classes:

Method Description
GregorianCalenar() Creates a GregorianCalenar object set to the current date and time.
GregorianCalenar(year, month, day) Creates a GregorianCalenar object to the specified date.
GregorianCalenar(year, month, day,hour, minute) Creates a GregorianCalenar object to the specified date and time.
GregorianCalenar(year, month, day, hour, minute, second) Creates a GregorianCalenar object to the specified date and time.

Descriptions:

  • Year must be four-digit integer.
  • Month must be an integer from 0 to 11, 0 = January.
  • Day must be an integer from 1 to 31.
  • Hour must be an integer from 0 to 23, with 0 being 12 AM (Midnight).
  • Minute and second must be integers from 0 to 59.

Example 1: A statement that gets the current date

GregorianCalenar now = new GregorianCalenar();

Example 2: Statements that create dates with literals

GregorianCalenar startDate = new GregorianCalenar(2004, 0, 30);
GregorianCalenar endDate = new GregorianCalenar(2007, 6, 20, 15, 30);