001: /*
002: *******************************************************************************
003: * Copyright (C) 2006, International Business Machines Corporation and *
004: * others. All Rights Reserved. *
005: *******************************************************************************
006: */
007:
008: package com.ibm.icu.tests;
009:
010: import java.text.FieldPosition;
011: import java.text.ParseException;
012: import java.text.ParsePosition;
013: import java.util.Date;
014: import java.util.Locale;
015:
016: import com.ibm.icu.text.DateFormatSymbols;
017: import com.ibm.icu.text.SimpleDateFormat;
018: import com.ibm.icu.util.Calendar;
019: import com.ibm.icu.util.TimeZone;
020: import com.ibm.icu.util.ULocale;
021:
022: public class SimpleDateFormatTest extends ICUTestCase {
023: private static final String mdy = "MMM dd yyyy";
024: private static final String md2 = "MMM dd yy";
025: private static final String hmz = "'The time is' HH:mm:ss zzz";
026: private static final String hmzmdy = hmz + " 'on' " + mdy;
027: private static final String hmzmdyStr = "The time is 15:05:20 CST on Jan 10 2006";
028:
029: private static final TimeZone tzc = TimeZone.getTimeZone("CST");
030: private static final TimeZone tzp = TimeZone.getTimeZone("PST");
031: private static final Calendar cal = Calendar.getInstance(tzc);
032: private static final Date date;
033: static {
034: cal.clear();
035: cal.set(2006, 0, 10, 15, 5, 20); // arrgh, doesn't clear millis
036: date = cal.getTime();
037: }
038:
039: /*
040: * Test method for 'com.ibm.icu.text.SimpleDateFormat.format(Calendar, StringBuffer, FieldPosition)'
041: */
042: public void testFormatCalendarStringBufferFieldPosition() {
043: StringBuffer buf = new StringBuffer();
044: FieldPosition fp = new FieldPosition(0);
045: SimpleDateFormat sdf = new SimpleDateFormat(hmzmdy);
046: sdf.format(cal, buf, fp);
047: assertEquals(hmzmdyStr, buf.toString());
048: }
049:
050: /*
051: * Test method for 'com.ibm.icu.text.SimpleDateFormat.parse(String, Calendar, ParsePosition)'
052: */
053: public void testParseStringCalendarParsePosition() {
054: Calendar cal = Calendar.getInstance(tzp);
055: cal.clear();
056: ParsePosition pp = new ParsePosition(0);
057: SimpleDateFormat sdf = new SimpleDateFormat(hmzmdy);
058: sdf.parse(hmzmdyStr, cal, pp);
059: assertEquals(date, cal.getTime());
060: // note: java doesn't return the parsed time zone
061: }
062:
063: /*
064: * Test method for 'com.ibm.icu.text.SimpleDateFormat.clone()'
065: */
066: public void testClone() {
067:
068: }
069:
070: /*
071: * Test method for 'com.ibm.icu.text.SimpleDateFormat.SimpleDateFormat()'
072: */
073: public void testSimpleDateFormat() {
074: SimpleDateFormat sdf = new SimpleDateFormat();
075: java.text.SimpleDateFormat jsdf = new java.text.SimpleDateFormat();
076: assertEquals(jsdf.format(date), sdf.format(date));
077: }
078:
079: /*
080: * Test method for 'com.ibm.icu.text.SimpleDateFormat.SimpleDateFormat(String)'
081: */
082: public void testSimpleDateFormatString() {
083: SimpleDateFormat sdf = new SimpleDateFormat(mdy);
084: java.text.SimpleDateFormat jsdf = new java.text.SimpleDateFormat(
085: mdy);
086: assertEquals(jsdf.format(date), sdf.format(date));
087: }
088:
089: /*
090: * Test method for 'com.ibm.icu.text.SimpleDateFormat.SimpleDateFormat(String, Locale)'
091: */
092: public void testSimpleDateFormatStringLocale() {
093: Locale l = Locale.JAPAN;
094: SimpleDateFormat sdf = new SimpleDateFormat(mdy, l);
095: java.text.SimpleDateFormat jsdf = new java.text.SimpleDateFormat(
096: mdy, l);
097: assertEquals(jsdf.format(date), sdf.format(date));
098: }
099:
100: /*
101: * Test method for 'com.ibm.icu.text.SimpleDateFormat.SimpleDateFormat(String, ULocale)'
102: */
103: public void testSimpleDateFormatStringULocale() {
104: ULocale l = ULocale.JAPAN;
105: SimpleDateFormat sdf = new SimpleDateFormat(mdy, l);
106: java.text.SimpleDateFormat jsdf = new java.text.SimpleDateFormat(
107: mdy, l.toLocale());
108: assertEquals(jsdf.format(date), sdf.format(date));
109: }
110:
111: /*
112: * Test method for 'com.ibm.icu.text.SimpleDateFormat.SimpleDateFormat(String, DateFormatSymbols)'
113: */
114: public void testSimpleDateFormatStringDateFormatSymbols() {
115: Locale l = Locale.US;
116: DateFormatSymbols dfs = new DateFormatSymbols(l);
117: java.text.DateFormatSymbols jdfs = new java.text.DateFormatSymbols(
118: l);
119: SimpleDateFormat sdf = new SimpleDateFormat(mdy, dfs);
120: java.text.SimpleDateFormat jsdf = new java.text.SimpleDateFormat(
121: mdy, jdfs);
122: assertEquals(jsdf.format(date), sdf.format(date));
123: }
124:
125: /*
126: * Test method for 'com.ibm.icu.text.SimpleDateFormat.set2DigitYearStart(Date)'
127: */
128: public void testSet2DigitYearStart() {
129: SimpleDateFormat sdf = new SimpleDateFormat(md2);
130: sdf.set2DigitYearStart(date);
131: try {
132: Date d = sdf.parse("Jan 15 04");
133: assertNotEqual(-1, d.toString().indexOf("2104"));
134: } catch (ParseException pe) {
135: fail(pe.getMessage());
136: }
137: }
138:
139: /*
140: * Test method for 'com.ibm.icu.text.SimpleDateFormat.get2DigitYearStart()'
141: */
142: public void testGet2DigitYearStart() {
143: SimpleDateFormat sdf = new SimpleDateFormat(md2);
144: sdf.set2DigitYearStart(date);
145: assertEquals(date, sdf.get2DigitYearStart());
146: }
147:
148: /*
149: * Test method for 'com.ibm.icu.text.SimpleDateFormat.toPattern()'
150: */
151: public void testToPattern() {
152: SimpleDateFormat sdf = new SimpleDateFormat(mdy);
153: assertEquals(mdy, sdf.toPattern());
154: }
155:
156: /*
157: * Test method for 'com.ibm.icu.text.SimpleDateFormat.toLocalizedPattern()'
158: */
159: public void testToLocalizedPattern() {
160: Locale l = Locale.getDefault();
161: Locale.setDefault(Locale.US);
162: SimpleDateFormat sdf = new SimpleDateFormat(mdy);
163: assertEquals(mdy, sdf.toLocalizedPattern());
164: Locale.setDefault(l);
165: }
166:
167: /*
168: * Test method for 'com.ibm.icu.text.SimpleDateFormat.applyPattern(String)'
169: */
170: public void testApplyPattern() {
171: SimpleDateFormat sdf = new SimpleDateFormat();
172: sdf.setTimeZone(tzc);
173: sdf.applyPattern(hmzmdy);
174: assertEquals(hmzmdyStr, sdf.format(date));
175: }
176:
177: /*
178: * Test method for 'com.ibm.icu.text.SimpleDateFormat.applyLocalizedPattern(String)'
179: */
180: public void testApplyLocalizedPattern() {
181: SimpleDateFormat sdf = new SimpleDateFormat();
182: sdf.setTimeZone(tzc);
183: sdf.applyLocalizedPattern(hmzmdy);
184: assertEquals(hmzmdyStr, sdf.format(date));
185: }
186:
187: /*
188: * Test method for 'com.ibm.icu.text.SimpleDateFormat.getDateFormatSymbols()'
189: */
190: public void testGetDateFormatSymbols() {
191: DateFormatSymbols dfs = new DateFormatSymbols(Locale.US);
192: SimpleDateFormat sdf = new SimpleDateFormat(mdy, dfs);
193: assertEquals(dfs, sdf.getDateFormatSymbols());
194: }
195:
196: /*
197: * Test method for 'com.ibm.icu.text.SimpleDateFormat.setDateFormatSymbols(DateFormatSymbols)'
198: */
199: public void testSetDateFormatSymbols() {
200: DateFormatSymbols dfs = new DateFormatSymbols(Locale.JAPAN);
201: SimpleDateFormat sdf = new SimpleDateFormat(hmzmdy);
202: sdf.setDateFormatSymbols(dfs);
203: // assumes Japanese symbols do not have gregorian month names
204: assertEquals(-1, sdf.format(date).indexOf("Jan"));
205: }
206: }
|