*/
public final static DateFormat getTimeInstance()
{
- return get(-1, DEFAULT, ULocale.getDefault(Category.FORMAT));
+ return get(-1, DEFAULT, ULocale.getDefault(Category.FORMAT), null);
}
/**
*/
public final static DateFormat getTimeInstance(int style)
{
- return get(-1, style, ULocale.getDefault(Category.FORMAT));
+ return get(-1, style, ULocale.getDefault(Category.FORMAT), null);
}
/**
public final static DateFormat getTimeInstance(int style,
Locale aLocale)
{
- return get(-1, style, ULocale.forLocale(aLocale));
+ return get(-1, style, ULocale.forLocale(aLocale), null);
}
/**
public final static DateFormat getTimeInstance(int style,
ULocale locale)
{
- return get(-1, style, locale);
+ return get(-1, style, locale, null);
}
/**
*/
public final static DateFormat getDateInstance()
{
- return get(DEFAULT, -1, ULocale.getDefault(Category.FORMAT));
+ return get(DEFAULT, -1, ULocale.getDefault(Category.FORMAT), null);
}
/**
*/
public final static DateFormat getDateInstance(int style)
{
- return get(style, -1, ULocale.getDefault(Category.FORMAT));
+ return get(style, -1, ULocale.getDefault(Category.FORMAT), null);
}
/**
public final static DateFormat getDateInstance(int style,
Locale aLocale)
{
- return get(style, -1, ULocale.forLocale(aLocale));
+ return get(style, -1, ULocale.forLocale(aLocale), null);
}
/**
public final static DateFormat getDateInstance(int style,
ULocale locale)
{
- return get(style, -1, locale);
+ return get(style, -1, locale, null);
}
/**
*/
public final static DateFormat getDateTimeInstance()
{
- return get(DEFAULT, DEFAULT, ULocale.getDefault(Category.FORMAT));
+ return get(DEFAULT, DEFAULT, ULocale.getDefault(Category.FORMAT), null);
}
/**
public final static DateFormat getDateTimeInstance(int dateStyle,
int timeStyle)
{
- return get(dateStyle, timeStyle, ULocale.getDefault(Category.FORMAT));
+ return get(dateStyle, timeStyle, ULocale.getDefault(Category.FORMAT), null);
}
/**
public final static DateFormat getDateTimeInstance(
int dateStyle, int timeStyle, Locale aLocale)
{
- return get(dateStyle, timeStyle, ULocale.forLocale(aLocale));
+ return get(dateStyle, timeStyle, ULocale.forLocale(aLocale), null);
}
/**
public final static DateFormat getDateTimeInstance(
int dateStyle, int timeStyle, ULocale locale)
{
- return get(dateStyle, timeStyle, locale);
+ return get(dateStyle, timeStyle, locale, null);
}
/**
* @param timeStyle a value from 0 to 3 indicating the time format,
* or -1 to indicate no time
* @param loc the locale for the format
+ * @param cal the calendar to be used, or null
*/
- private static DateFormat get(int dateStyle, int timeStyle, ULocale loc) {
- if((timeStyle != -1 && (timeStyle & RELATIVE)>0) ||
- (dateStyle != -1 && (dateStyle & RELATIVE)>0)) {
- RelativeDateFormat r = new RelativeDateFormat(timeStyle, dateStyle /* offset? */, loc);
+ private static DateFormat get(int dateStyle, int timeStyle, ULocale loc, Calendar cal) {
+ if((timeStyle != DateFormat.NONE && (timeStyle & RELATIVE)>0) ||
+ (dateStyle != DateFormat.NONE && (dateStyle & RELATIVE)>0)) {
+ RelativeDateFormat r = new RelativeDateFormat(timeStyle, dateStyle /* offset? */, loc, cal);
return r;
}
- if (timeStyle < -1 || timeStyle > 3) {
+ if (timeStyle < DateFormat.NONE || timeStyle > DateFormat.SHORT) {
throw new IllegalArgumentException("Illegal time style " + timeStyle);
}
- if (dateStyle < -1 || dateStyle > 3) {
+ if (dateStyle < DateFormat.NONE || dateStyle > DateFormat.SHORT) {
throw new IllegalArgumentException("Illegal date style " + dateStyle);
}
+
+ if (cal == null) {
+ cal = Calendar.getInstance(loc);
+ }
+
try {
- Calendar cal = Calendar.getInstance(loc);
DateFormat result = cal.getDateTimeFormat(dateStyle, timeStyle, loc);
result.setLocale(cal.getLocale(ULocale.VALID_LOCALE),
cal.getLocale(ULocale.ACTUAL_LOCALE));
static final public DateFormat getDateTimeInstance(Calendar cal, int dateStyle,
int timeStyle, Locale locale)
{
- return cal.getDateTimeFormat(dateStyle, timeStyle, ULocale.forLocale(locale));
+ return getDateTimeInstance(dateStyle, timeStyle, ULocale.forLocale(locale));
}
/**
static final public DateFormat getDateTimeInstance(Calendar cal, int dateStyle,
int timeStyle, ULocale locale)
{
- return cal.getDateTimeFormat(dateStyle, timeStyle, locale);
+ if (cal == null) {
+ throw new IllegalArgumentException("Calendar must be supplied");
+ }
+ return get(dateStyle, timeStyle, locale, cal);
}
/**
}
}
}
-
+
+ // A regression test case for ticket#10632.
+ // Make sure RELATIVE style works for getInstance overloads taking
+ // Calendar instance.
+ public void Test10632() {
+ Date[] testDates = new Date[3];
+ Calendar cal = Calendar.getInstance();
+
+ // today
+ testDates[0] = cal.getTime();
+
+ // tomorrow
+ cal.add(Calendar.DATE, 1);
+ testDates[1] = cal.getTime();
+
+ // yesterday
+ cal.add(Calendar.DATE, -2);
+ testDates[2] = cal.getTime();
+
+
+ // Relative styles for testing
+ int[] dateStylesList = {
+ DateFormat.RELATIVE_FULL,
+ DateFormat.RELATIVE_LONG,
+ DateFormat.RELATIVE_MEDIUM,
+ DateFormat.RELATIVE_SHORT
+ };
+
+ Calendar fmtCal = DateFormat.getInstance().getCalendar();
+
+ for (int i = 0; i < dateStylesList.length; i++) {
+ DateFormat fmt0 = DateFormat.getDateTimeInstance(dateStylesList[i], DateFormat.DEFAULT);
+ DateFormat fmt1 = DateFormat.getDateTimeInstance(fmtCal, dateStylesList[i], DateFormat.DEFAULT);
+
+ for (int j = 0; j < testDates.length; j++) {
+ String s0 = fmt0.format(testDates[j]);
+ String s1 = fmt1.format(testDates[j]);
+
+ if (!s0.equals(s1)) {
+ errln("FAIL: Different results returned by two equivalent relative formatters: s0="
+ + s0 + ", s1=" + s1);
+ }
+ }
+ }
+ }
}