1 16 package org.joda.time.base; 17 18 import java.util.Calendar ; 19 import java.util.GregorianCalendar ; 20 import java.util.Locale ; 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 43 public abstract class AbstractDateTime 44 extends AbstractInstant 45 implements ReadableDateTime { 46 47 50 protected AbstractDateTime() { 51 super(); 52 } 53 54 65 public int get(DateTimeFieldType type) { 66 if (type == null) { 67 throw new IllegalArgumentException ("The DateTimeFieldType must not be null"); 68 } 69 return type.getField(getChronology()).get(getMillis()); 70 } 71 72 78 public int getEra() { 79 return getChronology().era().get(getMillis()); 80 } 81 82 87 public int getCenturyOfEra() { 88 return getChronology().centuryOfEra().get(getMillis()); 89 } 90 91 96 public int getYearOfEra() { 97 return getChronology().yearOfEra().get(getMillis()); 98 } 99 100 105 public int getYearOfCentury() { 106 return getChronology().yearOfCentury().get(getMillis()); 107 } 108 109 114 public int getYear() { 115 return getChronology().year().get(getMillis()); 116 } 117 118 123 public int getWeekyear() { 124 return getChronology().weekyear().get(getMillis()); 125 } 126 127 132 public int getMonthOfYear() { 133 return getChronology().monthOfYear().get(getMillis()); 134 } 135 136 141 public int getWeekOfWeekyear() { 142 return getChronology().weekOfWeekyear().get(getMillis()); 143 } 144 145 150 public int getDayOfYear() { 151 return getChronology().dayOfYear().get(getMillis()); 152 } 153 154 161 public int getDayOfMonth() { 162 return getChronology().dayOfMonth().get(getMillis()); 163 } 164 165 172 public int getDayOfWeek() { 173 return getChronology().dayOfWeek().get(getMillis()); 174 } 175 176 182 public int getHourOfDay() { 183 return getChronology().hourOfDay().get(getMillis()); 184 } 185 186 191 public int getMinuteOfDay() { 192 return getChronology().minuteOfDay().get(getMillis()); 193 } 194 195 200 public int getMinuteOfHour() { 201 return getChronology().minuteOfHour().get(getMillis()); 202 } 203 204 209 public int getSecondOfDay() { 210 return getChronology().secondOfDay().get(getMillis()); 211 } 212 213 218 public int getSecondOfMinute() { 219 return getChronology().secondOfMinute().get(getMillis()); 220 } 221 222 227 public int getMillisOfDay() { 228 return getChronology().millisOfDay().get(getMillis()); 229 } 230 231 236 public int getMillisOfSecond() { 237 return getChronology().millisOfSecond().get(getMillis()); 238 } 239 240 258 public Calendar toCalendar(Locale locale) { 259 if (locale == null) { 260 locale = Locale.getDefault(); 261 } 262 DateTimeZone zone = getZone(); 263 Calendar cal = Calendar.getInstance(zone.toTimeZone(), locale); 264 cal.setTime(toDate()); 265 return cal; 266 } 267 268 282 public GregorianCalendar toGregorianCalendar() { 283 DateTimeZone zone = getZone(); 284 GregorianCalendar cal = new GregorianCalendar (zone.toTimeZone()); 285 cal.setTime(toDate()); 286 return cal; 287 } 288 289 296 public String toString(String pattern) { 297 if (pattern == null) { 298 return toString(); 299 } 300 return DateTimeFormat.forPattern(pattern).print(this); 301 } 302 303 310 public String toString(String pattern, Locale locale) throws IllegalArgumentException { 311 if (pattern == null) { 312 return toString(); 313 } 314 return DateTimeFormat.forPattern(pattern).withLocale(locale).print(this); 315 } 316 317 } 318 | Popular Tags |