private DateFormat _getDateFormat(
FacesContext context,
String pattern
)
{
RequestContext reqContext = RequestContext.getCurrentInstance();
Locale locale = getLocale();
if (null == locale)
{
if (reqContext != null)
locale = reqContext.getFormattingLocale();
if (locale == null)
{
locale = context.getViewRoot().getLocale();
}
}
TimeZone tZone = _getTimeZone();
DateFormat format = _getCachedFormat(locale, tZone, pattern);
if (format != null)
{
format.setTimeZone(tZone);
}
else
{
// create a format off of the styles.
// We will change shortish to short only at place where it is required.
// Otherwise we may end up throwing convert exception for case where
// dateStyle is invalid. So evaluating only at the required place.
if ( null == pattern )
{
int type = _getType(getType());
if (type == _TYPE_DATE || type == _TYPE_BOTH)
{
int actualDateStyle = _getDateStyle(getDateStyle());
int dateStyle = _changeShortishAsShortIfNeeded(actualDateStyle);
if (type == _TYPE_DATE)
{
format = DateFormat.getDateInstance(dateStyle, locale);
}
else
{
int timeStyle = _getTimeStyle(getTimeStyle());
format = DateFormat.getDateTimeInstance(dateStyle, timeStyle, locale);
}
}
if (type == _TYPE_TIME)
{
int timeStyle = _getTimeStyle(getTimeStyle());
format = DateFormat.getTimeInstance(timeStyle, locale);
}
}
else
{
// create a format off of the pattern
format = _getSimpleDateFormat(pattern, locale);
}
if (format instanceof SimpleDateFormat)
{
SimpleDateFormat simpleFormat = (SimpleDateFormat)format;
// make sure that we have a 4 digit year for "shortish"
// dates
// DO NOT CHANGE THE FOLLOWING LINE to "dateStyle"; this
// must be retrieved from the instance variable! (See above)
// and we need to apply shortish only if it is of date type or
// type is date and time.
if (null == pattern && "shortish".equals(getDateStyle()) )
{
int type = _getType(getType());
if (type == _TYPE_DATE || type == _TYPE_BOTH )
{
simpleFormat = _get4YearFormat(simpleFormat, locale);
format = simpleFormat;
}
}
Calendar cal;
if (reqContext == null)
{
cal = null;
if(_LOG.isWarning())
{
_LOG.warning("Cannot find RequestContext; two-digit-year-start will be defaulted");
}
}
else
{
cal = new GregorianCalendar(reqContext.getTwoDigitYearStart(), 0, 0);
}
if (cal != null)
simpleFormat.set2DigitYearStart(cal.getTime());
}