| Method from org.apache.commons.lang.time.FastDateFormat Detail: |
protected StringBuffer applyRules(Calendar calendar,
StringBuffer buf) {
Rule[] rules = mRules;
int len = mRules.length;
for (int i = 0; i < len; i++) {
rules[i].appendTo(buf, calendar);
}
return buf;
}
|
public boolean equals(Object obj) {
if (obj instanceof FastDateFormat == false) {
return false;
}
FastDateFormat other = (FastDateFormat) obj;
if (
(mPattern == other.mPattern || mPattern.equals(other.mPattern)) &&
(mTimeZone == other.mTimeZone || mTimeZone.equals(other.mTimeZone)) &&
(mLocale == other.mLocale || mLocale.equals(other.mLocale)) &&
(mTimeZoneForced == other.mTimeZoneForced) &&
(mLocaleForced == other.mLocaleForced)
) {
return true;
}
return false;
}
|
public String format(long millis) {
return format(new Date(millis));
}
|
public String format(Date date) {
Calendar c = new GregorianCalendar(mTimeZone);
c.setTime(date);
return applyRules(c, new StringBuffer(mMaxLengthEstimate)).toString();
}
|
public String format(Calendar calendar) {
return format(calendar, new StringBuffer(mMaxLengthEstimate)).toString();
}
|
public StringBuffer format(long millis,
StringBuffer buf) {
return format(new Date(millis), buf);
}
|
public StringBuffer format(Date date,
StringBuffer buf) {
Calendar c = new GregorianCalendar(mTimeZone);
c.setTime(date);
return applyRules(c, buf);
}
|
public StringBuffer format(Calendar calendar,
StringBuffer buf) {
if (mTimeZoneForced) {
calendar = (Calendar) calendar.clone();
calendar.setTimeZone(mTimeZone);
}
return applyRules(calendar, buf);
}
|
public StringBuffer format(Object obj,
StringBuffer toAppendTo,
FieldPosition pos) {
if (obj instanceof Date) {
return format((Date) obj, toAppendTo);
} else if (obj instanceof Calendar) {
return format((Calendar) obj, toAppendTo);
} else if (obj instanceof Long) {
return format(((Long) obj).longValue(), toAppendTo);
} else {
throw new IllegalArgumentException("Unknown class: " +
(obj == null ? "< null >" : obj.getClass().getName()));
}
}
Formats a Date, Calendar or
Long (milliseconds) object.
|
public static FastDateFormat getDateInstance(int style) {
return getDateInstance(style, null, null);
}
|
public static FastDateFormat getDateInstance(int style,
Locale locale) {
return getDateInstance(style, null, locale);
}
|
public static FastDateFormat getDateInstance(int style,
TimeZone timeZone) {
return getDateInstance(style, timeZone, null);
}
|
public static synchronized FastDateFormat getDateInstance(int style,
TimeZone timeZone,
Locale locale) {
Object key = new Integer(style);
if (timeZone != null) {
key = new Pair(key, timeZone);
}
if (locale == null) {
locale = Locale.getDefault();
}
key = new Pair(key, locale);
FastDateFormat format = (FastDateFormat) cDateInstanceCache.get(key);
if (format == null) {
try {
SimpleDateFormat formatter = (SimpleDateFormat) DateFormat.getDateInstance(style, locale);
String pattern = formatter.toPattern();
format = getInstance(pattern, timeZone, locale);
cDateInstanceCache.put(key, format);
} catch (ClassCastException ex) {
throw new IllegalArgumentException("No date pattern for locale: " + locale);
}
}
return format;
}
Gets a date formatter instance using the specified style, time
zone and locale.
|
public static FastDateFormat getDateTimeInstance(int dateStyle,
int timeStyle) {
return getDateTimeInstance(dateStyle, timeStyle, null, null);
}
|
public static FastDateFormat getDateTimeInstance(int dateStyle,
int timeStyle,
Locale locale) {
return getDateTimeInstance(dateStyle, timeStyle, null, locale);
}
|
public static FastDateFormat getDateTimeInstance(int dateStyle,
int timeStyle,
TimeZone timeZone) {
return getDateTimeInstance(dateStyle, timeStyle, timeZone, null);
}
|
public static synchronized FastDateFormat getDateTimeInstance(int dateStyle,
int timeStyle,
TimeZone timeZone,
Locale locale) {
Object key = new Pair(new Integer(dateStyle), new Integer(timeStyle));
if (timeZone != null) {
key = new Pair(key, timeZone);
}
if (locale == null) {
locale = Locale.getDefault();
}
key = new Pair(key, locale);
FastDateFormat format = (FastDateFormat) cDateTimeInstanceCache.get(key);
if (format == null) {
try {
SimpleDateFormat formatter = (SimpleDateFormat) DateFormat.getDateTimeInstance(dateStyle, timeStyle,
locale);
String pattern = formatter.toPattern();
format = getInstance(pattern, timeZone, locale);
cDateTimeInstanceCache.put(key, format);
} catch (ClassCastException ex) {
throw new IllegalArgumentException("No date time pattern for locale: " + locale);
}
}
return format;
}
Gets a date/time formatter instance using the specified style,
time zone and locale.
|
public static FastDateFormat getInstance() {
//-----------------------------------------------------------------------
return getInstance(getDefaultPattern(), null, null);
}
|
public static FastDateFormat getInstance(String pattern) {
return getInstance(pattern, null, null);
}
|
public static FastDateFormat getInstance(String pattern,
TimeZone timeZone) {
return getInstance(pattern, timeZone, null);
}
|
public static FastDateFormat getInstance(String pattern,
Locale locale) {
return getInstance(pattern, null, locale);
}
|
public static synchronized FastDateFormat getInstance(String pattern,
TimeZone timeZone,
Locale locale) {
FastDateFormat emptyFormat = new FastDateFormat(pattern, timeZone, locale);
FastDateFormat format = (FastDateFormat) cInstanceCache.get(emptyFormat);
if (format == null) {
format = emptyFormat;
format.init(); // convert shell format into usable one
cInstanceCache.put(format, format); // this is OK!
}
return format;
}
Gets a formatter instance using the specified pattern, time zone
and locale.
|
public Locale getLocale() {
return mLocale;
}
|
public int getMaxLengthEstimate() {
return mMaxLengthEstimate;
}
Gets an estimate for the maximum string length that the
formatter will produce.
The actual formatted length will almost always be less than or
equal to this amount.
|
public String getPattern() {
return mPattern;
}
|
public static FastDateFormat getTimeInstance(int style) {
return getTimeInstance(style, null, null);
}
|
public static FastDateFormat getTimeInstance(int style,
Locale locale) {
return getTimeInstance(style, null, locale);
}
|
public static FastDateFormat getTimeInstance(int style,
TimeZone timeZone) {
return getTimeInstance(style, timeZone, null);
}
|
public static synchronized FastDateFormat getTimeInstance(int style,
TimeZone timeZone,
Locale locale) {
Object key = new Integer(style);
if (timeZone != null) {
key = new Pair(key, timeZone);
}
if (locale != null) {
key = new Pair(key, locale);
}
FastDateFormat format = (FastDateFormat) cTimeInstanceCache.get(key);
if (format == null) {
if (locale == null) {
locale = Locale.getDefault();
}
try {
SimpleDateFormat formatter = (SimpleDateFormat) DateFormat.getTimeInstance(style, locale);
String pattern = formatter.toPattern();
format = getInstance(pattern, timeZone, locale);
cTimeInstanceCache.put(key, format);
} catch (ClassCastException ex) {
throw new IllegalArgumentException("No date pattern for locale: " + locale);
}
}
return format;
}
Gets a time formatter instance using the specified style, time
zone and locale.
|
public TimeZone getTimeZone() {
return mTimeZone;
}
Gets the time zone used by this formatter.
This zone is always used for Date formatting.
If a Calendar is passed in to be formatted, the
time zone on that may be used depending on
#getTimeZoneOverridesCalendar() .
|
static synchronized String getTimeZoneDisplay(TimeZone tz,
boolean daylight,
int style,
Locale locale) {
Object key = new TimeZoneDisplayKey(tz, daylight, style, locale);
String value = (String) cTimeZoneDisplayCache.get(key);
if (value == null) {
// This is a very slow call, so cache the results.
value = tz.getDisplayName(daylight, style, locale);
cTimeZoneDisplayCache.put(key, value);
}
return value;
}
Gets the time zone display name, using a cache for performance.
|
public boolean getTimeZoneOverridesCalendar() {
return mTimeZoneForced;
}
|
public int hashCode() {
int total = 0;
total += mPattern.hashCode();
total += mTimeZone.hashCode();
total += (mTimeZoneForced ? 1 : 0);
total += mLocale.hashCode();
total += (mLocaleForced ? 1 : 0);
return total;
}
|
protected void init() {
List rulesList = parsePattern();
mRules = (Rule[]) rulesList.toArray(new Rule[rulesList.size()]);
int len = 0;
for (int i=mRules.length; --i >= 0; ) {
len += mRules[i].estimateLength();
}
mMaxLengthEstimate = len;
}
|
public Object parseObject(String source,
ParsePosition pos) {
pos.setIndex(0);
pos.setErrorIndex(0);
return null;
}
|
protected List parsePattern() {
DateFormatSymbols symbols = new DateFormatSymbols(mLocale);
List rules = new ArrayList();
String[] ERAs = symbols.getEras();
String[] months = symbols.getMonths();
String[] shortMonths = symbols.getShortMonths();
String[] weekdays = symbols.getWeekdays();
String[] shortWeekdays = symbols.getShortWeekdays();
String[] AmPmStrings = symbols.getAmPmStrings();
int length = mPattern.length();
int[] indexRef = new int[1];
for (int i = 0; i < length; i++) {
indexRef[0] = i;
String token = parseToken(mPattern, indexRef);
i = indexRef[0];
int tokenLen = token.length();
if (tokenLen == 0) {
break;
}
Rule rule;
char c = token.charAt(0);
switch (c) {
case 'G": // era designator (text)
rule = new TextField(Calendar.ERA, ERAs);
break;
case 'y": // year (number)
if (tokenLen >= 4) {
rule = selectNumberRule(Calendar.YEAR, tokenLen);
} else {
rule = TwoDigitYearField.INSTANCE;
}
break;
case 'M": // month in year (text and number)
if (tokenLen >= 4) {
rule = new TextField(Calendar.MONTH, months);
} else if (tokenLen == 3) {
rule = new TextField(Calendar.MONTH, shortMonths);
} else if (tokenLen == 2) {
rule = TwoDigitMonthField.INSTANCE;
} else {
rule = UnpaddedMonthField.INSTANCE;
}
break;
case 'd": // day in month (number)
rule = selectNumberRule(Calendar.DAY_OF_MONTH, tokenLen);
break;
case 'h": // hour in am/pm (number, 1..12)
rule = new TwelveHourField(selectNumberRule(Calendar.HOUR, tokenLen));
break;
case 'H": // hour in day (number, 0..23)
rule = selectNumberRule(Calendar.HOUR_OF_DAY, tokenLen);
break;
case 'm": // minute in hour (number)
rule = selectNumberRule(Calendar.MINUTE, tokenLen);
break;
case 's": // second in minute (number)
rule = selectNumberRule(Calendar.SECOND, tokenLen);
break;
case 'S": // millisecond (number)
rule = selectNumberRule(Calendar.MILLISECOND, tokenLen);
break;
case 'E": // day in week (text)
rule = new TextField(Calendar.DAY_OF_WEEK, tokenLen < 4 ? shortWeekdays : weekdays);
break;
case 'D": // day in year (number)
rule = selectNumberRule(Calendar.DAY_OF_YEAR, tokenLen);
break;
case 'F": // day of week in month (number)
rule = selectNumberRule(Calendar.DAY_OF_WEEK_IN_MONTH, tokenLen);
break;
case 'w": // week in year (number)
rule = selectNumberRule(Calendar.WEEK_OF_YEAR, tokenLen);
break;
case 'W": // week in month (number)
rule = selectNumberRule(Calendar.WEEK_OF_MONTH, tokenLen);
break;
case 'a": // am/pm marker (text)
rule = new TextField(Calendar.AM_PM, AmPmStrings);
break;
case 'k": // hour in day (1..24)
rule = new TwentyFourHourField(selectNumberRule(Calendar.HOUR_OF_DAY, tokenLen));
break;
case 'K": // hour in am/pm (0..11)
rule = selectNumberRule(Calendar.HOUR, tokenLen);
break;
case 'z": // time zone (text)
if (tokenLen >= 4) {
rule = new TimeZoneNameRule(mTimeZone, mTimeZoneForced, mLocale, TimeZone.LONG);
} else {
rule = new TimeZoneNameRule(mTimeZone, mTimeZoneForced, mLocale, TimeZone.SHORT);
}
break;
case 'Z": // time zone (value)
if (tokenLen == 1) {
rule = TimeZoneNumberRule.INSTANCE_NO_COLON;
} else {
rule = TimeZoneNumberRule.INSTANCE_COLON;
}
break;
case '\'": // literal text
String sub = token.substring(1);
if (sub.length() == 1) {
rule = new CharacterLiteral(sub.charAt(0));
} else {
rule = new StringLiteral(sub);
}
break;
default:
throw new IllegalArgumentException("Illegal pattern component: " + token);
}
rules.add(rule);
}
return rules;
}
|
protected String parseToken(String pattern,
int[] indexRef) {
StringBuffer buf = new StringBuffer();
int i = indexRef[0];
int length = pattern.length();
char c = pattern.charAt(i);
if (c >= 'A" && c < = 'Z" || c >= 'a" && c < = 'z") {
// Scan a run of the same character, which indicates a time
// pattern.
buf.append(c);
while (i + 1 < length) {
char peek = pattern.charAt(i + 1);
if (peek == c) {
buf.append(c);
i++;
} else {
break;
}
}
} else {
// This will identify token as text.
buf.append('\'");
boolean inLiteral = false;
for (; i < length; i++) {
c = pattern.charAt(i);
if (c == '\'") {
if (i + 1 < length && pattern.charAt(i + 1) == '\'") {
// '' is treated as escaped '
i++;
buf.append(c);
} else {
inLiteral = !inLiteral;
}
} else if (!inLiteral &&
(c >= 'A" && c < = 'Z" || c >= 'a" && c < = 'z")) {
i--;
break;
} else {
buf.append(c);
}
}
}
indexRef[0] = i;
return buf.toString();
}
|
protected FastDateFormat.NumberRule selectNumberRule(int field,
int padding) {
switch (padding) {
case 1:
return new UnpaddedNumberField(field);
case 2:
return new TwoDigitNumberField(field);
default:
return new PaddedNumberField(field, padding);
}
}
|
public String toString() {
return "FastDateFormat[" + mPattern + "]";
}
|