mirror of
https://github.com/unicode-org/icu.git
synced 2025-04-13 08:53:20 +00:00
ICU-10632 DateFormat factory methods taking Calendar instance to handle relative styles properly.
X-SVN-Rev: 34988
This commit is contained in:
parent
33db481060
commit
1f070a89af
4 changed files with 86 additions and 24 deletions
|
@ -54,8 +54,11 @@ public class RelativeDateFormat extends DateFormat {
|
|||
* @param timeStyle The time style for the date and time.
|
||||
* @param dateStyle The date style for the date and time.
|
||||
* @param locale The locale for the date.
|
||||
* @param cal The calendar to be used
|
||||
*/
|
||||
public RelativeDateFormat(int timeStyle, int dateStyle, ULocale locale) {
|
||||
public RelativeDateFormat(int timeStyle, int dateStyle, ULocale locale, Calendar cal) {
|
||||
calendar = cal;
|
||||
|
||||
fLocale = locale;
|
||||
fTimeStyle = timeStyle;
|
||||
fDateStyle = dateStyle;
|
||||
|
@ -92,7 +95,7 @@ public class RelativeDateFormat extends DateFormat {
|
|||
loadDates();
|
||||
initializeCombinedFormat(calendar, fLocale);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* serial version (generated)
|
||||
*/
|
||||
|
|
|
@ -1165,7 +1165,7 @@ public abstract class DateFormat extends UFormat {
|
|||
*/
|
||||
public final static DateFormat getTimeInstance()
|
||||
{
|
||||
return get(-1, DEFAULT, ULocale.getDefault(Category.FORMAT));
|
||||
return get(-1, DEFAULT, ULocale.getDefault(Category.FORMAT), null);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1180,7 +1180,7 @@ public abstract class DateFormat extends UFormat {
|
|||
*/
|
||||
public final static DateFormat getTimeInstance(int style)
|
||||
{
|
||||
return get(-1, style, ULocale.getDefault(Category.FORMAT));
|
||||
return get(-1, style, ULocale.getDefault(Category.FORMAT), null);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1196,7 +1196,7 @@ public abstract class DateFormat extends UFormat {
|
|||
public final static DateFormat getTimeInstance(int style,
|
||||
Locale aLocale)
|
||||
{
|
||||
return get(-1, style, ULocale.forLocale(aLocale));
|
||||
return get(-1, style, ULocale.forLocale(aLocale), null);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1212,7 +1212,7 @@ public abstract class DateFormat extends UFormat {
|
|||
public final static DateFormat getTimeInstance(int style,
|
||||
ULocale locale)
|
||||
{
|
||||
return get(-1, style, locale);
|
||||
return get(-1, style, locale, null);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1224,7 +1224,7 @@ public abstract class DateFormat extends UFormat {
|
|||
*/
|
||||
public final static DateFormat getDateInstance()
|
||||
{
|
||||
return get(DEFAULT, -1, ULocale.getDefault(Category.FORMAT));
|
||||
return get(DEFAULT, -1, ULocale.getDefault(Category.FORMAT), null);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1242,7 +1242,7 @@ public abstract class DateFormat extends UFormat {
|
|||
*/
|
||||
public final static DateFormat getDateInstance(int style)
|
||||
{
|
||||
return get(style, -1, ULocale.getDefault(Category.FORMAT));
|
||||
return get(style, -1, ULocale.getDefault(Category.FORMAT), null);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1261,7 +1261,7 @@ public abstract class DateFormat extends UFormat {
|
|||
public final static DateFormat getDateInstance(int style,
|
||||
Locale aLocale)
|
||||
{
|
||||
return get(style, -1, ULocale.forLocale(aLocale));
|
||||
return get(style, -1, ULocale.forLocale(aLocale), null);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1280,7 +1280,7 @@ public abstract class DateFormat extends UFormat {
|
|||
public final static DateFormat getDateInstance(int style,
|
||||
ULocale locale)
|
||||
{
|
||||
return get(style, -1, locale);
|
||||
return get(style, -1, locale, null);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1292,7 +1292,7 @@ public abstract class DateFormat extends UFormat {
|
|||
*/
|
||||
public final static DateFormat getDateTimeInstance()
|
||||
{
|
||||
return get(DEFAULT, DEFAULT, ULocale.getDefault(Category.FORMAT));
|
||||
return get(DEFAULT, DEFAULT, ULocale.getDefault(Category.FORMAT), null);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1314,7 +1314,7 @@ public abstract class DateFormat extends UFormat {
|
|||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1334,7 +1334,7 @@ public abstract class DateFormat extends UFormat {
|
|||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1354,7 +1354,7 @@ public abstract class DateFormat extends UFormat {
|
|||
public final static DateFormat getDateTimeInstance(
|
||||
int dateStyle, int timeStyle, ULocale locale)
|
||||
{
|
||||
return get(dateStyle, timeStyle, locale);
|
||||
return get(dateStyle, timeStyle, locale, null);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1625,22 +1625,27 @@ public abstract class DateFormat extends UFormat {
|
|||
* @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));
|
||||
|
@ -1773,7 +1778,7 @@ public abstract class DateFormat extends UFormat {
|
|||
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));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1801,7 +1806,10 @@ public abstract class DateFormat extends UFormat {
|
|||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -3461,6 +3461,13 @@ public abstract class Calendar implements Serializable, Cloneable, Comparable<Ca
|
|||
|
||||
static private DateFormat formatHelper(Calendar cal, ULocale loc, int dateStyle,
|
||||
int timeStyle) {
|
||||
if (timeStyle < DateFormat.NONE || timeStyle > DateFormat.SHORT) {
|
||||
throw new IllegalArgumentException("Illegal time style " + timeStyle);
|
||||
}
|
||||
if (dateStyle < DateFormat.NONE || dateStyle > DateFormat.SHORT) {
|
||||
throw new IllegalArgumentException("Illegal date style " + dateStyle);
|
||||
}
|
||||
|
||||
PatternData patternData = PatternData.make(cal, loc);
|
||||
String override = null;
|
||||
|
||||
|
|
|
@ -4391,5 +4391,49 @@ public class DateFormatTest extends com.ibm.icu.dev.test.TestFmwk {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue