public StringBuffer format(Date date, StringBuffer toAppendTo,
FieldPosition pos)
{
pos.beginIndex = pos.endIndex = 0;
return format(date, toAppendTo, pos.getFieldDelegate());
}
// Called from Format after creating a FieldDelegate
private StringBuffer format(Date date, StringBuffer toAppendTo,
FieldDelegate delegate) {
// Convert input date to time field list
calendar.setTime(date);
boolean useDateFormatSymbols = useDateFormatSymbols();
for (int i = 0; i < compiledPattern.length; ) {
int tag = compiledPattern[i] >>> 8;
int count = compiledPattern[i++] & 0xff;
if (count == 255) {
count = compiledPattern[i++] << 16;
count |= compiledPattern[i++];
}
4、替代方案:
(1)将工具类中private static SimpleDateFormat simpleDateFormat = null;去除掉,写到方法里面去;
(2)使用threadlocal方式;
(3)使用Apache下面的DateFormatUtils、DateUtils、最主要的区别是线程安全、占用内存少。(推荐)
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.lang.time.DateUtils;附图:SimpleDateFormat与DateFormatUtils、DateUtils占用的内存,可以看出apache工具类的优势。