View Javadoc

1   /*
2    *  Copyright 2001-2005 Stephen Colebourne
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  package org.joda.time.chrono.gj;
17  
18  import org.joda.time.DateTimeFieldType;
19  import org.joda.time.DurationField;
20  
21  /**
22   * 
23   * @author Brian S O'Neill
24   */
25  class TestGJMonthOfYearField extends TestGJDateTimeField {
26      public TestGJMonthOfYearField(TestGJChronology chrono) {
27          super(DateTimeFieldType.monthOfYear(), chrono.millisPerMonth(), chrono);
28      }
29  
30      public int get(long millis) {
31          return iChronology.gjFromMillis(millis)[1];
32      }
33  
34      public long set(long millis, int value) {
35          long timeOnlyMillis = iChronology.getTimeOnlyMillis(millis);
36          int[] ymd = iChronology.gjFromMillis(millis);
37          // First set to start of month...
38          millis = iChronology.millisFromGJ(ymd[0], value, 1);
39          // ...and use dayOfMonth field to check range.
40          int maxDay = iChronology.dayOfMonth().getMaximumValue(millis);
41          if (ymd[2] > maxDay) {
42              ymd[2] = maxDay;
43          }
44          return timeOnlyMillis + iChronology.millisFromGJ(ymd[0], value, ymd[2]);
45      }
46  
47      public long add(long millis, long value) {
48          int newYear = iChronology.year().get(millis)
49              + (int)TestGJChronology.div(value, 12);
50          int newMonth = get(millis) + (int)TestGJChronology.mod(value, 12);
51          if (newMonth > 12) {
52              newYear++;
53              newMonth -= 12;
54          }
55          int newDay = iChronology.dayOfMonth().get(millis);
56          millis = iChronology.getTimeOnlyMillis(millis) 
57              + iChronology.millisFromGJ(newYear, newMonth, newDay);
58          while (get(millis) != newMonth) {
59              millis = iChronology.dayOfYear().add(millis, -1);
60          }
61          return millis;
62      }
63  
64      public boolean isLeap(long millis) {
65          int[] ymd = iChronology.gjFromMillis(millis);
66          return ymd[1] == 2 && iChronology.isLeapYear(ymd[0]);
67      }
68  
69      public int getLeapAmount(long millis) {
70          return isLeap(millis) ? 1 : 0;
71      }
72  
73      public DurationField getLeapDurationField() {
74          return iChronology.days();
75      }
76  
77      public DurationField getRangeDurationField() {
78          return iChronology.years();
79      }
80  
81      public int getMinimumValue() {
82          return 1;
83      }
84  
85      public int getMaximumValue() {
86          return 12;
87      }
88  
89      public long roundFloor(long millis) {
90          int[] ymd = iChronology.gjFromMillis(millis);
91          return iChronology.millisFromGJ(ymd[0], ymd[1], 1);
92      }
93  }