KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > joda > time > base > AbstractDateTime


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.base;
17
18 import java.util.Calendar JavaDoc;
19 import java.util.GregorianCalendar JavaDoc;
20 import java.util.Locale JavaDoc;
21
22 import org.joda.time.DateTimeFieldType;
23 import org.joda.time.DateTimeZone;
24 import org.joda.time.ReadableDateTime;
25 import org.joda.time.format.DateTimeFormat;
26
27 /**
28  * AbstractDateTime provides the common behaviour for datetime classes.
29  * <p>
30  * This class should generally not be used directly by API users.
31  * The {@link ReadableDateTime} interface should be used when different
32  * kinds of date/time objects are to be referenced.
33  * <p>
34  * Whenever you want to implement <code>ReadableDateTime</code> you should
35  * extend this class.
36  * <p>
37  * AbstractDateTime subclasses may be mutable and not thread-safe.
38  *
39  * @author Brian S O'Neill
40  * @author Stephen Colebourne
41  * @since 1.0
42  */

43 public abstract class AbstractDateTime
44         extends AbstractInstant
45         implements ReadableDateTime {
46
47     /**
48      * Constructor.
49      */

50     protected AbstractDateTime() {
51         super();
52     }
53
54     //-----------------------------------------------------------------------
55
/**
56      * Get the value of one of the fields of a datetime.
57      * <p>
58      * This method uses the chronology of the datetime to obtain the value.
59      * It is essentially a generic way of calling one of the get methods.
60      *
61      * @param type a field type, usually obtained from DateTimeFieldType
62      * @return the value of that field
63      * @throws IllegalArgumentException if the field type is null
64      */

65     public int get(DateTimeFieldType type) {
66         if (type == null) {
67             throw new IllegalArgumentException JavaDoc("The DateTimeFieldType must not be null");
68         }
69         return type.getField(getChronology()).get(getMillis());
70     }
71
72     //-----------------------------------------------------------------------
73
/**
74      * Get the era field value.
75      *
76      * @return the era
77      */

78     public int getEra() {
79         return getChronology().era().get(getMillis());
80     }
81
82     /**
83      * Get the year of era field value.
84      *
85      * @return the year of era
86      */

87     public int getCenturyOfEra() {
88         return getChronology().centuryOfEra().get(getMillis());
89     }
90
91     /**
92      * Get the year of era field value.
93      *
94      * @return the year of era
95      */

96     public int getYearOfEra() {
97         return getChronology().yearOfEra().get(getMillis());
98     }
99
100     /**
101      * Get the year of century field value.
102      *
103      * @return the year of century
104      */

105     public int getYearOfCentury() {
106         return getChronology().yearOfCentury().get(getMillis());
107     }
108
109     /**
110      * Get the year field value.
111      *
112      * @return the year
113      */

114     public int getYear() {
115         return getChronology().year().get(getMillis());
116     }
117
118     /**
119      * Get the weekyear field value.
120      *
121      * @return the year of a week based year
122      */

123     public int getWeekyear() {
124         return getChronology().weekyear().get(getMillis());
125     }
126
127     /**
128      * Get the month of year field value.
129      *
130      * @return the month of year
131      */

132     public int getMonthOfYear() {
133         return getChronology().monthOfYear().get(getMillis());
134     }
135
136     /**
137      * Get the week of weekyear field value.
138      *
139      * @return the week of a week based year
140      */

141     public int getWeekOfWeekyear() {
142         return getChronology().weekOfWeekyear().get(getMillis());
143     }
144
145     /**
146      * Get the day of year field value.
147      *
148      * @return the day of year
149      */

150     public int getDayOfYear() {
151         return getChronology().dayOfYear().get(getMillis());
152     }
153
154     /**
155      * Get the day of month field value.
156      * <p>
157      * The values for the day of month are defined in {@link org.joda.time.DateTimeConstants}.
158      *
159      * @return the day of month
160      */

161     public int getDayOfMonth() {
162         return getChronology().dayOfMonth().get(getMillis());
163     }
164
165     /**
166      * Get the day of week field value.
167      * <p>
168      * The values for the day of week are defined in {@link org.joda.time.DateTimeConstants}.
169      *
170      * @return the day of week
171      */

172     public int getDayOfWeek() {
173         return getChronology().dayOfWeek().get(getMillis());
174     }
175
176     //-----------------------------------------------------------------------
177
/**
178      * Get the hour of day field value.
179      *
180      * @return the hour of day
181      */

182     public int getHourOfDay() {
183         return getChronology().hourOfDay().get(getMillis());
184     }
185
186     /**
187      * Get the minute of day field value.
188      *
189      * @return the minute of day
190      */

191     public int getMinuteOfDay() {
192         return getChronology().minuteOfDay().get(getMillis());
193     }
194
195     /**
196      * Get the minute of hour field value.
197      *
198      * @return the minute of hour
199      */

200     public int getMinuteOfHour() {
201         return getChronology().minuteOfHour().get(getMillis());
202     }
203
204     /**
205      * Get the second of day field value.
206      *
207      * @return the second of day
208      */

209     public int getSecondOfDay() {
210         return getChronology().secondOfDay().get(getMillis());
211     }
212
213     /**
214      * Get the second of minute field value.
215      *
216      * @return the second of minute
217      */

218     public int getSecondOfMinute() {
219         return getChronology().secondOfMinute().get(getMillis());
220     }
221
222     /**
223      * Get the millis of day field value.
224      *
225      * @return the millis of day
226      */

227     public int getMillisOfDay() {
228         return getChronology().millisOfDay().get(getMillis());
229     }
230
231     /**
232      * Get the millis of second field value.
233      *
234      * @return the millis of second
235      */

236     public int getMillisOfSecond() {
237         return getChronology().millisOfSecond().get(getMillis());
238     }
239
240     //-----------------------------------------------------------------------
241
/**
242      * Get the date time as a <code>java.util.Calendar</code>, assigning
243      * exactly the same millisecond instant.
244      * The locale is passed in, enabling Calendar to select the correct
245      * localized subclass.
246      * <p>
247      * The JDK and Joda-Time both have time zone implementations and these
248      * differ in accuracy. Joda-Time's implementation is generally more up to
249      * date and thus more accurate - for example JDK1.3 has no historical data.
250      * The effect of this is that the field values of the <code>Calendar</code>
251      * may differ from those of this object, even though the milliseond value
252      * is the same. Most of the time this just means that the JDK field values
253      * are wrong, as our time zone information is more up to date.
254      *
255      * @param locale the locale to get the Calendar for, or default if null
256      * @return a localized Calendar initialised with this datetime
257      */

258     public Calendar JavaDoc toCalendar(Locale JavaDoc locale) {
259         if (locale == null) {
260             locale = Locale.getDefault();
261         }
262         DateTimeZone zone = getZone();
263         Calendar JavaDoc cal = Calendar.getInstance(zone.toTimeZone(), locale);
264         cal.setTime(toDate());
265         return cal;
266     }
267
268     /**
269      * Get the date time as a <code>java.util.GregorianCalendar</code>,
270      * assigning exactly the same millisecond instant.
271      * <p>
272      * The JDK and Joda-Time both have time zone implementations and these
273      * differ in accuracy. Joda-Time's implementation is generally more up to
274      * date and thus more accurate - for example JDK1.3 has no historical data.
275      * The effect of this is that the field values of the <code>Calendar</code>
276      * may differ from those of this object, even though the milliseond value
277      * is the same. Most of the time this just means that the JDK field values
278      * are wrong, as our time zone information is more up to date.
279      *
280      * @return a GregorianCalendar initialised with this datetime
281      */

282     public GregorianCalendar JavaDoc toGregorianCalendar() {
283         DateTimeZone zone = getZone();
284         GregorianCalendar JavaDoc cal = new GregorianCalendar JavaDoc(zone.toTimeZone());
285         cal.setTime(toDate());
286         return cal;
287     }
288
289     //-----------------------------------------------------------------------
290
/**
291      * Output the instant using the specified format pattern.
292      *
293      * @param pattern the pattern specification, null means use <code>toString</code>
294      * @see org.joda.time.format.DateTimeFormat
295      */

296     public String JavaDoc toString(String JavaDoc pattern) {
297         if (pattern == null) {
298             return toString();
299         }
300         return DateTimeFormat.forPattern(pattern).print(this);
301     }
302
303     /**
304      * Output the instant using the specified format pattern.
305      *
306      * @param pattern the pattern specification, null means use <code>toString</code>
307      * @param locale Locale to use, null means default
308      * @see org.joda.time.format.DateTimeFormat
309      */

310     public String JavaDoc toString(String JavaDoc pattern, Locale JavaDoc locale) throws IllegalArgumentException JavaDoc {
311         if (pattern == null) {
312             return toString();
313         }
314         return DateTimeFormat.forPattern(pattern).withLocale(locale).print(this);
315     }
316
317 }
318
Popular Tags