]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-52_1/main/tests/core/src/com/ibm/icu/dev/test/calendar/JapaneseTest.java
Upgrade ICU4J.
[Dictionary.git] / jars / icu4j-52_1 / main / tests / core / src / com / ibm / icu / dev / test / calendar / JapaneseTest.java
1 /*
2  *******************************************************************************
3  * Copyright (C) 2002-2010, International Business Machines Corporation and    *
4  * others. All Rights Reserved.                                                *
5  *******************************************************************************
6  */
7 package com.ibm.icu.dev.test.calendar;
8
9 import java.text.ParseException;
10 import java.text.ParsePosition;
11 import java.util.Date;
12 import java.util.Locale;
13
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;
21
22 /**
23  * Tests for the <code>JapaneseCalendar</code> class.
24  */
25 public class JapaneseTest extends CalendarTest {
26     public static void main(String args[]) throws Exception {
27         new JapaneseTest().run(args);
28     }
29
30     public void TestCoverage() {
31     {
32         // new JapaneseCalendar(TimeZone)
33         JapaneseCalendar cal = new JapaneseCalendar(TimeZone.getDefault());
34         if(cal == null){
35             errln("could not create JapaneseCalendar with TimeZone");
36         }
37     }
38
39     {
40         // new JapaneseCalendar(ULocale)
41         JapaneseCalendar cal = new JapaneseCalendar(ULocale.getDefault());
42         if(cal == null){
43             errln("could not create JapaneseCalendar with ULocale");
44         }
45     }
46
47     {
48         // new JapaneseCalendar(TimeZone, ULocale)
49         JapaneseCalendar cal = new JapaneseCalendar(TimeZone.getDefault(), ULocale.getDefault());
50         if(cal == null){
51             errln("could not create JapaneseCalendar with TimeZone ULocale");
52         }
53     }
54
55     {
56         // new JapaneseCalendar(Locale)
57         JapaneseCalendar cal = new JapaneseCalendar(Locale.getDefault());
58         if(cal == null){
59             errln("could not create JapaneseCalendar with Locale");
60         }
61     }
62
63     {
64         // new JapaneseCalendar(TimeZone, Locale)
65         JapaneseCalendar cal = new JapaneseCalendar(TimeZone.getDefault(), Locale.getDefault());
66         if(cal == null){
67             errln("could not create JapaneseCalendar with TimeZone Locale");
68         }
69     }
70
71     {
72         // new JapaneseCalendar(Date)
73         JapaneseCalendar cal = new JapaneseCalendar(new Date());
74         if(cal == null){
75             errln("could not create JapaneseCalendar with Date");
76         }
77     }
78
79     {
80         // new JapaneseCalendar(int year, int month, int date)
81         JapaneseCalendar cal = new JapaneseCalendar(1868, Calendar.JANUARY, 1);
82         if(cal == null){
83             errln("could not create JapaneseCalendar with year,month,date");
84         }
85     }
86
87     {
88         // new JapaneseCalendar(int era, int year, int month, int date)
89         JapaneseCalendar cal = new JapaneseCalendar(JapaneseCalendar.MEIJI, 43, Calendar.JANUARY, 1);
90         if(cal == null){
91             errln("could not create JapaneseCalendar with era,year,month,date");
92         }
93     }
94
95     {
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);
98         if(cal == null){
99             errln("could not create JapaneseCalendar with year,month,date,hour,min,second");
100         }
101     }
102
103     {
104         // limits
105         JapaneseCalendar cal = new JapaneseCalendar();
106         DateFormat fmt = cal.getDateTimeFormat(DateFormat.FULL, DateFormat.FULL, Locale.ENGLISH);
107
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()));
116     }
117
118     {
119         // data
120         JapaneseCalendar cal = new JapaneseCalendar(1868, Calendar.JANUARY, 1);
121         Date time = cal.getTime();
122
123         String[] calendarLocales = {
124         "en", "ja_JP"
125         };
126
127         String[] formatLocales = {
128         "en", "ja"
129         };
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);
134
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));
140             }
141         }
142     }
143     }
144     
145     public void Test3860()
146     {
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);
155
156         cal.clear();
157         cal.setTime(aDate);
158         int gotYear = cal.get(Calendar.YEAR);
159         int gotEra = cal.get(Calendar.ERA);
160         
161         int expectYear = 1;
162         int expectEra = JapaneseCalendar.CURRENT_ERA;
163         
164         if((gotYear != expectYear) || (gotEra != expectEra)) {
165             errln("Expected year " + expectYear + ", era " + expectEra +", but got year " + gotYear + " and era " + gotEra + ", == " + inEn);
166         } else {
167             logln("Got year " + gotYear + " and era " + gotEra + ", == " + inEn);
168         }
169
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"));
176         cal2.clear();
177         String samplestr = "1";
178         logln("Test Year: " + samplestr);
179         try {
180             aDate = fmt.parse(samplestr);
181         } catch (ParseException pe) {
182             errln("Error parsing " + samplestr);
183         }
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);
189
190         cal2.setTime(aDate);
191         gotYear = cal2.get(Calendar.YEAR);
192         gotEra = cal2.get(Calendar.ERA);
193         expectYear = 1;
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 +")");
198         } else {            
199             logln(" year: " + gotYear + ", era: " + gotEra);
200         }
201     }
202
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)
215         }
216         Date aDate = new Date(aDateLong);
217         logln("aDate: " + aDate.toString() +", from " + aDateLong);
218         String str;
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);
226         }
227         Date otherDate;
228         try {
229             otherDate = fmt.parse(expected);
230             if(!otherDate.equals(aDate)) { 
231                 String str3;
232     //            ParsePosition pp;
233                 Date dd = fmt.parse(expected);
234                 str3 = fmt.format(otherDate);
235                 long oLong = otherDate.getTime();
236                 long aLong = otherDate.getTime();
237                 
238                 errln("FAIL: Parse incorrect of " + expected + ":  wanted " + aDate + " ("+aLong+"), but got " +  " " +
239                     otherDate + " ("+oLong+") = " + str3 + " not " + dd.toString() );
240
241
242             } else {
243                 logln("Parsed OK: " + expected);
244             }
245         } catch(java.text.ParseException pe) {
246             errln("FAIL: ParseException: " + pe.toString());
247             pe.printStackTrace();
248         }
249     }
250
251
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",
260         };
261
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);
266             
267             if(expectedVal == actualVal) {
268                 logln(FIELD_NAME[fieldNum]+": "+ actualVal);
269             } else {
270                 errln("FAIL: "+FIELD_NAME[fieldNum]+": expected "+ expectedVal + " got " +  actualVal);
271             }
272         }
273     }
274
275     public void Test5345calendar() {
276         logln("** testIncompleteCalendar()");
277         // Test calendar with incomplete information
278         JapaneseCalendar c = new JapaneseCalendar(TimeZone.getDefault());
279         logln("test clear");
280         c.clear();
281  
282         // Showa 45 = Gregorian 1970
283         int expected0[] = {   Calendar.ERA, 234,
284                               Calendar.YEAR, 45 };
285         checkExpected(c, expected0);
286
287         logln("test setting era");
288         c.clear();
289         c.set(Calendar.ERA, JapaneseCalendar.MEIJI);
290         
291         
292         int expectedA[] = {   Calendar.ERA, JapaneseCalendar.MEIJI };
293         checkExpected(c, expectedA);
294
295
296         logln("test setting era and year and month and date");
297         c.clear();
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);
302
303
304         int expectedC[] = {   Calendar.ERA, JapaneseCalendar.MEIJI -1};
305         checkExpected(c, expectedC);
306
307
308         logln("test setting  year and month and date THEN era");
309         c.clear();
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);
314         
315         
316         checkExpected(c, expectedC);
317         
318         
319         logln("test setting era and year");
320         c.clear();
321         c.set(Calendar.YEAR, 1);
322         c.set(Calendar.ERA, JapaneseCalendar.MEIJI);
323
324
325         int expectedB[] = { Calendar.ERA, JapaneseCalendar.MEIJI,
326                             Calendar.YEAR, 1 };
327         checkExpected(c, expectedB);
328
329     }
330
331     
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)");
347         try {
348             Date date = jdf.parse(text);
349             logln("But is this not the date?: " + date);
350             c.setTime(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);
355             } else {
356                 logln("Nowyear "+nowYear +" == thenyear "+thenYear);
357             }
358         } catch (java.text.ParseException ex) {
359             ex.printStackTrace();
360         }
361     }
362
363     /**
364      * Test limits of the Japanese calendar
365      */
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);
372     }
373 }
374