From bb74c8c14cbacd3cad9aa5a657819b972500a595 Mon Sep 17 00:00:00 2001 From: Peter Edberg Date: Mon, 3 Mar 2014 02:20:50 +0000 Subject: [PATCH] ICU-8511 Add set/getTimeZone methods to ICU4J DateIntervalFormat X-SVN-Rev: 35291 --- .../com/ibm/icu/text/DateIntervalFormat.java | 38 +++++++++++++++++++ .../test/format/DateIntervalFormatTest.java | 35 ++++++++++++++++- 2 files changed, 72 insertions(+), 1 deletion(-) diff --git a/icu4j/main/classes/core/src/com/ibm/icu/text/DateIntervalFormat.java b/icu4j/main/classes/core/src/com/ibm/icu/text/DateIntervalFormat.java index dcce819a96..d414a3f0f7 100644 --- a/icu4j/main/classes/core/src/com/ibm/icu/text/DateIntervalFormat.java +++ b/icu4j/main/classes/core/src/com/ibm/icu/text/DateIntervalFormat.java @@ -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 diff --git a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/DateIntervalFormatTest.java b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/DateIntervalFormatTest.java index ae5ea26ef2..38d7c229a6 100644 --- a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/DateIntervalFormatTest.java +++ b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/DateIntervalFormatTest.java @@ -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() */