]> granicus.if.org Git - icu/commitdiff
ICU-8511 Add set/getTimeZone methods to ICU4J DateIntervalFormat
authorPeter Edberg <pedberg@unicode.org>
Mon, 3 Mar 2014 02:20:50 +0000 (02:20 +0000)
committerPeter Edberg <pedberg@unicode.org>
Mon, 3 Mar 2014 02:20:50 +0000 (02:20 +0000)
X-SVN-Rev: 35291

icu4j/main/classes/core/src/com/ibm/icu/text/DateIntervalFormat.java
icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/DateIntervalFormatTest.java

index dcce819a96329548ef759109893f6b3ebb717557..d414a3f0f79774bbdd374a35ea75d3fd2d5d1b33 100644 (file)
@@ -21,6 +21,7 @@ import com.ibm.icu.text.DateIntervalInfo.PatternInfo;
 import com.ibm.icu.util.Calendar;
 import com.ibm.icu.util.DateInterval;
 import com.ibm.icu.util.Output;
+import com.ibm.icu.util.TimeZone;
 import com.ibm.icu.util.ULocale;
 import com.ibm.icu.util.ULocale.Category;
 
@@ -871,6 +872,43 @@ public class DateIntervalFormat extends UFormat {
         }
     }
 
+    /**
+     * Get the TimeZone
+     * @return A copy of the TimeZone associated with this date interval formatter.
+     * @draft ICU 53
+     */
+    public TimeZone getTimeZone()
+    {
+        if ( fDateFormat != null ) {
+            // Here we clone, like other getters here, but unlike
+            // DateFormat.getTimeZone() and Calendar.getTimeZone()
+            // which return the TimeZone from the Calendar's zone variable
+            return (TimeZone)(fDateFormat.getTimeZone().clone());
+        }
+        // If fDateFormat is null (unexpected), return default timezone.
+        return TimeZone.getDefault();
+    }
+
+
+    /**
+     * Set the TimeZone for the calendar used by this DateIntervalFormat object.
+     * @param zone The new TimeZone to use.
+     * @draft ICU 53
+     */
+    public void setTimeZone(TimeZone zone)
+    {
+        if (fDateFormat != null) {
+            fDateFormat.setTimeZone(zone);
+        }
+        // fDateFormat has the master calendar for the DateIntervalFormat;
+        // fFromCalendar and fToCalendar are internal work clones of that calendar.
+        if (fFromCalendar != null) {
+            fFromCalendar.setTimeZone(zone);
+        }
+        if (fToCalendar != null) {
+            fToCalendar.setTimeZone(zone);
+        }
+    }
 
     /**
      * Gets the date formatter
index ae5ea26ef2e2744c554c0351da63f563ac0aaa85..38d7c229a69920c169aba796df0d852cd76aa935 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *******************************************************************************
- * Copyright (C) 2001-2013, International Business Machines Corporation and    *
+ * Copyright (C) 2001-2014, International Business Machines Corporation and    *
  * others. All Rights Reserved.                                                *
  *******************************************************************************
  */
@@ -25,6 +25,7 @@ import com.ibm.icu.text.DateIntervalInfo.PatternInfo;
 import com.ibm.icu.text.SimpleDateFormat;
 import com.ibm.icu.util.Calendar;
 import com.ibm.icu.util.DateInterval;
+import com.ibm.icu.util.TimeZone;
 import com.ibm.icu.util.ULocale;
 
 public class DateIntervalFormatTest extends com.ibm.icu.dev.test.TestFmwk {
@@ -1228,6 +1229,38 @@ public class DateIntervalFormatTest extends com.ibm.icu.dev.test.TestFmwk {
                 actualPattern);
     }
     
+    public void TestGetSetTimeZone(){
+        DateIntervalFormat dtitvfmt = DateIntervalFormat.getInstance("MMMdHHmm", Locale.ENGLISH);
+        long date1 = 1299090600000L; // 2011-Mar-02 1030 in US/Pacific, 2011-Mar-03 0330 in Asia/Tokyo
+        long date2 = 1299115800000L; // 2011-Mar-02 1730 in US/Pacific, 2011-Mar-03 1030 in Asia/Tokyo
+        DateInterval dtitv = new DateInterval(date1, date2);
+        TimeZone tzCalif = TimeZone.getFrozenTimeZone("US/Pacific");
+        TimeZone tzTokyo = TimeZone.getFrozenTimeZone("Asia/Tokyo");
+        String fmtCalif = "Mar 2, 10:30 \u2013 Mar 2, 17:30"; // ICU4C result is "Mar 2, 10:30 \u2013 17:30" (does not duplicate day)
+        String fmtTokyo = "Mar 3, 03:30 \u2013 Mar 3, 10:30"; // ICU4C result is "Mar 3, 03:30 \u2013 10:30" (does not duplicate day)
+        
+        StringBuffer buf = new StringBuffer();
+        FieldPosition pos = new FieldPosition(0);
+        dtitvfmt.setTimeZone(tzCalif);
+        dtitvfmt.format(dtitv, buf, pos);
+        if (!buf.toString().equals(fmtCalif)) {
+            errln("DateIntervalFormat for tzCalif, expect \"" + fmtCalif + "\", get \"" + buf + "\"");
+        }
+        
+        buf.setLength(0);
+        pos.setBeginIndex(0);
+        dtitvfmt.setTimeZone(tzTokyo);
+        dtitvfmt.format(dtitv, buf, pos);
+        if (!buf.toString().equals(fmtTokyo)) {
+            errln("DateIntervalFormat for tzTokyo, expect \"" + fmtTokyo + "\", get \"" + buf + "\"");
+        }
+        
+        if (!dtitvfmt.getTimeZone().equals(tzTokyo)) {
+            errln("DateIntervalFormat.getTimeZone() returns mismatch");
+        }
+    }
+
+
     /* Tests the method
      *      public int hashCode()
      */