]> granicus.if.org Git - icu/commitdiff
ICU-10632 DateFormat factory methods taking Calendar instance to handle relative...
authorYoshito Umaoka <y.umaoka@gmail.com>
Fri, 24 Jan 2014 22:47:23 +0000 (22:47 +0000)
committerYoshito Umaoka <y.umaoka@gmail.com>
Fri, 24 Jan 2014 22:47:23 +0000 (22:47 +0000)
X-SVN-Rev: 34988

icu4j/main/classes/core/src/com/ibm/icu/impl/RelativeDateFormat.java
icu4j/main/classes/core/src/com/ibm/icu/text/DateFormat.java
icu4j/main/classes/core/src/com/ibm/icu/util/Calendar.java
icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/DateFormatTest.java

index 8db8958c1dd4e2b8c78626f7284b70aea93c099f..d633754d16cae2c07bf9082a6583c445d64154b8 100644 (file)
@@ -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)
      */
index 503217ca0b0853d45221725eaa476ced254273ba..ef5138cdcecfff44e27078e8eccebaad606b6d2e 100644 (file)
@@ -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);
     }
 
     /**
index fe24a29e438d2ee482ba5da0c7553efaeb88fc3d..cb2066fb0a8afa3d5d8dfe4a15ac5c616793b455 100644 (file)
@@ -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;
 
index f19c87a06789aeb86aa2f7d93357cc2f405212a4..8489d95386aee80e718cedf73c93dbf1031d4c2a 100644 (file)
@@ -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);
+                }
+            }
+        }
+    }
 }