2 *******************************************************************************
3 * Copyright (C) 2002-2010, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
7 package com.ibm.icu.dev.test.calendar;
9 import java.text.ParseException;
10 import java.text.ParsePosition;
11 import java.util.Date;
12 import java.util.Locale;
14 import com.ibm.icu.impl.LocaleUtility;
15 import com.ibm.icu.text.DateFormat;
16 import com.ibm.icu.text.SimpleDateFormat;
17 import com.ibm.icu.util.Calendar;
18 import com.ibm.icu.util.JapaneseCalendar;
19 import com.ibm.icu.util.TimeZone;
20 import com.ibm.icu.util.ULocale;
23 * Tests for the <code>JapaneseCalendar</code> class.
25 public class JapaneseTest extends CalendarTest {
26 public static void main(String args[]) throws Exception {
27 new JapaneseTest().run(args);
30 public void TestCoverage() {
32 // new JapaneseCalendar(TimeZone)
33 JapaneseCalendar cal = new JapaneseCalendar(TimeZone.getDefault());
35 errln("could not create JapaneseCalendar with TimeZone");
40 // new JapaneseCalendar(ULocale)
41 JapaneseCalendar cal = new JapaneseCalendar(ULocale.getDefault());
43 errln("could not create JapaneseCalendar with ULocale");
48 // new JapaneseCalendar(TimeZone, ULocale)
49 JapaneseCalendar cal = new JapaneseCalendar(TimeZone.getDefault(), ULocale.getDefault());
51 errln("could not create JapaneseCalendar with TimeZone ULocale");
56 // new JapaneseCalendar(Locale)
57 JapaneseCalendar cal = new JapaneseCalendar(Locale.getDefault());
59 errln("could not create JapaneseCalendar with Locale");
64 // new JapaneseCalendar(TimeZone, Locale)
65 JapaneseCalendar cal = new JapaneseCalendar(TimeZone.getDefault(), Locale.getDefault());
67 errln("could not create JapaneseCalendar with TimeZone Locale");
72 // new JapaneseCalendar(Date)
73 JapaneseCalendar cal = new JapaneseCalendar(new Date());
75 errln("could not create JapaneseCalendar with Date");
80 // new JapaneseCalendar(int year, int month, int date)
81 JapaneseCalendar cal = new JapaneseCalendar(1868, Calendar.JANUARY, 1);
83 errln("could not create JapaneseCalendar with year,month,date");
88 // new JapaneseCalendar(int era, int year, int month, int date)
89 JapaneseCalendar cal = new JapaneseCalendar(JapaneseCalendar.MEIJI, 43, Calendar.JANUARY, 1);
91 errln("could not create JapaneseCalendar with era,year,month,date");
96 // new JapaneseCalendar(int year, int month, int date, int hour, int minute, int second)
97 JapaneseCalendar cal = new JapaneseCalendar(1868, Calendar.JANUARY, 1, 1, 1, 1);
99 errln("could not create JapaneseCalendar with year,month,date,hour,min,second");
105 JapaneseCalendar cal = new JapaneseCalendar();
106 DateFormat fmt = cal.getDateTimeFormat(DateFormat.FULL, DateFormat.FULL, Locale.ENGLISH);
108 cal.set(Calendar.ERA, JapaneseCalendar.MEIJI);
109 logln("date: " + cal.getTime());
110 logln("min era: " + cal.getMinimum(Calendar.ERA));
111 logln("min year: " + cal.getMinimum(Calendar.YEAR));
112 cal.set(Calendar.YEAR, cal.getActualMaximum(Calendar.YEAR));
113 logln("date: " + fmt.format(cal.getTime()));
114 cal.add(Calendar.YEAR, 1);
115 logln("date: " + fmt.format(cal.getTime()));
120 JapaneseCalendar cal = new JapaneseCalendar(1868, Calendar.JANUARY, 1);
121 Date time = cal.getTime();
123 String[] calendarLocales = {
127 String[] formatLocales = {
130 for (int i = 0; i < calendarLocales.length; ++i) {
131 String calLocName = calendarLocales[i];
132 Locale calLocale = LocaleUtility.getLocaleFromName(calLocName);
133 cal = new JapaneseCalendar(calLocale);
135 for (int j = 0; j < formatLocales.length; ++j) {
136 String locName = formatLocales[j];
137 Locale formatLocale = LocaleUtility.getLocaleFromName(locName);
138 DateFormat format = DateFormat.getDateTimeInstance(cal, DateFormat.FULL, DateFormat.FULL, formatLocale);
139 logln(calLocName + "/" + locName + " --> " + format.format(time));
145 public void Test3860()
147 ULocale loc = new ULocale("ja_JP@calendar=japanese");
148 Calendar cal = new JapaneseCalendar(loc);
149 DateFormat enjformat = cal.getDateTimeFormat(0,0,new ULocale("en_JP@calendar=japanese"));
150 DateFormat format = cal.getDateTimeFormat(0,0,loc);
151 ((SimpleDateFormat)format).applyPattern("y.M.d"); // Note: just 'y' doesn't work here.
152 ParsePosition pos = new ParsePosition(0);
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
154 String inEn = enjformat.format(aDate);
158 int gotYear = cal.get(Calendar.YEAR);
159 int gotEra = cal.get(Calendar.ERA);
162 int expectEra = JapaneseCalendar.CURRENT_ERA;
164 if((gotYear != expectYear) || (gotEra != expectEra)) {
165 errln("Expected year " + expectYear + ", era " + expectEra +", but got year " + gotYear + " and era " + gotEra + ", == " + inEn);
167 logln("Got year " + gotYear + " and era " + gotEra + ", == " + inEn);
170 // Test parse with missing era (should default to current era, heisei)
171 // Test parse with incomplete information
172 logln("Testing parse w/ just year...");
173 Calendar cal2 = new JapaneseCalendar(loc);
174 SimpleDateFormat fmt = new SimpleDateFormat("y", loc);
175 SimpleDateFormat fmt2 = new SimpleDateFormat("HH:mm:ss.S MMMM d, yyyy G", new ULocale("en_US@calendar=gregorian"));
177 String samplestr = "1";
178 logln("Test Year: " + samplestr);
180 aDate = fmt.parse(samplestr);
181 } catch (ParseException pe) {
182 errln("Error parsing " + samplestr);
184 ParsePosition pp = new ParsePosition(0);
185 fmt.parse(samplestr, cal2, pp);
186 logln("cal2 after 1 parse:");
187 String str = fmt2.format(aDate);
188 logln("as Gregorian Calendar: " + str);
191 gotYear = cal2.get(Calendar.YEAR);
192 gotEra = cal2.get(Calendar.ERA);
194 expectEra = JapaneseCalendar.CURRENT_ERA;
195 if((gotYear != 1) || (gotEra != expectEra)) {
196 errln("parse "+ samplestr + " of 'y' as Japanese Calendar, expected year " + expectYear +
197 " and era " + expectEra + ", but got year " + gotYear + " and era " + gotEra + " (Gregorian:" + str +")");
199 logln(" year: " + gotYear + ", era: " + gotEra);
203 public void Test5345parse() {
204 // Test parse with incomplete information
205 DateFormat fmt2= DateFormat.getDateInstance(); //DateFormat.LONG, Locale.US);
206 JapaneseCalendar c = new JapaneseCalendar(TimeZone.getDefault(), new ULocale("en_US"));
207 SimpleDateFormat fmt = (SimpleDateFormat)c.getDateTimeFormat(1,1,new ULocale("en_US@calendar=japanese"));
208 fmt.applyPattern("G y");
209 logln("fmt's locale = " + fmt.getLocale(ULocale.ACTUAL_LOCALE));
210 //SimpleDateFormat fmt = new SimpleDateFormat("G y", new Locale("en_US@calendar=japanese"));
211 long aDateLong = -3197117222000L; // 1868-09-08 00:00 Pacific Time (GMT-07:52:58)
212 if (TimeZone.getDefaultTimeZoneType() == TimeZone.TIMEZONE_JDK) {
213 // Java time zone implementation does not support LMTs
214 aDateLong = -3197116800000L; // 1868-09-08 00:00 Pacific Time (GMT-08:00)
216 Date aDate = new Date(aDateLong);
217 logln("aDate: " + aDate.toString() +", from " + aDateLong);
219 str = fmt2.format(aDate);
220 logln("Test Date: " + str);
221 str = fmt.format(aDate);
222 logln("as Japanese Calendar: " + str);
223 String expected = "Meiji 1";
224 if(!str.equals(expected)) {
225 errln("FAIL: Expected " + expected + " but got " + str);
229 otherDate = fmt.parse(expected);
230 if(!otherDate.equals(aDate)) {
233 Date dd = fmt.parse(expected);
234 str3 = fmt.format(otherDate);
235 long oLong = otherDate.getTime();
236 long aLong = otherDate.getTime();
238 errln("FAIL: Parse incorrect of " + expected + ": wanted " + aDate + " ("+aLong+"), but got " + " " +
239 otherDate + " ("+oLong+") = " + str3 + " not " + dd.toString() );
243 logln("Parsed OK: " + expected);
245 } catch(java.text.ParseException pe) {
246 errln("FAIL: ParseException: " + pe.toString());
247 pe.printStackTrace();
252 private void checkExpected(Calendar c, int expected[] ) {
253 final String[] FIELD_NAME = {
254 "ERA", "YEAR", "MONTH", "WEEK_OF_YEAR", "WEEK_OF_MONTH",
255 "DAY_OF_MONTH", "DAY_OF_YEAR", "DAY_OF_WEEK",
256 "DAY_OF_WEEK_IN_MONTH", "AM_PM", "HOUR", "HOUR_OF_DAY",
257 "MINUTE", "SECOND", "MILLISECOND", "ZONE_OFFSET",
258 "DST_OFFSET", "YEAR_WOY", "DOW_LOCAL", "EXTENDED_YEAR",
259 "JULIAN_DAY", "MILLISECONDS_IN_DAY",
262 for(int i= 0;i<expected.length;i += 2) {
263 int fieldNum = expected[i+0];
264 int expectedVal = expected[i+1];
265 int actualVal = c.get(fieldNum);
267 if(expectedVal == actualVal) {
268 logln(FIELD_NAME[fieldNum]+": "+ actualVal);
270 errln("FAIL: "+FIELD_NAME[fieldNum]+": expected "+ expectedVal + " got " + actualVal);
275 public void Test5345calendar() {
276 logln("** testIncompleteCalendar()");
277 // Test calendar with incomplete information
278 JapaneseCalendar c = new JapaneseCalendar(TimeZone.getDefault());
282 // Showa 45 = Gregorian 1970
283 int expected0[] = { Calendar.ERA, 234,
285 checkExpected(c, expected0);
287 logln("test setting era");
289 c.set(Calendar.ERA, JapaneseCalendar.MEIJI);
292 int expectedA[] = { Calendar.ERA, JapaneseCalendar.MEIJI };
293 checkExpected(c, expectedA);
296 logln("test setting era and year and month and date");
298 c.set(Calendar.ERA, JapaneseCalendar.MEIJI);
299 c.set(Calendar.YEAR, 1);
300 c.set(Calendar.MONTH, Calendar.JANUARY);
301 c.set(Calendar.DATE, 1);
304 int expectedC[] = { Calendar.ERA, JapaneseCalendar.MEIJI -1};
305 checkExpected(c, expectedC);
308 logln("test setting year and month and date THEN era");
310 c.set(Calendar.YEAR, 1);
311 c.set(Calendar.MONTH, Calendar.JANUARY);
312 c.set(Calendar.DATE, 1);
313 c.set(Calendar.ERA, JapaneseCalendar.MEIJI);
316 checkExpected(c, expectedC);
319 logln("test setting era and year");
321 c.set(Calendar.YEAR, 1);
322 c.set(Calendar.ERA, JapaneseCalendar.MEIJI);
325 int expectedB[] = { Calendar.ERA, JapaneseCalendar.MEIJI,
327 checkExpected(c, expectedB);
332 public void TestJapaneseYear3282() {
333 Calendar c = Calendar.getInstance(ULocale.ENGLISH);
334 c.set(2003,Calendar.SEPTEMBER,25);
335 JapaneseCalendar jcal = new JapaneseCalendar();
336 //jcal.setTime(new Date(1187906308151L)); alternate value
337 jcal.setTime(c.getTime());
338 logln("Now is: " + jcal.getTime());
339 c.setTime(jcal.getTime());
340 int nowYear = c.get(Calendar.YEAR);
341 logln("Now year: "+nowYear);
342 SimpleDateFormat jdf = (SimpleDateFormat) SimpleDateFormat.getDateInstance(jcal,
343 SimpleDateFormat.DEFAULT, Locale.getDefault());
344 jdf.applyPattern("G yy/MM/dd");
345 String text = jdf.format(jcal.getTime());
346 logln("Now is: " + text + " (in Japan)");
348 Date date = jdf.parse(text);
349 logln("But is this not the date?: " + date);
351 int thenYear = c.get(Calendar.YEAR);
352 logln("Then year: "+thenYear);
353 if(thenYear != nowYear) {
354 errln("Nowyear "+nowYear +" is not thenyear "+thenYear);
356 logln("Nowyear "+nowYear +" == thenyear "+thenYear);
358 } catch (java.text.ParseException ex) {
359 ex.printStackTrace();
364 * Test limits of the Japanese calendar
366 public void TestLimits() {
367 Calendar cal = Calendar.getInstance();
368 cal.set(1988, Calendar.DECEMBER, 1);
369 JapaneseCalendar jcal = new JapaneseCalendar();
370 doLimitsTest(jcal, null, cal.getTime());
371 doTheoreticalLimitsTest(jcal, true);