Calendar.h

Go to the documentation of this file.
00001 #ifndef JAVA_UTIL_CALENDAR_H
00002 #define JAVA_UTIL_CALENDAR_H 1
00003 
00004 #ifndef JAVA_UTIL_TIMEZONE_H
00005 #include <java_util/TimeZone.h>
00006 #endif /* JAVA_UTIL_TIMEZONE_H */
00007 #ifndef JAVA_UTIL_DATE_H
00008 #include <java_util/Date.h>
00009 #endif /* JAVA_UTIL_DATE_H */
00010 
00011 namespace java {
00012   namespace util {
00013 
00128     class JAVA_UTIL_SPEC Calendar : public Object {
00129 
00130     public:
00131 
00132       // Data flow in Calendar
00133       // ---------------------
00134 
00135       // The current time is represented in two ways by Calendar: as UTC
00136       // milliseconds from the epoch start (1 January 1970 0:00 UTC), and as local
00137       // fields such as MONTH, HOUR, AM_PM, etc.  It is possible to compute the
00138       // millis from the fields, and vice versa.  The data needed to do this
00139       // conversion is encapsulated by a TimeZone object owned by the Calendar.
00140       // The data provided by the TimeZone object may also be overridden if the
00141       // user sets the ZONE_OFFSET and/or DST_OFFSET fields directly. The class
00142       // keeps track of what information was most recently set by the caller, and
00143       // uses that to compute any other information as needed.
00144 
00145       // If the user sets the fields using set(), the data flow is as follows.
00146       // This is implemented by the Calendar subclass's computeTime() method.
00147       // During this process, certain fields may be ignored.  The disambiguation
00148       // algorithm for resolving which fields to pay attention to is described
00149       // above.
00150 
00151       //   local fields (YEAR, MONTH, DATE, HOUR, MINUTE, etc.)
00152       //           |
00153       //           | Using Calendar-specific algorithm
00154       //           V
00155       //   local standard millis
00156       //           |
00157       //           | Using TimeZone or user-set ZONE_OFFSET / DST_OFFSET
00158       //           V
00159       //   UTC millis (in time data member)
00160 
00161       // If the user sets the UTC millis using setTime(), the data flow is as
00162       // follows.  This is implemented by the Calendar subclass's computeFields()
00163       // method.
00164 
00165       //   UTC millis (in time data member)
00166       //           |
00167       //           | Using TimeZone getOffset()
00168       //           V
00169       //   local standard millis
00170       //           |
00171       //           | Using Calendar-specific algorithm
00172       //           V
00173       //   local fields (YEAR, MONTH, DATE, HOUR, MINUTE, etc.)
00174 
00175       // In general, a round trip from fields, through local and UTC millis, and
00176       // back out to fields is made when necessary.  This is implemented by the
00177       // complete() method.  Resolving a partial set of fields into a UTC millis
00178       // value allows all remaining fields to be generated from that value.  If
00179       // the Calendar is lenient, the fields are also renormalized to standard
00180       // ranges when they are regenerated.
00181 
00182       enum FIELDS { 
00183 
00191         ERA = 0, 
00192 
00197         YEAR, 
00198 
00217         MONTH, 
00218 
00229         WEEK_OF_YEAR, 
00230 
00241         WEEK_OF_MONTH,
00242 
00249         DATE,
00250 
00257         DAY_OF_MONTH = DATE, 
00258 
00263         DAY_OF_YEAR, 
00264 
00278         DAY_OF_WEEK, 
00279 
00302         DAY_OF_WEEK_IN_MONTH,
00303 
00312         AM_PM, 
00313 
00322         HOUR, 
00323 
00330         HOUR_OF_DAY, 
00331 
00337         MINUTE, 
00338 
00344         SECOND, 
00345 
00351         MILLISECOND,
00352 
00357         ZONE_OFFSET, 
00358 
00363         DST_OFFSET, 
00364 
00369         FIELD_COUNT
00370       };
00371     
00372       enum DAYS { 
00373 
00378         SUNDAY = 1, 
00379 
00384         MONDAY, 
00385 
00390         TUESDAY, 
00391 
00396         WEDNESDAY, 
00397 
00402         THURSDAY, 
00403 
00408         FRIDAY, 
00409 
00414         SATURDAY 
00415       };
00416 
00417       enum MONTHS { 
00418 
00423         JANUARY = 0, 
00424 
00429         FEBRUARY, 
00430 
00435         MARCH, 
00436 
00441         APRIL, 
00442 
00447         MAY, 
00448 
00453         UNE,
00454 
00459         ULY, 
00460 
00465         AUGUST, 
00466 
00471         SEPTEMBER, 
00472 
00477         OCTOBER, 
00478 
00483         NOVEMBER, 
00484 
00489         DECEMBER, 
00490 
00496         UNDECIMBER
00497       };
00498 
00499       enum HALF_DAY { 
00500 
00505         AM = 0, 
00506 
00511         PM
00512       };
00513 
00518       static Ref<Calendar> getInstance();
00519 
00525       static Ref<Calendar> getInstance(const TimeZone& zone);
00526 
00531       Date getTime() const;
00532 
00541       void setTime(const Date& date);
00542 
00548       jint get(jint field) const;
00549 
00555       void set(jint field, jint value);
00556 
00566       void set(jint year, jint month, jint date);
00567 
00579       void set(jint year, jint month, jint date, jint hour, jint minute);
00580 
00593       void set(jint year, jint month, jint date, jint hour, jint minute, jint second);
00594 
00598       void clear();
00599 
00604       void clear(jint field);
00605 
00610       jboolean isSet(jint field) const;
00611 
00621       virtual jboolean equals(const Object& obj) const;
00622 
00628       virtual jint hashCode() const;
00629 
00637       virtual jboolean before(const Calendar& when) const;
00638 
00646       virtual jboolean after(const Calendar& when) const;
00647 
00657       virtual void add(jint field, jint amount) = 0;
00658 
00677       virtual void roll(jint field, jboolean up) = 0;
00678 
00692       virtual void roll(jint field, jint amount);
00693 
00698       virtual void setTimeZone(const TimeZone& value);
00699 
00704       virtual TimeZone& getTimeZone() const;
00705 
00715       virtual void setLenient(jboolean lenient);
00716 
00720       virtual jboolean isLenient() const;
00721 
00727       virtual void setFirstDayOfWeek(jint value);
00728 
00734       virtual jint getFirstDayOfWeek() const;
00735 
00744       virtual void setMinimalDaysInFirstWeek(jint value);
00745   
00754       virtual jint getMinimalDaysInFirstWeek() const;
00755 
00762       virtual jint getMinimum(jint field) const = 0;
00763 
00770       virtual jint getMaximum(jint field) const = 0;
00771 
00778       virtual jint getGreatestMinimum(jint field) const = 0;
00779 
00786       virtual jint getLeastMaximum(jint field) const = 0;
00787 
00801       virtual jint getActualMinimum(jint field) const;
00802 
00818       virtual jint getActualMaximum(jint field) const;
00819 
00824       virtual jboolean inDaylightTime() const = 0;
00825 
00834       virtual String toString() const;
00835 
00836     protected:
00837 
00838       // Internal notes:
00839       // Calendar contains two kinds of time representations: current "time" in
00840       // milliseconds, and a set of time "fields" representing the current time.
00841       // The two representations are usually in sync, but can get out of sync
00842       // as follows.
00843       // 1. Initially, no fields are set, and the time is invalid.
00844       // 2. If the time is set, all fields are computed and in sync.
00845       // 3. If a single field is set, the time is invalid.
00846       // Recomputation of the time and fields happens when the object needs
00847       // to return a result to the user, or use a result for a computation.
00848 
00855       jint fields[FIELD_COUNT];
00856 
00857       enum STAMPS {
00858         UNSET, 
00859         INTERNALLY_SET, 
00860         MINIMUM_USER_STAMP
00861       };
00862 
00868       jint stamp[FIELD_COUNT];
00869 
00876       jlong time;
00877 
00884       jboolean isTimeSet;
00885 
00893       jboolean areFieldsSet;
00894 
00902       jboolean areAllFieldsSet;
00903 
00909       virtual void computeTime() = 0;
00910 
00922       virtual void computeFields() = 0;
00923 
00928       virtual jlong getTimeInMillis() const;
00929 
00934       virtual void setTimeInMillis(jlong millis);
00935 
00942       jint internalGet(jint field) const;
00943 
00949       void internalSet(jint field, jint value);
00950 
00954       void complete();
00955 
00956     protected:
00957 
00962       Calendar();
00963 
00968       Calendar(const TimeZone& zone);
00969 
00970     private:
00971 
00978       jboolean lenient;
00979 
00985       Ref<TimeZone> zone;
00986 
00992       jint firstDayOfWeek;
00993 
00999       jint minimalDaysInFirstWeek;
01000 
01008       jint nextStamp;
01009 
01015       virtual void updateTime();
01016 
01017       REGISTER_MEMBERS;
01018 
01019     };
01020 
01021   } // namespace util
01022 } // namespace java
01023 
01024 #endif /* JAVA_UTIL_CALENDAR_H */

Generated on Fri May 16 11:54:42 2008 for CrossPlatformJavaLikeC++API by  doxygen 1.5.3