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