From 70939d31521519ce71f86d4d6da463f573f81595 Mon Sep 17 00:00:00 2001 From: Yoshito Umaoka Date: Mon, 14 Jan 2008 15:27:46 +0000 Subject: [PATCH] ICU-4943 Added some constructors in ChineseCalendar to make it consistent with other Calendar implementation class. X-SVN-Rev: 23219 --- .../icu/dev/test/calendar/ChineseTest.java | 83 +++++++++- .../src/com/ibm/icu/util/ChineseCalendar.java | 153 ++++++++++++++++-- 2 files changed, 218 insertions(+), 18 deletions(-) diff --git a/icu4j/src/com/ibm/icu/dev/test/calendar/ChineseTest.java b/icu4j/src/com/ibm/icu/dev/test/calendar/ChineseTest.java index fab831f04f..c26fe0ed77 100644 --- a/icu4j/src/com/ibm/icu/dev/test/calendar/ChineseTest.java +++ b/icu4j/src/com/ibm/icu/dev/test/calendar/ChineseTest.java @@ -1,5 +1,5 @@ /********************************************************************* - * Copyright (C) 2000-2007, International Business Machines Corporation and + * Copyright (C) 2000-2008, International Business Machines Corporation and * others. All Rights Reserved. ********************************************************************* */ @@ -527,6 +527,85 @@ public class ChineseTest extends CalendarTest { // } public void TestCoverage() { + // Coverage for constructors + { + // new ChineseCalendar(Date) + ChineseCalendar cal = new ChineseCalendar(new Date()); + if(cal == null){ + errln("could not create ChineseCalendar with Date"); + } + } + + { + // new ChineseCalendar(int year, int month, int isLeapMonth, int date) + ChineseCalendar cal = new ChineseCalendar(23, Calendar.JULY, 1, 2); + if(cal == null){ + errln("could not create ChineseCalendar with year,month,isLeapMonth,date"); + } + // Make sure the given values are properly set + if (cal.get(Calendar.YEAR) != 23 || cal.get(Calendar.MONTH) != Calendar.JULY + || cal.get(ChineseCalendar.IS_LEAP_MONTH) != 1 || cal.get(Calendar.DATE) != 2 + || cal.get(Calendar.MILLISECONDS_IN_DAY) != 0) { + errln("ChineseCalendar was initialized incorrectly with year,month,isLeapMonth,date"); + } + } + + { + // new ChineseCalendar(int year, int month, int isLeapMonth, int date, int hour, int minute, int second) + ChineseCalendar cal = new ChineseCalendar(23, Calendar.JULY, 1, 2, 12, 34, 56); + if(cal == null){ + errln("could not create ChineseCalendar with year,month,isLeapMonth,date,hour,minute,second"); + } + // Make sure the given values are properly set + if (cal.get(Calendar.YEAR) != 23 || cal.get(Calendar.MONTH) != Calendar.JULY + || cal.get(ChineseCalendar.IS_LEAP_MONTH) != 1 || cal.get(Calendar.DATE) != 2 + || cal.get(Calendar.HOUR_OF_DAY) != 12 || cal.get(Calendar.MINUTE) != 34 + || cal.get(Calendar.SECOND) != 56 || cal.get(Calendar.MILLISECOND) != 0) { + errln("ChineseCalendar was initialized incorrectly with year,month,isLeapMonth,date,hour,minute,second"); + } + } + + { + // new ChineseCalendar(Locale) + ChineseCalendar cal = new ChineseCalendar(Locale.getDefault()); + if(cal == null){ + errln("could not create ChineseCalendar with Locale"); + } + } + + { + // new ChineseCalendar(ULocale) + ChineseCalendar cal = new ChineseCalendar(ULocale.getDefault()); + if(cal == null){ + errln("could not create ChineseCalendar with ULocale"); + } + } + + + { + // new ChineseCalendar(TimeZone) + ChineseCalendar cal = new ChineseCalendar(TimeZone.getDefault()); + if(cal == null){ + errln("could not create ChineseCalendar with TimeZone"); + } + } + + { + // new ChineseCalendar(TimeZone, Locale) + ChineseCalendar cal = new ChineseCalendar(TimeZone.getDefault(), Locale.getDefault()); + if(cal == null){ + errln("could not create ChineseCalendar with TimeZone,Locale"); + } + } + + { + // new ChineseCalendar(TimeZone, ULocale) + ChineseCalendar cal = new ChineseCalendar(TimeZone.getDefault(), ULocale.getDefault()); + if(cal == null){ + errln("could not create ChineseCalendar with TimeZone,ULocale"); + } + } + ChineseCalendar cal = new ChineseCalendar(); DateFormat format = DateFormat.getInstance(cal); if(!(format instanceof ChineseDateFormat)){ @@ -553,7 +632,7 @@ public class ChineseTest extends CalendarTest { time = getDate(2001, Calendar.MAY, 23); str = fmt.format(time); logln("Chinese calendar time: " + time + " result: " + str); - } + } } public void TestScratch(){ String[] strMonths = {"Januari", "Pebruari", "Maret", "April", "Mei", "Juni", diff --git a/icu4j/src/com/ibm/icu/util/ChineseCalendar.java b/icu4j/src/com/ibm/icu/util/ChineseCalendar.java index 033ac88075..87a6f566d6 100644 --- a/icu4j/src/com/ibm/icu/util/ChineseCalendar.java +++ b/icu4j/src/com/ibm/icu/util/ChineseCalendar.java @@ -1,20 +1,21 @@ /********************************************************************* - * Copyright (C) 2000-2007, International Business Machines + * Copyright (C) 2000-2008, International Business Machines * Corporation and others. All Rights Reserved. ********************************************************************* */ package com.ibm.icu.util; -import com.ibm.icu.text.*; -import com.ibm.icu.util.TimeZone; -import com.ibm.icu.impl.CalendarAstronomer; -import com.ibm.icu.impl.CalendarCache; - import java.io.IOException; import java.io.ObjectInputStream; +import java.util.Date; import java.util.Locale; +import com.ibm.icu.impl.CalendarAstronomer; +import com.ibm.icu.impl.CalendarCache; +import com.ibm.icu.text.ChineseDateFormat; +import com.ibm.icu.text.DateFormat; + /** * ChineseCalendar is a concrete subclass of {@link Calendar} * that implements a traditional Chinese calendar. The traditional Chinese @@ -135,7 +136,7 @@ public class ChineseCalendar extends Calendar { //------------------------------------------------------------------ /** - * Construct a Chinese calendar with the default time zone and locale. + * Construct a ChineseCalendar with the default time zone and locale. * @stable ICU 2.8 */ public ChineseCalendar() { @@ -144,20 +145,140 @@ public class ChineseCalendar extends Calendar { } /** - * Construct a Chinese calendar with the given time zone and locale. - * @param zone time zone for this calendar - * @param locale locale for this calendar - * @stable ICU 2.8 + * Construct a ChineseCalendar with the give date set in the default time zone + * with the default locale. + * @param date The date to which the new calendar is set. + * @draft ICU 4.0 + * @provisional This API might change or be removed in a future release. */ - public ChineseCalendar(TimeZone zone, Locale locale) { - super(zone, locale); + public ChineseCalendar(Date date) { + super(); + setTime(date); + } + + /** + * Constructs a ChineseCalendar with the given date set + * in the default time zone with the default locale. + * + * @param year The value used to set the calendar's {@link #YEAR YEAR} time field. + * @param month The value used to set the calendar's {@link #MONTH MONTH} time field. + * The value is 0-based. e.g., 0 for January. + * @param isLeapMonth The value used to set the Chiense calendar's (@link #IS_LEAP_MONTH) + * time field. + * @param date The value used to set the calendar's {@link #DATE DATE} time field. + * @draft ICU 4.0 + * @provisional This API might change or be removed in a future release. + */ + public ChineseCalendar(int year, int month, int isLeapMonth, int date) { + super(TimeZone.getDefault(), ULocale.getDefault()); + + // We need to set the current time once to initialize the ChineseCalendar's + // ERA field to be the current era. + setTimeInMillis(System.currentTimeMillis()); + // Then we need to clean up time fields + this.set(MILLISECONDS_IN_DAY, 0); + + // Then set the given field values. + this.set(YEAR, year); + this.set(MONTH, month); + this.set(IS_LEAP_MONTH, isLeapMonth); + this.set(DATE, date); + } + + /** + * Constructs a ChineseCalendar with the given date + * and time set for the default time zone with the default locale. + * + * @param year the value used to set the {@link #YEAR YEAR} time field in the calendar. + * @param month the value used to set the {@link #MONTH MONTH} time field in the calendar. + * Note that the month value is 0-based. e.g., 0 for January. + * @param isLeapMonth the value used to set the {@link #IS_LEAP_MONTH} time field + * in the calendar. + * @param date the value used to set the {@link #DATE DATE} time field in the calendar. + * @param hour the value used to set the {@link #HOUR_OF_DAY HOUR_OF_DAY} time field + * in the calendar. + * @param minute the value used to set the {@link #MINUTE MINUTE} time field + * in the calendar. + * @param second the value used to set the {@link #SECOND SECOND} time field + * in the calendar. + * @draft ICU 4.0 + * @provisional This API might change or be removed in a future release. + */ + public ChineseCalendar(int year, int month, int isLeapMonth, int date, int hour, + int minute, int second) + { + super(TimeZone.getDefault(), ULocale.getDefault()); + + // We need to set the current time once to initialize the ChineseCalendar's + // ERA field to be the current era. + setTimeInMillis(System.currentTimeMillis()); + // Then set 0 to millisecond field + this.set(MILLISECOND, 0); + + // Then, set the given field values. + this.set(YEAR, year); + this.set(MONTH, month); + this.set(IS_LEAP_MONTH, isLeapMonth); + this.set(DATE, date); + this.set(HOUR_OF_DAY, hour); + this.set(MINUTE, minute); + this.set(SECOND, second); + } + + /** + * Constructs a ChineseCalendar based on the current time + * in the default time zone with the given locale. + * @param aLocale The given locale + * @draft ICU 4.0 + * @provisional This API might change or be removed in a future release. + */ + public ChineseCalendar(Locale aLocale) { + this(TimeZone.getDefault(), aLocale); setTimeInMillis(System.currentTimeMillis()); } /** - * Construct a Chinese calendar with the given time zone and locale. - * @param zone time zone for this calendar - * @param locale ulocale for this calendar + * Construct a ChineseCalendar based on the current time + * in the given time zone with the default locale. + * @param zone the given time zone + * @draft ICU 4.0 + * @provisional This API might change or be removed in a future release. + */ + public ChineseCalendar(TimeZone zone) { + super(zone, ULocale.getDefault()); + setTimeInMillis(System.currentTimeMillis()); + } + + /** + * Construct a ChineseCalendar based on the current time + * in the given time zone with the given locale. + * @param zone the given time zone + * @param aLocale the given locale + * @stable ICU 2.8 + */ + public ChineseCalendar(TimeZone zone, Locale aLocale) { + super(zone, aLocale); + setTimeInMillis(System.currentTimeMillis()); + } + + /** + * Constructs a ChineseCalendar based on the current time + * in the default time zone with the given locale. + * + * @param locale the given ulocale + * @draft ICU 4.0 + * @provisional This API might change or be removed in a future release. + */ + public ChineseCalendar(ULocale locale) { + this(TimeZone.getDefault(), locale); + setTimeInMillis(System.currentTimeMillis()); + } + + /** + * Construct a ChineseCalendar based on the current time + * with the given time zone with the given locale. + * @param zone the given time zone + * @param locale the given ulocale * @stable ICU 3.2 */ public ChineseCalendar(TimeZone zone, ULocale locale) {