2 *******************************************************************************
\r
3 * Copyright (C) 2002-2010, International Business Machines Corporation and *
\r
4 * others. All Rights Reserved. *
\r
5 *******************************************************************************
\r
7 package com.ibm.icu.dev.test.calendar;
\r
9 import java.text.ParseException;
\r
10 import java.text.ParsePosition;
\r
11 import java.util.Date;
\r
12 import java.util.Locale;
\r
14 import com.ibm.icu.impl.LocaleUtility;
\r
15 import com.ibm.icu.text.DateFormat;
\r
16 import com.ibm.icu.text.SimpleDateFormat;
\r
17 import com.ibm.icu.util.Calendar;
\r
18 import com.ibm.icu.util.JapaneseCalendar;
\r
19 import com.ibm.icu.util.TimeZone;
\r
20 import com.ibm.icu.util.ULocale;
\r
23 * Tests for the <code>JapaneseCalendar</code> class.
\r
25 public class JapaneseTest extends CalendarTest {
\r
26 public static void main(String args[]) throws Exception {
\r
27 new JapaneseTest().run(args);
\r
30 public void TestCoverage() {
\r
32 // new JapaneseCalendar(TimeZone)
\r
33 JapaneseCalendar cal = new JapaneseCalendar(TimeZone.getDefault());
\r
35 errln("could not create JapaneseCalendar with TimeZone");
\r
40 // new JapaneseCalendar(ULocale)
\r
41 JapaneseCalendar cal = new JapaneseCalendar(ULocale.getDefault());
\r
43 errln("could not create JapaneseCalendar with ULocale");
\r
48 // new JapaneseCalendar(TimeZone, ULocale)
\r
49 JapaneseCalendar cal = new JapaneseCalendar(TimeZone.getDefault(), ULocale.getDefault());
\r
51 errln("could not create JapaneseCalendar with TimeZone ULocale");
\r
56 // new JapaneseCalendar(Locale)
\r
57 JapaneseCalendar cal = new JapaneseCalendar(Locale.getDefault());
\r
59 errln("could not create JapaneseCalendar with Locale");
\r
64 // new JapaneseCalendar(TimeZone, Locale)
\r
65 JapaneseCalendar cal = new JapaneseCalendar(TimeZone.getDefault(), Locale.getDefault());
\r
67 errln("could not create JapaneseCalendar with TimeZone Locale");
\r
72 // new JapaneseCalendar(Date)
\r
73 JapaneseCalendar cal = new JapaneseCalendar(new Date());
\r
75 errln("could not create JapaneseCalendar with Date");
\r
80 // new JapaneseCalendar(int year, int month, int date)
\r
81 JapaneseCalendar cal = new JapaneseCalendar(1868, Calendar.JANUARY, 1);
\r
83 errln("could not create JapaneseCalendar with year,month,date");
\r
88 // new JapaneseCalendar(int era, int year, int month, int date)
\r
89 JapaneseCalendar cal = new JapaneseCalendar(JapaneseCalendar.MEIJI, 43, Calendar.JANUARY, 1);
\r
91 errln("could not create JapaneseCalendar with era,year,month,date");
\r
96 // new JapaneseCalendar(int year, int month, int date, int hour, int minute, int second)
\r
97 JapaneseCalendar cal = new JapaneseCalendar(1868, Calendar.JANUARY, 1, 1, 1, 1);
\r
99 errln("could not create JapaneseCalendar with year,month,date,hour,min,second");
\r
105 JapaneseCalendar cal = new JapaneseCalendar();
\r
106 DateFormat fmt = cal.getDateTimeFormat(DateFormat.FULL, DateFormat.FULL, Locale.ENGLISH);
\r
108 cal.set(Calendar.ERA, JapaneseCalendar.MEIJI);
\r
109 logln("date: " + cal.getTime());
\r
110 logln("min era: " + cal.getMinimum(Calendar.ERA));
\r
111 logln("min year: " + cal.getMinimum(Calendar.YEAR));
\r
112 cal.set(Calendar.YEAR, cal.getActualMaximum(Calendar.YEAR));
\r
113 logln("date: " + fmt.format(cal.getTime()));
\r
114 cal.add(Calendar.YEAR, 1);
\r
115 logln("date: " + fmt.format(cal.getTime()));
\r
120 JapaneseCalendar cal = new JapaneseCalendar(1868, Calendar.JANUARY, 1);
\r
121 Date time = cal.getTime();
\r
123 String[] calendarLocales = {
\r
127 String[] formatLocales = {
\r
130 for (int i = 0; i < calendarLocales.length; ++i) {
\r
131 String calLocName = calendarLocales[i];
\r
132 Locale calLocale = LocaleUtility.getLocaleFromName(calLocName);
\r
133 cal = new JapaneseCalendar(calLocale);
\r
135 for (int j = 0; j < formatLocales.length; ++j) {
\r
136 String locName = formatLocales[j];
\r
137 Locale formatLocale = LocaleUtility.getLocaleFromName(locName);
\r
138 DateFormat format = DateFormat.getDateTimeInstance(cal, DateFormat.FULL, DateFormat.FULL, formatLocale);
\r
139 logln(calLocName + "/" + locName + " --> " + format.format(time));
\r
145 public void Test3860()
\r
147 ULocale loc = new ULocale("ja_JP@calendar=japanese");
\r
148 Calendar cal = new JapaneseCalendar(loc);
\r
149 DateFormat enjformat = cal.getDateTimeFormat(0,0,new ULocale("en_JP@calendar=japanese"));
\r
150 DateFormat format = cal.getDateTimeFormat(0,0,loc);
\r
151 ((SimpleDateFormat)format).applyPattern("y.M.d"); // Note: just 'y' doesn't work here.
\r
152 ParsePosition pos = new ParsePosition(0);
\r
153 Date aDate = format.parse("1.1.9", pos); // after the start of heisei accession. Jan 1, 1H wouldn't work because it is actually showa 64
\r
154 String inEn = enjformat.format(aDate);
\r
157 cal.setTime(aDate);
\r
158 int gotYear = cal.get(Calendar.YEAR);
\r
159 int gotEra = cal.get(Calendar.ERA);
\r
161 int expectYear = 1;
\r
162 int expectEra = JapaneseCalendar.CURRENT_ERA;
\r
164 if((gotYear != expectYear) || (gotEra != expectEra)) {
\r
165 errln("Expected year " + expectYear + ", era " + expectEra +", but got year " + gotYear + " and era " + gotEra + ", == " + inEn);
\r
167 logln("Got year " + gotYear + " and era " + gotEra + ", == " + inEn);
\r
170 // Test parse with missing era (should default to current era, heisei)
\r
171 // Test parse with incomplete information
\r
172 logln("Testing parse w/ just year...");
\r
173 Calendar cal2 = new JapaneseCalendar(loc);
\r
174 SimpleDateFormat fmt = new SimpleDateFormat("y", loc);
\r
175 SimpleDateFormat fmt2 = new SimpleDateFormat("HH:mm:ss.S MMMM d, yyyy G", new ULocale("en_US@calendar=gregorian"));
\r
177 String samplestr = "1";
\r
178 logln("Test Year: " + samplestr);
\r
180 aDate = fmt.parse(samplestr);
\r
181 } catch (ParseException pe) {
\r
182 errln("Error parsing " + samplestr);
\r
184 ParsePosition pp = new ParsePosition(0);
\r
185 fmt.parse(samplestr, cal2, pp);
\r
186 logln("cal2 after 1 parse:");
\r
187 String str = fmt2.format(aDate);
\r
188 logln("as Gregorian Calendar: " + str);
\r
190 cal2.setTime(aDate);
\r
191 gotYear = cal2.get(Calendar.YEAR);
\r
192 gotEra = cal2.get(Calendar.ERA);
\r
194 expectEra = JapaneseCalendar.CURRENT_ERA;
\r
195 if((gotYear != 1) || (gotEra != expectEra)) {
\r
196 errln("parse "+ samplestr + " of 'y' as Japanese Calendar, expected year " + expectYear +
\r
197 " and era " + expectEra + ", but got year " + gotYear + " and era " + gotEra + " (Gregorian:" + str +")");
\r
199 logln(" year: " + gotYear + ", era: " + gotEra);
\r
203 public void Test5345parse() {
\r
204 // Test parse with incomplete information
\r
205 DateFormat fmt2= DateFormat.getDateInstance(); //DateFormat.LONG, Locale.US);
\r
206 JapaneseCalendar c = new JapaneseCalendar(TimeZone.getDefault(), new ULocale("en_US"));
\r
207 SimpleDateFormat fmt = (SimpleDateFormat)c.getDateTimeFormat(1,1,new ULocale("en_US@calendar=japanese"));
\r
208 fmt.applyPattern("G y");
\r
209 logln("fmt's locale = " + fmt.getLocale(ULocale.ACTUAL_LOCALE));
\r
210 //SimpleDateFormat fmt = new SimpleDateFormat("G y", new Locale("en_US@calendar=japanese"));
\r
211 long aDateLong = -3197117222000L;
\r
212 Date aDate = new Date(aDateLong); //08 Sept 1868
\r
213 logln("aDate: " + aDate.toString() +", from " + aDateLong);
\r
215 str = fmt2.format(aDate);
\r
216 logln("Test Date: " + str);
\r
217 str = fmt.format(aDate);
\r
218 logln("as Japanese Calendar: " + str);
\r
219 String expected = "Meiji 1";
\r
220 if(!str.equals(expected)) {
\r
221 errln("FAIL: Expected " + expected + " but got " + str);
\r
225 otherDate = fmt.parse(expected);
\r
226 if(!otherDate.equals(aDate)) {
\r
228 // ParsePosition pp;
\r
229 Date dd = fmt.parse(expected);
\r
230 str3 = fmt.format(otherDate);
\r
231 long oLong = otherDate.getTime();
\r
232 long aLong = otherDate.getTime();
\r
234 errln("FAIL: Parse incorrect of " + expected + ": wanted " + aDate + " ("+aLong+"), but got " + " " +
\r
235 otherDate + " ("+oLong+") = " + str3 + " not " + dd.toString() );
\r
239 logln("Parsed OK: " + expected);
\r
241 } catch(java.text.ParseException pe) {
\r
242 errln("FAIL: ParseException: " + pe.toString());
\r
243 pe.printStackTrace();
\r
248 private void checkExpected(Calendar c, int expected[] ) {
\r
249 final String[] FIELD_NAME = {
\r
250 "ERA", "YEAR", "MONTH", "WEEK_OF_YEAR", "WEEK_OF_MONTH",
\r
251 "DAY_OF_MONTH", "DAY_OF_YEAR", "DAY_OF_WEEK",
\r
252 "DAY_OF_WEEK_IN_MONTH", "AM_PM", "HOUR", "HOUR_OF_DAY",
\r
253 "MINUTE", "SECOND", "MILLISECOND", "ZONE_OFFSET",
\r
254 "DST_OFFSET", "YEAR_WOY", "DOW_LOCAL", "EXTENDED_YEAR",
\r
255 "JULIAN_DAY", "MILLISECONDS_IN_DAY",
\r
258 for(int i= 0;i<expected.length;i += 2) {
\r
259 int fieldNum = expected[i+0];
\r
260 int expectedVal = expected[i+1];
\r
261 int actualVal = c.get(fieldNum);
\r
263 if(expectedVal == actualVal) {
\r
264 logln(FIELD_NAME[fieldNum]+": "+ actualVal);
\r
266 errln("FAIL: "+FIELD_NAME[fieldNum]+": expected "+ expectedVal + " got " + actualVal);
\r
271 public void Test5345calendar() {
\r
272 logln("** testIncompleteCalendar()");
\r
273 // Test calendar with incomplete information
\r
274 JapaneseCalendar c = new JapaneseCalendar(TimeZone.getDefault());
\r
275 logln("test clear");
\r
278 // Showa 45 = Gregorian 1970
\r
279 int expected0[] = { Calendar.ERA, 234,
\r
280 Calendar.YEAR, 45 };
\r
281 checkExpected(c, expected0);
\r
283 logln("test setting era");
\r
285 c.set(Calendar.ERA, JapaneseCalendar.MEIJI);
\r
288 int expectedA[] = { Calendar.ERA, JapaneseCalendar.MEIJI };
\r
289 checkExpected(c, expectedA);
\r
292 logln("test setting era and year and month and date");
\r
294 c.set(Calendar.ERA, JapaneseCalendar.MEIJI);
\r
295 c.set(Calendar.YEAR, 1);
\r
296 c.set(Calendar.MONTH, Calendar.JANUARY);
\r
297 c.set(Calendar.DATE, 1);
\r
300 int expectedC[] = { Calendar.ERA, JapaneseCalendar.MEIJI -1};
\r
301 checkExpected(c, expectedC);
\r
304 logln("test setting year and month and date THEN era");
\r
306 c.set(Calendar.YEAR, 1);
\r
307 c.set(Calendar.MONTH, Calendar.JANUARY);
\r
308 c.set(Calendar.DATE, 1);
\r
309 c.set(Calendar.ERA, JapaneseCalendar.MEIJI);
\r
312 checkExpected(c, expectedC);
\r
315 logln("test setting era and year");
\r
317 c.set(Calendar.YEAR, 1);
\r
318 c.set(Calendar.ERA, JapaneseCalendar.MEIJI);
\r
321 int expectedB[] = { Calendar.ERA, JapaneseCalendar.MEIJI,
\r
322 Calendar.YEAR, 1 };
\r
323 checkExpected(c, expectedB);
\r
328 public void TestJapaneseYear3282() {
\r
329 Calendar c = Calendar.getInstance(ULocale.ENGLISH);
\r
330 c.set(2003,Calendar.SEPTEMBER,25);
\r
331 JapaneseCalendar jcal = new JapaneseCalendar();
\r
332 //jcal.setTime(new Date(1187906308151L)); alternate value
\r
333 jcal.setTime(c.getTime());
\r
334 logln("Now is: " + jcal.getTime());
\r
335 c.setTime(jcal.getTime());
\r
336 int nowYear = c.get(Calendar.YEAR);
\r
337 logln("Now year: "+nowYear);
\r
338 SimpleDateFormat jdf = (SimpleDateFormat) SimpleDateFormat.getDateInstance(jcal,
\r
339 SimpleDateFormat.DEFAULT, Locale.getDefault());
\r
340 jdf.applyPattern("G yy/MM/dd");
\r
341 String text = jdf.format(jcal.getTime());
\r
342 logln("Now is: " + text + " (in Japan)");
\r
344 Date date = jdf.parse(text);
\r
345 logln("But is this not the date?: " + date);
\r
347 int thenYear = c.get(Calendar.YEAR);
\r
348 logln("Then year: "+thenYear);
\r
349 if(thenYear != nowYear) {
\r
350 errln("Nowyear "+nowYear +" is not thenyear "+thenYear);
\r
352 logln("Nowyear "+nowYear +" == thenyear "+thenYear);
\r
354 } catch (java.text.ParseException ex) {
\r
355 ex.printStackTrace();
\r
360 * Test limits of the Japanese calendar
\r
362 public void TestLimits() {
\r
363 Calendar cal = Calendar.getInstance();
\r
364 cal.set(1988, Calendar.DECEMBER, 1);
\r
365 JapaneseCalendar jcal = new JapaneseCalendar();
\r
366 doLimitsTest(jcal, null, cal.getTime());
\r
367 doTheoreticalLimitsTest(jcal, true);
\r