📄 fastdateformat.java
字号:
mSize = size;
}
public int estimateLength() {
return 4;
}
public void appendTo(StringBuffer buffer, Calendar calendar) {
appendTo(buffer, calendar.get(mField));
}
public final void appendTo(StringBuffer buffer, int value) {
if (value < 100) {
for (int i = mSize; --i >= 2; ) {
buffer.append('0');
}
buffer.append((char)(value / 10 + '0'));
buffer.append((char)(value % 10 + '0'));
} else {
int digits;
if (value < 1000) {
digits = 3;
} else {
digits = (int)(Math.log(value) / LOG_10) + 1;
}
for (int i = mSize; --i >= digits; ) {
buffer.append('0');
}
buffer.append(Integer.toString(value));
}
}
}
/**
* <p>Inner class to output a two digit number.</p>
*/
private static class TwoDigitNumberField implements NumberRule {
private final int mField;
TwoDigitNumberField(int field) {
mField = field;
}
public int estimateLength() {
return 2;
}
public void appendTo(StringBuffer buffer, Calendar calendar) {
appendTo(buffer, calendar.get(mField));
}
public final void appendTo(StringBuffer buffer, int value) {
if (value < 100) {
buffer.append((char)(value / 10 + '0'));
buffer.append((char)(value % 10 + '0'));
} else {
buffer.append(Integer.toString(value));
}
}
}
/**
* <p>Inner class to output a two digit year.</p>
*/
private static class TwoDigitYearField implements NumberRule {
static final TwoDigitYearField INSTANCE = new TwoDigitYearField();
TwoDigitYearField() {
}
public int estimateLength() {
return 2;
}
public void appendTo(StringBuffer buffer, Calendar calendar) {
appendTo(buffer, calendar.get(Calendar.YEAR) % 100);
}
public final void appendTo(StringBuffer buffer, int value) {
buffer.append((char)(value / 10 + '0'));
buffer.append((char)(value % 10 + '0'));
}
}
/**
* <p>Inner class to output a two digit month.</p>
*/
private static class TwoDigitMonthField implements NumberRule {
static final TwoDigitMonthField INSTANCE = new TwoDigitMonthField();
TwoDigitMonthField() {
}
public int estimateLength() {
return 2;
}
public void appendTo(StringBuffer buffer, Calendar calendar) {
appendTo(buffer, calendar.get(Calendar.MONTH) + 1);
}
public final void appendTo(StringBuffer buffer, int value) {
buffer.append((char)(value / 10 + '0'));
buffer.append((char)(value % 10 + '0'));
}
}
/**
* <p>Inner class to output the twelve hour field.</p>
*/
private static class TwelveHourField implements NumberRule {
private final NumberRule mRule;
TwelveHourField(NumberRule rule) {
mRule = rule;
}
public int estimateLength() {
return mRule.estimateLength();
}
public void appendTo(StringBuffer buffer, Calendar calendar) {
int value = calendar.get(Calendar.HOUR);
if (value == 0) {
value = calendar.getLeastMaximum(Calendar.HOUR) + 1;
}
mRule.appendTo(buffer, value);
}
public void appendTo(StringBuffer buffer, int value) {
mRule.appendTo(buffer, value);
}
}
/**
* <p>Inner class to output the twenty four hour field.</p>
*/
private static class TwentyFourHourField implements NumberRule {
private final NumberRule mRule;
TwentyFourHourField(NumberRule rule) {
mRule = rule;
}
public int estimateLength() {
return mRule.estimateLength();
}
public void appendTo(StringBuffer buffer, Calendar calendar) {
int value = calendar.get(Calendar.HOUR_OF_DAY);
if (value == 0) {
value = calendar.getMaximum(Calendar.HOUR_OF_DAY) + 1;
}
mRule.appendTo(buffer, value);
}
public void appendTo(StringBuffer buffer, int value) {
mRule.appendTo(buffer, value);
}
}
/**
* <p>Inner class to output a time zone name.</p>
*/
private static class TimeZoneNameRule implements Rule {
private final TimeZone mTimeZone;
private final boolean mTimeZoneForced;
private final Locale mLocale;
private final int mStyle;
private final String mStandard;
private final String mDaylight;
TimeZoneNameRule(TimeZone timeZone, boolean timeZoneForced, Locale locale, int style) {
mTimeZone = timeZone;
mTimeZoneForced = timeZoneForced;
mLocale = locale;
mStyle = style;
if (timeZoneForced) {
mStandard = getTimeZoneDisplay(timeZone, false, style, locale);
mDaylight = getTimeZoneDisplay(timeZone, true, style, locale);
} else {
mStandard = null;
mDaylight = null;
}
}
public int estimateLength() {
if (mTimeZoneForced) {
return Math.max(mStandard.length(), mDaylight.length());
} else if (mStyle == TimeZone.SHORT) {
return 4;
} else {
return 40;
}
}
public void appendTo(StringBuffer buffer, Calendar calendar) {
if (mTimeZoneForced) {
if (mTimeZone.useDaylightTime() && calendar.get(Calendar.DST_OFFSET) != 0) {
buffer.append(mDaylight);
} else {
buffer.append(mStandard);
}
} else {
TimeZone timeZone = calendar.getTimeZone();
if (timeZone.useDaylightTime() && calendar.get(Calendar.DST_OFFSET) != 0) {
buffer.append(getTimeZoneDisplay(timeZone, true, mStyle, mLocale));
} else {
buffer.append(getTimeZoneDisplay(timeZone, false, mStyle, mLocale));
}
}
}
}
/**
* <p>Inner class to output a time zone as a number <code>+/-HHMM</code>
* or <code>+/-HH:MM</code>.</p>
*/
private static class TimeZoneNumberRule implements Rule {
static final TimeZoneNumberRule INSTANCE_COLON = new TimeZoneNumberRule(true);
static final TimeZoneNumberRule INSTANCE_NO_COLON = new TimeZoneNumberRule(false);
final boolean mColon;
TimeZoneNumberRule(boolean colon) {
mColon = colon;
}
public int estimateLength() {
return 5;
}
public void appendTo(StringBuffer buffer, Calendar calendar) {
int offset = calendar.get(Calendar.ZONE_OFFSET) + calendar.get(Calendar.DST_OFFSET);
if (offset < 0) {
buffer.append('-');
offset = -offset;
} else {
buffer.append('+');
}
int hours = offset / (60 * 60 * 1000);
buffer.append((char)(hours / 10 + '0'));
buffer.append((char)(hours % 10 + '0'));
if (mColon) {
buffer.append(':');
}
int minutes = offset / (60 * 1000) - 60 * hours;
buffer.append((char)(minutes / 10 + '0'));
buffer.append((char)(minutes % 10 + '0'));
}
}
// ----------------------------------------------------------------------
/**
* <p>Inner class that acts as a compound key for time zone names.</p>
*/
private static class TimeZoneDisplayKey {
private final TimeZone mTimeZone;
private final int mStyle;
private final Locale mLocale;
TimeZoneDisplayKey(TimeZone timeZone,
boolean daylight, int style, Locale locale) {
mTimeZone = timeZone;
if (daylight) {
style |= 0x80000000;
}
mStyle = style;
mLocale = locale;
}
public int hashCode() {
return mStyle * 31 + mLocale.hashCode();
}
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof TimeZoneDisplayKey) {
TimeZoneDisplayKey other = (TimeZoneDisplayKey)obj;
return
mTimeZone.equals(other.mTimeZone) &&
mStyle == other.mStyle &&
mLocale.equals(other.mLocale);
}
return false;
}
}
// ----------------------------------------------------------------------
/**
* <p>Helper class for creating compound objects.</p>
*
* <p>One use for this class is to create a hashtable key
* out of multiple objects.</p>
*/
private static class Pair {
private final Object mObj1;
private final Object mObj2;
public Pair(Object obj1, Object obj2) {
mObj1 = obj1;
mObj2 = obj2;
}
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof Pair)) {
return false;
}
Pair key = (Pair)obj;
return
(mObj1 == null ?
key.mObj1 == null : mObj1.equals(key.mObj1)) &&
(mObj2 == null ?
key.mObj2 == null : mObj2.equals(key.mObj2));
}
public int hashCode() {
return
(mObj1 == null ? 0 : mObj1.hashCode()) +
(mObj2 == null ? 0 : mObj2.hashCode());
}
public String toString() {
return "[" + mObj1 + ':' + mObj2 + ']';
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -