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
Constant | Description |
---|---|
YEAR | The year portion of the calendar date |
MONTH | The month portion of the calendar date |
DATE | The day of the month |
DAY_OF_YEAR | The day number within the year |
DAY_OF_MONTH | The day number within the month |
DAY_OF_WEEK | The day of the week (Sun-1, Mon-2, etc.) |
WEEK_OF_YEAR | The week number within the year |
WEEK_OF_MONTH | The week number within the month |
AM_PM | The indicator for AM or PM (AM-0 and PM-1) |
HOUR | The hour in 12-hour notation |
HOUR_OF_DAY | The hour in 24-hour notation |
MINUTE | The minute within the hour |
SECOND | The second within the minute |
MONDAY...SUNDAY | The day of the week |
JANUARY...DECEMBER | The 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)); } }