Java in a Nutshell, 5th Edition [Electronic resources]

نسخه متنی -صفحه : 1191/ 690
نمايش فراداده

Calendarjava.util

Java 1.1cloneable serializable comparable

This abstract class defines methods that perform date and time arithmetic. It also includes methods that convert dates and times to and from the machine-usable millisecond format used by the Date class and units such as minutes, hours, days, weeks, months, and years that are more useful to humans. As an abstract class, Calendar cannot be directly instantiated. Instead, it provides static getInstance( ) methods that return instances of a Calendar subclass suitable for use in a specified or default locale with a specified or default time zone. See also Date, DateFormat, and TimeZone.

Calendar defines a number of useful constants. Some of these are values that represent days of the week and months of the year. Other constants, such as HOUR and DAY_OF_WEEK, represent various fields of date and time information. These field constants are passed to a number of Calendar methods, such as get( ) and set( ), in order to indicate what particular date or time field is desired.

setTime( ) and the various set( ) methods set the date represented by a Calendar object. The add( ) method adds (or subtracts) values to a calendar field, incrementing the next larger field when the field being set rolls over. roll( ) does the same, without modifying anything but the specified field. before( ) and after( ) compare two Calendar objects. Many of the methods of the Calendar class are replacements for methods of Date that have been deprecated as of Java 1.1. While the Calendar class converts a time value to its various hour, day, month, and other fields, it is not intended to present those fields in a form suitable for display to the end user. That function is performed by the java.text.DateFormat class, which handles internationalization issues.

Calendar implements Comparable in Java 5.0, but not in earlier releases.

Figure 16-9. java.util.Calendar

public abstract class

Calendar implements Serializable, Cloneable, Comparable<Calendar> { // Protected Constructors protected

Calendar ( ); protected

Calendar (TimeZone

zone , Locale

aLocale ); // Public Constants public static final int

AM ; =0 public static final int

AM_PM ; =9 public static final int

APRIL ; =3 public static final int

AUGUST ; =7 public static final int

DATE ; =5 public static final int

DAY_OF_MONTH ; =5 public static final int

DAY_OF_WEEK ; =7 public static final int

DAY_OF_WEEK_IN_MONTH ; =8 public static final int

DAY_OF_YEAR ; =6 public static final int

DECEMBER ; =11 public static final int

DST_OFFSET ; =16 public static final int

ERA ; =0 public static final int

FEBRUARY ; =1 public static final int

FIELD_COUNT ; =17 public static final int

FRIDAY ; =6 public static final int

HOUR ; =10 public static final int

HOUR_OF_DAY ; =11 public static final int

JANUARY ; =0 public static final int

JULY ; =6 public static final int

JUNE ; =5 public static final int

MARCH ; =2 public static final int

MAY ; =4 public static final int

MILLISECOND ; =14 public static final int

MINUTE ; =12 public static final int

MONDAY ; =2 public static final int

MONTH ; =2 public static final int

NOVEMBER ; =10 public static final int

OCTOBER ; =9 public static final int

PM ; =1 public static final int

SATURDAY ; =7 public static final int

SECOND ; =13 public static final int

SEPTEMBER ; =8 public static final int

SUNDAY ; =1 public static final int

THURSDAY ; =5 public static final int

TUESDAY ; =3 public static final int

UNDECIMBER ; =12 public static final int

WEDNESDAY ; =4 public static final int

WEEK_OF_MONTH ; =4 public static final int

WEEK_OF_YEAR ; =3 public static final int

YEAR ; =1 public static final int

ZONE_OFFSET ; =15 // Public Class Methods public static Locale[ ]

getAvailableLocales ( ); synchronized public static Calendar

getInstance ( ); public static Calendar

getInstance (Locale

aLocale ); public static Calendar

getInstance (TimeZone

zone ); public static Calendar

getInstance (TimeZone

zone , Locale

aLocale ); // Public Instance Methods public abstract void

add (int

field , int

amount ); public boolean

after (Object

when ); public boolean

before (Object

when ); public final void

clear ( ); public final void

clear (int

field ); public int

get (int

field );

1.2 public int

getActualMaximum (int

field );

1.2 public int

getActualMinimum (int

field ); public int

getFirstDayOfWeek ( ); public abstract int

getGreatestMinimum (int

field ); public abstract int

getLeastMaximum (int

field ); public abstract int

getMaximum (int

field ); public int

getMinimalDaysInFirstWeek ( ); public abstract int

getMinimum (int

field ); public final Date

getTime ( ); public long

getTimeInMillis ( ); public TimeZone

getTimeZone ( ); public boolean

isLenient ( ); public final boolean

isSet (int

field );

1.2 public void

roll (int

field , int

amount ); public abstract void

roll (int

field , boolean

up ); public void

set (int

field , int

value ); public final void

set (int

year , int

month , int

date ); public final void

set (int

year , int

month , int

date , int

hourOfDay , int

minute ); public final void

set (int

year , int

month , int

date , int

hourOfDay , int

minute , int

second ); public void

setFirstDayOfWeek (int

value ); public void

setLenient (boolean

lenient ); public void

setMinimalDaysInFirstWeek (int

value ); public final void

setTime (Date

date ); public void

setTimeInMillis (long

millis ); public void

setTimeZone (TimeZone

value ); // Methods Implementing Comparable

5.0 public int

compareTo (Calendar

anotherCalendar ); // Public Methods Overriding Object public Object

clone ( ); public boolean

equals (Object

obj );

1.2 public int

hashCode ( ); public String

toString ( ); // Protected Instance Methods protected void

complete ( ); protected abstract void

computeFields ( ); protected abstract void

computeTime ( ); protected final int

internalGet (int

field ); // Protected Instance Fields protected boolean

areFieldsSet ; protected int[ ]

fields ; protected boolean[ ]

isSet ; protected boolean

isTimeSet ; protected long

time ; }

Subclasses

GregorianCalendar

Passed To

java.text.DateFormat.setCalendar( ), javax.xml.datatype.Duration.{addTo( ), getTimeInMillis( ), normalizeWith( )}

Returned By

java.text.DateFormat.getCalendar( )

Type Of

java.text.DateFormat.calendar