An instance of java.util.Date represents a specific instant in time with millisecond precision. java.util.Calendar is an abstract base class for extracting detailed calendar information, such as year, month, date, hour , minute, and second. Subclasses of Calendar can implement specific calendar systems, such as the Gregorian calendar, the lunar calendar, and the Jewish calendar. Currently, java.util.GregorianCalendar for the Gregorian calendar is supported in Java, as shown in Figure 10.3.
You can use new GregorianCalendar() to construct a default GregorianCalendar with the current time and new GregorianCalendar(year, month, date) to construct a GregorianCalendar with the specified year , month , and date . The month parameter is -based, that is, is for January.
The get(int field) method defined in the Calendar class is useful to extract the value for a given time field. The time fields are defined as constants, such as YEAR , MONTH , DATE , HOUR (for the twelve-hour clock), HOUR_OF_DAY (for the twenty-four-hour clock), MINUTE , SECOND , DAY_OF_WEEK (the day number within the current week, with 1 for Sunday), DAY_OF_MONTH (the day in the current month), DAY_OF_YEAR (the day number in the current year with 1 for the first day of the year), WEEK_OF_MONTH (the week number within the current month), and WEEK_OF_YEAR (the week number within the current year). For example, the following code
// Construct a Gregorian calendar for the current date and time java.util.Calendar calendar = new java.util.GregorianCalendar(); System.out.println( "Year\tMonth\tDate\tHour\tHour24\tMinute\tSecond" ); System.out.println(calendar.get(Calendar.YEAR) + "\t" + calendar.get(Calendar.MONTH) + "\t" + calendar.get(Calendar.DATE) + "\t" + calendar.get(Calendar.HOUR) + "\t" + calendar.get(Calendar.HOUR_OF_DAY) + "\t" + calendar.get(Calendar.MINUTE) + "\t" + calendar.get(Calendar.SECOND)); System.out.print( "Day of week: " + calendar.get(Calendar.DAY_OF_WEEK) + "\t" ); System.out.print( "Day of month: " + calendar.get(Calendar.DAY_OF_MONTH) + "\t" ); System.out.println( "Day of year: " + calendar.get(Calendar.DAY_OF_YEAR)); System.out.print( "Week of month: " + calendar.get(Calendar.WEEK_OF_MONTH) + "\t" ); System.out.print( "Week of year: " + calendar.get(Calendar.WEEK_OF_YEAR));
displays the information for the current date and time, as follows :
Year Month Date Hour Hour24 Minute Second 2003 2 9 8 20 17 39 Day of week: 1 Day of month: 9 Day of year: 68 Week of month: 3 Week of year: 11
The set(int field, value) method defined in the Calendar class can be used to set a field. For example, you can use calendar.set(Calendar.DAY_OF_MONTH, 1) to set the calendar to the first day of the month.
The add(field, value) method adds or subtracts the specified amount to a given field. For example, to subtract five days from the current time of the calendar, you must call add(Calendar.DAY_OF_MONTH, -5) .
To obtain the number of days in a month, use calendar.getActualMaximum(Calendar.DAY_OF_MONTH) . For example, if the calendar were for March, this method would return 31 .
You can set a time represented in a Date object for the calendar by invoking calendar.setTime(date) and retrieve the time by invoking calendar.getTime() .