]> granicus.if.org Git - icu/blob
0136a82ab9e159f30a033d920f9ff0d40d0931a9
[icu] /
1 /*
2  *******************************************************************************
3  * Copyright (C) 2006-2011, International Business Machines Corporation and    *
4  * others. All Rights Reserved.                                                *
5  *******************************************************************************
6  */
7
8 package com.ibm.icu.tests;
9
10 import java.text.FieldPosition;
11 import java.text.ParseException;
12 import java.text.ParsePosition;
13 import java.util.Date;
14 import java.util.Locale;
15
16 import com.ibm.icu.text.DateFormatSymbols;
17 import com.ibm.icu.text.SimpleDateFormat;
18 import com.ibm.icu.util.Calendar;
19 import com.ibm.icu.util.TimeZone;
20 import com.ibm.icu.util.ULocale;
21
22 public class SimpleDateFormatTest extends ICUTestCase {
23     private static final String mdy = "MMM dd yyyy";
24     private static final String md2 = "MMM dd yy";
25     private static final String hmz = "'The time is' HH:mm:ss zzz";
26     private static final String hmzmdy = hmz + " 'on' " + mdy;
27     private static final String hmzmdyStr = "The time is 15:05:20 CST on Jan 10 2006";
28         
29     private static final TimeZone tzc = TimeZone.getTimeZone("CST");
30     private static final TimeZone tzp = TimeZone.getTimeZone("PST");
31     private static final Calendar cal = Calendar.getInstance(tzc);
32     private static final Date date;
33     static {
34         cal.clear();
35         cal.set(2006, 0, 10, 15, 5, 20); // arrgh, doesn't clear millis
36         date = cal.getTime();
37     }
38
39     /*
40      * Test method for 'com.ibm.icu.text.SimpleDateFormat.format(Calendar, StringBuffer, FieldPosition)'
41      */
42     public void testFormatCalendarStringBufferFieldPosition() {
43         StringBuffer buf = new StringBuffer();
44         FieldPosition fp = new FieldPosition(0);
45         SimpleDateFormat sdf = new SimpleDateFormat(hmzmdy);
46         sdf.format(cal, buf, fp);
47         assertEquals(hmzmdyStr, buf.toString());
48     }
49
50     /*
51      * Test method for 'com.ibm.icu.text.SimpleDateFormat.parse(String, Calendar, ParsePosition)'
52      */
53     public void testParseStringCalendarParsePosition() {
54         Calendar cal = Calendar.getInstance(tzp);
55         cal.clear();
56         ParsePosition pp = new ParsePosition(0);
57         SimpleDateFormat sdf = new SimpleDateFormat(hmzmdy);
58         sdf.parse(hmzmdyStr, cal, pp);
59         assertEquals(date, cal.getTime());
60         // note: java doesn't return the parsed time zone
61     }
62
63     /*
64      * Test method for 'com.ibm.icu.text.SimpleDateFormat.clone()'
65      */
66     public void testClone() {
67
68     }
69
70     /*
71      * Test method for 'com.ibm.icu.text.SimpleDateFormat.SimpleDateFormat()'
72      */
73     public void testSimpleDateFormat() {
74         SimpleDateFormat sdf = new SimpleDateFormat();
75         java.text.SimpleDateFormat jsdf = new java.text.SimpleDateFormat();
76         assertEquals(jsdf.format(date), sdf.format(date));
77     }
78
79     /*
80      * Test method for 'com.ibm.icu.text.SimpleDateFormat.SimpleDateFormat(String)'
81      */
82     public void testSimpleDateFormatString() {
83         SimpleDateFormat sdf = new SimpleDateFormat(mdy);
84         java.text.SimpleDateFormat jsdf = new java.text.SimpleDateFormat(mdy);
85         assertEquals(jsdf.format(date), sdf.format(date));
86     }
87
88     /*
89      * Test method for 'com.ibm.icu.text.SimpleDateFormat.SimpleDateFormat(String, Locale)'
90      */
91     public void testSimpleDateFormatStringLocale() {
92         Locale l = Locale.JAPAN;
93         SimpleDateFormat sdf = new SimpleDateFormat(mdy, l);
94         java.text.SimpleDateFormat jsdf = new java.text.SimpleDateFormat(mdy, l);
95         assertEquals(jsdf.format(date), sdf.format(date));
96     }
97
98     /*
99      * Test method for 'com.ibm.icu.text.SimpleDateFormat.SimpleDateFormat(String, ULocale)'
100      */
101     public void testSimpleDateFormatStringULocale() {
102         ULocale l = ULocale.JAPAN;
103         SimpleDateFormat sdf = new SimpleDateFormat(mdy, l);
104         java.text.SimpleDateFormat jsdf = new java.text.SimpleDateFormat(mdy, l.toLocale());
105         assertEquals(jsdf.format(date), sdf.format(date));
106     }
107
108     /*
109      * Test method for 'com.ibm.icu.text.SimpleDateFormat.SimpleDateFormat(String, DateFormatSymbols)'
110      */
111     public void testSimpleDateFormatStringDateFormatSymbols() {
112         Locale l = Locale.US;
113         DateFormatSymbols dfs = new DateFormatSymbols(l);
114         java.text.DateFormatSymbols jdfs = new java.text.DateFormatSymbols(l);
115         SimpleDateFormat sdf = new SimpleDateFormat(mdy, dfs);
116         java.text.SimpleDateFormat jsdf = new java.text.SimpleDateFormat(mdy, jdfs);
117         assertEquals(jsdf.format(date), sdf.format(date));
118     }
119
120     /*
121      * Test method for 'com.ibm.icu.text.SimpleDateFormat.set2DigitYearStart(Date)'
122      */
123     public void testSet2DigitYearStart() {
124         SimpleDateFormat sdf = new SimpleDateFormat(md2);
125         sdf.set2DigitYearStart(date);
126         try {
127             Date d = sdf.parse("Jan 15 04");
128             assertNotEqual(-1, d.toString().indexOf("2104"));
129         }
130         catch (ParseException pe) {
131             fail(pe.getMessage());
132         }
133     }
134
135     /*
136      * Test method for 'com.ibm.icu.text.SimpleDateFormat.get2DigitYearStart()'
137      */
138     public void testGet2DigitYearStart() {
139         SimpleDateFormat sdf = new SimpleDateFormat(md2);
140         sdf.set2DigitYearStart(date);
141         assertEquals(date, sdf.get2DigitYearStart());
142     }
143
144     /*
145      * Test method for 'com.ibm.icu.text.SimpleDateFormat.toPattern()'
146      */
147     public void testToPattern() {
148         SimpleDateFormat sdf = new SimpleDateFormat(mdy);
149         assertEquals(mdy, sdf.toPattern());
150     }
151
152     /*
153      * Test method for 'com.ibm.icu.text.SimpleDateFormat.toLocalizedPattern()'
154      */
155     public void testToLocalizedPattern() {
156         Locale l = Locale.getDefault();
157         Locale.setDefault(Locale.US);
158         SimpleDateFormat sdf = new SimpleDateFormat(mdy);
159         assertEquals(mdy, sdf.toLocalizedPattern());
160         Locale.setDefault(l);
161     }
162
163     /*
164      * Test method for 'com.ibm.icu.text.SimpleDateFormat.applyPattern(String)'
165      */
166     public void testApplyPattern() {
167         SimpleDateFormat sdf = new SimpleDateFormat();
168         sdf.setTimeZone(tzc);
169         sdf.applyPattern(hmzmdy);
170         assertEquals(hmzmdyStr, sdf.format(date));
171     }
172
173     /*
174      * Test method for 'com.ibm.icu.text.SimpleDateFormat.applyLocalizedPattern(String)'
175      */
176     public void testApplyLocalizedPattern() {
177         SimpleDateFormat sdf = new SimpleDateFormat();
178         sdf.setTimeZone(tzc);
179         sdf.applyLocalizedPattern(hmzmdy);
180         assertEquals(hmzmdyStr, sdf.format(date));
181     }
182
183     /*
184      * Test method for 'com.ibm.icu.text.SimpleDateFormat.getDateFormatSymbols()'
185      */
186     public void testGetDateFormatSymbols() {
187         DateFormatSymbols dfs = new DateFormatSymbols(Locale.US);
188         SimpleDateFormat sdf = new SimpleDateFormat(mdy, dfs);
189         assertEquals(dfs, sdf.getDateFormatSymbols());
190     }
191
192     /*
193      * Test method for 'com.ibm.icu.text.SimpleDateFormat.setDateFormatSymbols(DateFormatSymbols)'
194      */
195     public void testSetDateFormatSymbols() {
196         DateFormatSymbols dfs = new DateFormatSymbols(Locale.JAPAN);
197         SimpleDateFormat sdf = new SimpleDateFormat(hmzmdy);
198         sdf.setDateFormatSymbols(dfs);
199         // assumes Japanese symbols do not have gregorian month names
200         assertEquals(-1, sdf.format(date).indexOf("Jan"));
201     }
202 }