Dates and Strings
Dates and times
GregorianCalendar
The GregorianCalendar class manipulates calendar information such as year, month, and day.
The GregorianCalendar class
java.util.GregorianCalendar;
Common constructors of the GregorianCalendar class
Method | Description |
---|---|
GregorianCalendar() | Creates a GregorianCalendar object set to the current date and time. |
GregorianCalendar(year, month, day) | Creates a GregorianCalendar object set to the specified date. |
GregorianCalendar(year, month, day, hour, minute) | Creates a GregorianCalendar object set to the specified date and time. |
GregorianCalendar(year, month, day, hour, minute, second) | Creates a GregorianCalendar object set to the specified date and time. |
A statement that gets the current date
GregorianCalendar now = new GregorianCalendar(); Statements that create dates with literals GregorianCalendar startDate = new GregorianCalendar(2004, 0, 30); GregorianCalendar startTime = new GregorianCalendar(2007, 8, 20, 15, 30);
A statement that creates a date with variables
GregorianCalendar birthDate = new GregorianCalendar(birthYear, birthMonth, birthDay);
Rules for coding date/time values
- Year must be a 4-digit integer.
- Month must be an integer from 0 to 11 with 0 being January and 11 being December.
- Day must be an integer from 1 to 31.
- Hour must be an integer from 0 to 23, with 0 being 12 AM (midnight) and 23 being 11 PM.
- Minute and second must be integers from 0 to 59.