Source Code Cross Referenced for Calendar.java in  » Apache-Harmony-Java-SE » java-package » java » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Apache Harmony Java SE » java package » java.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         *  Licensed to the Apache Software Foundation (ASF) under one or more
003:         *  contributor license agreements.  See the NOTICE file distributed with
004:         *  this work for additional information regarding copyright ownership.
005:         *  The ASF licenses this file to You under the Apache License, Version 2.0
006:         *  (the "License"); you may not use this file except in compliance with
007:         *  the License.  You may obtain a copy of the License at
008:         *
009:         *     http://www.apache.org/licenses/LICENSE-2.0
010:         *
011:         *  Unless required by applicable law or agreed to in writing, software
012:         *  distributed under the License is distributed on an "AS IS" BASIS,
013:         *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         *  See the License for the specific language governing permissions and
015:         *  limitations under the License.
016:         */
017:
018:        package java.util;
019:
020:        import java.io.IOException;
021:        import java.io.ObjectInputStream;
022:        import java.io.ObjectOutputStream;
023:        import java.io.ObjectStreamField;
024:        import java.io.Serializable;
025:
026:        /**
027:         * Calendar is an abstract class which provides the conversion between Dates and
028:         * integer calendar fields, such as the month, year or minute. Subclasses of
029:         * this class implement a specific calendar type, such as the gregorian
030:         * calendar.
031:         * 
032:         * @see Date
033:         * @see GregorianCalendar
034:         * @see TimeZone
035:         */
036:        public abstract class Calendar implements  Serializable, Cloneable,
037:                Comparable<Calendar> {
038:
039:            private static final long serialVersionUID = -1807547505821590642L;
040:
041:            /**
042:             * Set to true when the calendar fields have been set from the time, set to
043:             * false when a field is changed and the fields must be recomputed.
044:             */
045:            protected boolean areFieldsSet;
046:
047:            /**
048:             * An integer array of calendar fields.
049:             */
050:            protected int[] fields;
051:
052:            /*
053:             * A boolean array. Each element indicates if the corresponding field has
054:             * been set.
055:             */
056:            protected boolean[] isSet;
057:
058:            /**
059:             * Set to true when the time has been set, set to false when a field is
060:             * changed and the time must be recomputed.
061:             */
062:            protected boolean isTimeSet;
063:
064:            /**
065:             * The time in milliseconds since January 1, 1970.
066:             */
067:            protected long time;
068:
069:            transient int lastTimeFieldSet;
070:
071:            transient int lastDateFieldSet;
072:
073:            private boolean lenient;
074:
075:            private int firstDayOfWeek;
076:
077:            private int minimalDaysInFirstWeek;
078:
079:            private TimeZone zone;
080:
081:            public static final int JANUARY = 0, FEBRUARY = 1, MARCH = 2,
082:                    APRIL = 3, MAY = 4, JUNE = 5, JULY = 6, AUGUST = 7,
083:                    SEPTEMBER = 8, OCTOBER = 9, NOVEMBER = 10, DECEMBER = 11,
084:                    UNDECIMBER = 12,
085:
086:                    SUNDAY = 1, MONDAY = 2, TUESDAY = 3, WEDNESDAY = 4,
087:                    THURSDAY = 5, FRIDAY = 6, SATURDAY = 7;
088:
089:            public static final int ERA = 0, YEAR = 1, MONTH = 2,
090:                    WEEK_OF_YEAR = 3, WEEK_OF_MONTH = 4, DATE = 5,
091:                    DAY_OF_MONTH = 5, DAY_OF_YEAR = 6, DAY_OF_WEEK = 7,
092:                    DAY_OF_WEEK_IN_MONTH = 8,
093:
094:                    AM_PM = 9, HOUR = 10, HOUR_OF_DAY = 11, MINUTE = 12,
095:                    SECOND = 13, MILLISECOND = 14, ZONE_OFFSET = 15,
096:                    DST_OFFSET = 16,
097:
098:                    FIELD_COUNT = 17,
099:
100:                    AM = 0, PM = 1;
101:
102:            private static String[] fieldNames = {
103:                    "ERA=", "YEAR=", "MONTH=", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
104:                    "WEEK_OF_YEAR=", "WEEK_OF_MONTH=", "DAY_OF_MONTH=", "DAY_OF_YEAR=", //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
105:                    "DAY_OF_WEEK=", "DAY_OF_WEEK_IN_MONTH=", "AM_PM=", "HOUR=", //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$ //$NON-NLS-4$
106:                    "HOUR_OF_DAY", "MINUTE=", "SECOND=", "MILLISECOND=", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
107:                    "ZONE_OFFSET=", "DST_OFFSET=" }; //$NON-NLS-1$ //$NON-NLS-2$
108:
109:            /**
110:             * Initializes this Calendar instance using the default TimeZone and Locale.
111:             * 
112:             */
113:            protected Calendar() {
114:                this (TimeZone.getDefault(), Locale.getDefault());
115:            }
116:
117:            Calendar(TimeZone timezone) {
118:                fields = new int[FIELD_COUNT];
119:                isSet = new boolean[FIELD_COUNT];
120:                areFieldsSet = isTimeSet = false;
121:                setLenient(true);
122:                setTimeZone(timezone);
123:            }
124:
125:            /**
126:             * Initializes this Calendar instance using the specified TimeZone and
127:             * Locale.
128:             * 
129:             * @param timezone
130:             *            the timezone
131:             * @param locale
132:             *            the locale
133:             */
134:            protected Calendar(TimeZone timezone, Locale locale) {
135:                this (timezone);
136:                com.ibm.icu.util.Calendar icuCalendar = com.ibm.icu.util.Calendar
137:                        .getInstance(com.ibm.icu.util.SimpleTimeZone
138:                                .getTimeZone(timezone.getID()), locale);
139:                setFirstDayOfWeek(icuCalendar.getFirstDayOfWeek());
140:                setMinimalDaysInFirstWeek(icuCalendar
141:                        .getMinimalDaysInFirstWeek());
142:            }
143:
144:            /**
145:             * Adds the specified amount to a Calendar field.
146:             * 
147:             * @param field
148:             *            the Calendar field to modify
149:             * @param value
150:             *            the amount to add to the field
151:             * 
152:             * @exception IllegalArgumentException
153:             *                when the specified field is DST_OFFSET or ZONE_OFFSET.
154:             */
155:            abstract public void add(int field, int value);
156:
157:            /**
158:             * Answers if the Date specified by this Calendar instance is after the Date
159:             * specified by the parameter. The comparison is not dependent on the time
160:             * zones of the Calendars.
161:             * 
162:             * @param calendar
163:             *            the Calendar instance to compare
164:             * @return true when this Calendar is after calendar, false otherwise
165:             * 
166:             * @exception IllegalArgumentException
167:             *                when the time is not set and the time cannot be computed
168:             *                from the current field values
169:             */
170:            public boolean after(Object calendar) {
171:                if (!(calendar instanceof  Calendar)) {
172:                    return false;
173:                }
174:                return getTimeInMillis() > ((Calendar) calendar)
175:                        .getTimeInMillis();
176:            }
177:
178:            /**
179:             * Answers if the Date specified by this Calendar instance is before the
180:             * Date specified by the parameter. The comparison is not dependent on the
181:             * time zones of the Calendars.
182:             * 
183:             * @param calendar
184:             *            the Calendar instance to compare
185:             * @return true when this Calendar is before calendar, false otherwise
186:             * 
187:             * @exception IllegalArgumentException
188:             *                when the time is not set and the time cannot be computed
189:             *                from the current field values
190:             */
191:            public boolean before(Object calendar) {
192:                if (!(calendar instanceof  Calendar)) {
193:                    return false;
194:                }
195:                return getTimeInMillis() < ((Calendar) calendar)
196:                        .getTimeInMillis();
197:            }
198:
199:            /**
200:             * Clears all of the fields of this Calendar. All fields are initialized to
201:             * zero.
202:             * 
203:             */
204:            public final void clear() {
205:                for (int i = 0; i < FIELD_COUNT; i++) {
206:                    fields[i] = 0;
207:                    isSet[i] = false;
208:                }
209:                areFieldsSet = isTimeSet = false;
210:            }
211:
212:            /**
213:             * Clears the specified field to zero.
214:             * 
215:             * @param field
216:             *            the field to clear
217:             */
218:            public final void clear(int field) {
219:                fields[field] = 0;
220:                isSet[field] = false;
221:                areFieldsSet = isTimeSet = false;
222:            }
223:
224:            /**
225:             * Answers a new Calendar with the same properties.
226:             * 
227:             * @return a shallow copy of this Calendar
228:             * 
229:             * @see java.lang.Cloneable
230:             */
231:            @Override
232:            public Object clone() {
233:                try {
234:                    Calendar clone = (Calendar) super .clone();
235:                    clone.fields = fields.clone();
236:                    clone.isSet = isSet.clone();
237:                    clone.zone = (TimeZone) zone.clone();
238:                    return clone;
239:                } catch (CloneNotSupportedException e) {
240:                    return null;
241:                }
242:            }
243:
244:            /**
245:             * Computes the time from the fields if the time has not already been set.
246:             * Computes the fields from the time if the fields are not already set.
247:             * 
248:             * @exception IllegalArgumentException
249:             *                when the time is not set and the time cannot be computed
250:             *                from the current field values
251:             */
252:            protected void complete() {
253:                if (!isTimeSet) {
254:                    computeTime();
255:                    isTimeSet = true;
256:                }
257:                if (!areFieldsSet) {
258:                    computeFields();
259:                    areFieldsSet = true;
260:                }
261:            }
262:
263:            /**
264:             * Computes the Calendar fields from the time.
265:             * 
266:             */
267:            protected abstract void computeFields();
268:
269:            /**
270:             * Computes the time from the Calendar fields.
271:             * 
272:             * @exception IllegalArgumentException
273:             *                when the time cannot be computed from the current field
274:             *                values
275:             */
276:            protected abstract void computeTime();
277:
278:            /**
279:             * Compares the specified object to this Calendar and answer if they are
280:             * equal. The object must be an instance of Calendar and have the same
281:             * properties.
282:             * 
283:             * @param object
284:             *            the object to compare with this object
285:             * @return true if the specified object is equal to this Calendar, false
286:             *         otherwise
287:             */
288:            @Override
289:            public boolean equals(Object object) {
290:                if (this  == object) {
291:                    return true;
292:                }
293:                if (!(object instanceof  Calendar)) {
294:                    return false;
295:                }
296:                Calendar cal = (Calendar) object;
297:                return getTimeInMillis() == cal.getTimeInMillis()
298:                        && isLenient() == cal.isLenient()
299:                        && getFirstDayOfWeek() == cal.getFirstDayOfWeek()
300:                        && getMinimalDaysInFirstWeek() == cal
301:                                .getMinimalDaysInFirstWeek()
302:                        && getTimeZone().equals(cal.getTimeZone());
303:            }
304:
305:            /**
306:             * Gets the value of the specified field after computing the field values
307:             * from the time if required.
308:             * 
309:             * @param field
310:             *            the field
311:             * @return the value of the specified field
312:             * 
313:             * @exception IllegalArgumentException
314:             *                when the fields are not set, the time is not set, and the
315:             *                time cannot be computed from the current field values
316:             */
317:            public int get(int field) {
318:                complete();
319:                return fields[field];
320:            }
321:
322:            /**
323:             * Gets the maximum value of the specified field for the current date.
324:             * 
325:             * @param field
326:             *            the field
327:             * @return the maximum value of the specified field
328:             */
329:            public int getActualMaximum(int field) {
330:                int value, next;
331:                if (getMaximum(field) == (next = getLeastMaximum(field))) {
332:                    return next;
333:                }
334:                complete();
335:                long orgTime = time;
336:                set(field, next);
337:                do {
338:                    value = next;
339:                    roll(field, true);
340:                    next = get(field);
341:                } while (next > value);
342:                time = orgTime;
343:                areFieldsSet = false;
344:                return value;
345:            }
346:
347:            /**
348:             * Gets the minimum value of the specified field for the current date.
349:             * 
350:             * @param field
351:             *            the field
352:             * @return the minimum value of the specified field
353:             */
354:            public int getActualMinimum(int field) {
355:                int value, next;
356:                if (getMinimum(field) == (next = getGreatestMinimum(field))) {
357:                    return next;
358:                }
359:                complete();
360:                long orgTime = time;
361:                set(field, next);
362:                do {
363:                    value = next;
364:                    roll(field, false);
365:                    next = get(field);
366:                } while (next < value);
367:                time = orgTime;
368:                areFieldsSet = false;
369:                return value;
370:            }
371:
372:            /**
373:             * Gets the list of installed Locales which support Calendar.
374:             * 
375:             * @return an array of Locale
376:             */
377:            public static synchronized Locale[] getAvailableLocales() {
378:                return Locale.getAvailableLocales();
379:            }
380:
381:            /**
382:             * Gets the first day of the week for this Calendar.
383:             * 
384:             * @return a Calendar day of the week
385:             */
386:            public int getFirstDayOfWeek() {
387:                return firstDayOfWeek;
388:            }
389:
390:            /**
391:             * Gets the greatest minimum value of the specified field.
392:             * 
393:             * @param field
394:             *            the field
395:             * @return the greatest minimum value of the specified field
396:             */
397:            abstract public int getGreatestMinimum(int field);
398:
399:            /**
400:             * Constructs a new instance of the Calendar subclass appropriate for the
401:             * default Locale.
402:             * 
403:             * @return a Calendar subclass instance set to the current date and time in
404:             *         the default timezone
405:             */
406:            public static synchronized Calendar getInstance() {
407:                return new GregorianCalendar();
408:            }
409:
410:            /**
411:             * Constructs a new instance of the Calendar subclass appropriate for the
412:             * specified Locale.
413:             * 
414:             * @param locale
415:             *            the locale to use
416:             * @return a Calendar subclass instance set to the current date and time
417:             */
418:            public static synchronized Calendar getInstance(Locale locale) {
419:                return new GregorianCalendar(locale);
420:            }
421:
422:            /**
423:             * Constructs a new instance of the Calendar subclass appropriate for the
424:             * default Locale, using the specified TimeZone.
425:             * 
426:             * @param timezone
427:             *            the timezone to use
428:             * @return a Calendar subclass instance set to the current date and time in
429:             *         the specified timezone
430:             */
431:            public static synchronized Calendar getInstance(TimeZone timezone) {
432:                return new GregorianCalendar(timezone);
433:            }
434:
435:            /**
436:             * Constructs a new instance of the Calendar subclass appropriate for the
437:             * specified Locale.
438:             * 
439:             * @param timezone
440:             *            the timezone to use
441:             * @param locale
442:             *            the locale to use
443:             * @return a Calendar subclass instance set to the current date and time in
444:             *         the specified timezone
445:             */
446:            public static synchronized Calendar getInstance(TimeZone timezone,
447:                    Locale locale) {
448:                return new GregorianCalendar(timezone, locale);
449:            }
450:
451:            /**
452:             * Gets the smallest maximum value of the specified field.
453:             * 
454:             * @param field
455:             *            the field
456:             * @return the smallest maximum value of the specified field
457:             */
458:            abstract public int getLeastMaximum(int field);
459:
460:            /**
461:             * Gets the greatest maximum value of the specified field.
462:             * 
463:             * @param field
464:             *            the field
465:             * @return the greatest maximum value of the specified field
466:             */
467:            abstract public int getMaximum(int field);
468:
469:            /**
470:             * Gets the minimal days in the first week of the year.
471:             * 
472:             * @return the minimal days in the first week of the year
473:             */
474:            public int getMinimalDaysInFirstWeek() {
475:                return minimalDaysInFirstWeek;
476:            }
477:
478:            /**
479:             * Gets the smallest minimum value of the specified field.
480:             * 
481:             * @param field
482:             *            the field
483:             * @return the smallest minimum value of the specified field
484:             */
485:            abstract public int getMinimum(int field);
486:
487:            /**
488:             * Gets the time of this Calendar as a Date object.
489:             * 
490:             * @return a new Date initialized to the time of this Calendar
491:             * 
492:             * @exception IllegalArgumentException
493:             *                when the time is not set and the time cannot be computed
494:             *                from the current field values
495:             */
496:            public final Date getTime() {
497:                return new Date(getTimeInMillis());
498:            }
499:
500:            /**
501:             * Computes the time from the fields if required and answers the time.
502:             * 
503:             * @return the time of this Calendar
504:             * 
505:             * @exception IllegalArgumentException
506:             *                when the time is not set and the time cannot be computed
507:             *                from the current field values
508:             */
509:            public long getTimeInMillis() {
510:                if (!isTimeSet) {
511:                    computeTime();
512:                    isTimeSet = true;
513:                }
514:                return time;
515:            }
516:
517:            /**
518:             * Gets the timezone of this Calendar.
519:             * 
520:             * @return the timezone used by this Calendar
521:             */
522:            public TimeZone getTimeZone() {
523:                return zone;
524:            }
525:
526:            /**
527:             * Answers an integer hash code for the receiver. Objects which are equal
528:             * answer the same value for this method.
529:             * 
530:             * @return the receiver's hash
531:             * 
532:             * @see #equals
533:             */
534:            @Override
535:            public int hashCode() {
536:                return (isLenient() ? 1237 : 1231) + getFirstDayOfWeek()
537:                        + getMinimalDaysInFirstWeek()
538:                        + getTimeZone().hashCode();
539:            }
540:
541:            /**
542:             * Gets the value of the specified field without recomputing.
543:             * 
544:             * @param field
545:             *            the field
546:             * @return the value of the specified field
547:             */
548:            protected final int internalGet(int field) {
549:                return fields[field];
550:            }
551:
552:            /**
553:             * Answers if this Calendar accepts field values which are outside the valid
554:             * range for the field.
555:             * 
556:             * @return true if this Calendar is lenient, false otherwise
557:             */
558:            public boolean isLenient() {
559:                return lenient;
560:            }
561:
562:            /**
563:             * Answers if the specified field is set.
564:             * 
565:             * @param field
566:             *            a calendar field
567:             * @return true if the specified field is set, false otherwise
568:             */
569:            public final boolean isSet(int field) {
570:                return isSet[field];
571:            }
572:
573:            /**
574:             * Adds the specified amount the specified field and wrap the value of the
575:             * field when it goes beyond the maximum or minimum value for the current
576:             * date. Other fields will be adjusted as required to maintain a consistent
577:             * date.
578:             * 
579:             * @param field
580:             *            the field to roll
581:             * @param value
582:             *            the amount to add
583:             */
584:            public void roll(int field, int value) {
585:                boolean increment = value >= 0;
586:                int count = increment ? value : -value;
587:                for (int i = 0; i < count; i++) {
588:                    roll(field, increment);
589:                }
590:            }
591:
592:            /**
593:             * Increment or decrement the specified field and wrap the value of the
594:             * field when it goes beyond the maximum or minimum value for the current
595:             * date. Other fields will be adjusted as required to maintain a consistent
596:             * date.
597:             * 
598:             * @param field
599:             *            the field to roll
600:             * @param increment
601:             *            true to increment the field, false to decrement
602:             */
603:            abstract public void roll(int field, boolean increment);
604:
605:            /**
606:             * Sets a field to the specified value.
607:             * 
608:             * @param field
609:             *            the Calendar field to modify
610:             * @param value
611:             *            the value
612:             */
613:            public void set(int field, int value) {
614:                fields[field] = value;
615:                isSet[field] = true;
616:                areFieldsSet = isTimeSet = false;
617:                if (field > MONTH && field < AM_PM) {
618:                    lastDateFieldSet = field;
619:                }
620:                if (field == HOUR || field == HOUR_OF_DAY) {
621:                    lastTimeFieldSet = field;
622:                }
623:                if (field == AM_PM) {
624:                    lastTimeFieldSet = HOUR;
625:                }
626:            }
627:
628:            /**
629:             * Sets the year, month and day of the month fields.
630:             * 
631:             * @param year
632:             *            the year
633:             * @param month
634:             *            the month
635:             * @param day
636:             *            the day of the month
637:             */
638:            public final void set(int year, int month, int day) {
639:                set(YEAR, year);
640:                set(MONTH, month);
641:                set(DATE, day);
642:            }
643:
644:            /**
645:             * Sets the year, month, day of the month, hour of day and minute fields.
646:             * 
647:             * @param year
648:             *            the year
649:             * @param month
650:             *            the month
651:             * @param day
652:             *            the day of the month
653:             * @param hourOfDay
654:             *            the hour of day
655:             * @param minute
656:             *            the minute
657:             */
658:            public final void set(int year, int month, int day, int hourOfDay,
659:                    int minute) {
660:                set(year, month, day);
661:                set(HOUR_OF_DAY, hourOfDay);
662:                set(MINUTE, minute);
663:            }
664:
665:            /**
666:             * Sets the year, month, day of the month, hour of day, minute and second
667:             * fields.
668:             * 
669:             * @param year
670:             *            the year
671:             * @param month
672:             *            the month
673:             * @param day
674:             *            the day of the month
675:             * @param hourOfDay
676:             *            the hour of day
677:             * @param minute
678:             *            the minute
679:             * @param second
680:             *            the second
681:             */
682:            public final void set(int year, int month, int day, int hourOfDay,
683:                    int minute, int second) {
684:                set(year, month, day, hourOfDay, minute);
685:                set(SECOND, second);
686:            }
687:
688:            /**
689:             * Sets the first day of the week for this Calendar.
690:             * 
691:             * @param value
692:             *            a Calendar day of the week
693:             */
694:            public void setFirstDayOfWeek(int value) {
695:                firstDayOfWeek = value;
696:            }
697:
698:            /**
699:             * Sets this Calendar to accept field values which are outside the valid
700:             * range for the field.
701:             * 
702:             * @param value
703:             *            a boolean value
704:             */
705:            public void setLenient(boolean value) {
706:                lenient = value;
707:            }
708:
709:            /**
710:             * Sets the minimal days in the first week of the year.
711:             * 
712:             * @param value
713:             *            the minimal days in the first week of the year
714:             */
715:            public void setMinimalDaysInFirstWeek(int value) {
716:                minimalDaysInFirstWeek = value;
717:            }
718:
719:            /**
720:             * Sets the time of this Calendar.
721:             * 
722:             * @param date
723:             *            a Date object
724:             */
725:            public final void setTime(Date date) {
726:                setTimeInMillis(date.getTime());
727:            }
728:
729:            /**
730:             * Sets the time of this Calendar.
731:             * 
732:             * @param milliseconds
733:             *            the time as the number of milliseconds since Jan. 1, 1970
734:             */
735:            public void setTimeInMillis(long milliseconds) {
736:                if (!isTimeSet || !areFieldsSet || time != milliseconds) {
737:                    time = milliseconds;
738:                    isTimeSet = true;
739:                    areFieldsSet = false;
740:                    complete();
741:                }
742:            }
743:
744:            /**
745:             * Sets the timezone used by this Calendar.
746:             * 
747:             * @param timezone
748:             *            a TimeZone
749:             */
750:            public void setTimeZone(TimeZone timezone) {
751:                zone = timezone;
752:                areFieldsSet = false;
753:            }
754:
755:            /**
756:             * Answers the string representation of this Calendar.
757:             * 
758:             * @return the string representation of this Calendar
759:             */
760:            @Override
761:            @SuppressWarnings("nls")
762:            public String toString() {
763:                StringBuffer result = new StringBuffer(getClass().getName()
764:                        + "[time=" + (isTimeSet ? String.valueOf(time) : "?")
765:                        + ",areFieldsSet="
766:                        + areFieldsSet
767:                        + // ",areAllFieldsSet=" + areAllFieldsSet +
768:                        ",lenient=" + lenient + ",zone=" + zone
769:                        + ",firstDayOfWeek=" + firstDayOfWeek
770:                        + ",minimalDaysInFirstWeek=" + minimalDaysInFirstWeek);
771:                for (int i = 0; i < FIELD_COUNT; i++) {
772:                    result.append(',');
773:                    result.append(fieldNames[i]);
774:                    result.append('=');
775:                    if (isSet[i]) {
776:                        result.append(fields[i]);
777:                    } else {
778:                        result.append('?');
779:                    }
780:                }
781:                result.append(']');
782:                return result.toString();
783:            }
784:
785:            /**
786:             * Compares the times of the two Calendars, which represent the milliseconds
787:             * from the January 1, 1970 00:00:00.000 GMT (Gregorian).
788:             * 
789:             * @param anotherCalendar
790:             *            another calendar that is compared with.
791:             * @return 0 if the times of the two calendar are equal, -1 if the time of
792:             *         this calendar is before the other one, 1 if the time of this
793:             *         calendar is after the other one.
794:             * @throws NullPointerException
795:             *             if the argument of calendar is null.
796:             * @throws IllegalArgumentException
797:             *             if the argument of the calendar does not include a valid time
798:             *             value.
799:             */
800:            public int compareTo(Calendar anotherCalendar) {
801:                if (null == anotherCalendar) {
802:                    throw new NullPointerException();
803:                }
804:                long timeInMillis = getTimeInMillis();
805:                long anotherTimeInMillis = anotherCalendar.getTimeInMillis();
806:                if (timeInMillis > anotherTimeInMillis) {
807:                    return 1;
808:                }
809:                if (timeInMillis == anotherTimeInMillis) {
810:                    return 0;
811:                }
812:                return -1;
813:            }
814:
815:            @SuppressWarnings("nls")
816:            private static final ObjectStreamField[] serialPersistentFields = {
817:                    new ObjectStreamField("areFieldsSet", Boolean.TYPE),
818:                    new ObjectStreamField("fields", int[].class),
819:                    new ObjectStreamField("firstDayOfWeek", Integer.TYPE),
820:                    new ObjectStreamField("isSet", boolean[].class),
821:                    new ObjectStreamField("isTimeSet", Boolean.TYPE),
822:                    new ObjectStreamField("lenient", Boolean.TYPE),
823:                    new ObjectStreamField("minimalDaysInFirstWeek",
824:                            Integer.TYPE),
825:                    new ObjectStreamField("nextStamp", Integer.TYPE),
826:                    new ObjectStreamField("serialVersionOnStream", Integer.TYPE),
827:                    new ObjectStreamField("time", Long.TYPE),
828:                    new ObjectStreamField("zone", TimeZone.class), };
829:
830:            @SuppressWarnings("nls")
831:            private void writeObject(ObjectOutputStream stream)
832:                    throws IOException {
833:                complete();
834:                ObjectOutputStream.PutField putFields = stream.putFields();
835:                putFields.put("areFieldsSet", areFieldsSet);
836:                putFields.put("fields", this .fields);
837:                putFields.put("firstDayOfWeek", firstDayOfWeek);
838:                putFields.put("isSet", isSet);
839:                putFields.put("isTimeSet", isTimeSet);
840:                putFields.put("lenient", lenient);
841:                putFields.put("minimalDaysInFirstWeek", minimalDaysInFirstWeek);
842:                putFields.put("nextStamp", 2 /* MINIMUM_USER_STAMP */);
843:                putFields.put("serialVersionOnStream", 1);
844:                putFields.put("time", time);
845:                putFields.put("zone", zone);
846:                stream.writeFields();
847:            }
848:
849:            @SuppressWarnings("nls")
850:            private void readObject(ObjectInputStream stream)
851:                    throws IOException, ClassNotFoundException {
852:                ObjectInputStream.GetField readFields = stream.readFields();
853:                areFieldsSet = readFields.get("areFieldsSet", false);
854:                this .fields = (int[]) readFields.get("fields", null);
855:                firstDayOfWeek = readFields.get("firstDayOfWeek",
856:                        Calendar.SUNDAY);
857:                isSet = (boolean[]) readFields.get("isSet", null);
858:                isTimeSet = readFields.get("isTimeSet", false);
859:                lenient = readFields.get("lenient", true);
860:                minimalDaysInFirstWeek = readFields.get(
861:                        "minimalDaysInFirstWeek", 1);
862:                time = readFields.get("time", 0L);
863:                zone = (TimeZone) readFields.get("zone", null);
864:            }
865:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.