2 *******************************************************************************
3 * Copyright (C) 2006-2011, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
8 package com.ibm.icu.tests;
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;
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;
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";
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;
35 cal.set(2006, 0, 10, 15, 5, 20); // arrgh, doesn't clear millis
40 * Test method for 'com.ibm.icu.text.SimpleDateFormat.format(Calendar, StringBuffer, FieldPosition)'
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());
51 * Test method for 'com.ibm.icu.text.SimpleDateFormat.parse(String, Calendar, ParsePosition)'
53 public void testParseStringCalendarParsePosition() {
54 Calendar cal = Calendar.getInstance(tzp);
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
64 * Test method for 'com.ibm.icu.text.SimpleDateFormat.clone()'
66 public void testClone() {
71 * Test method for 'com.ibm.icu.text.SimpleDateFormat.SimpleDateFormat()'
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));
80 * Test method for 'com.ibm.icu.text.SimpleDateFormat.SimpleDateFormat(String)'
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));
89 * Test method for 'com.ibm.icu.text.SimpleDateFormat.SimpleDateFormat(String, Locale)'
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));
99 * Test method for 'com.ibm.icu.text.SimpleDateFormat.SimpleDateFormat(String, ULocale)'
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));
109 * Test method for 'com.ibm.icu.text.SimpleDateFormat.SimpleDateFormat(String, DateFormatSymbols)'
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));
121 * Test method for 'com.ibm.icu.text.SimpleDateFormat.set2DigitYearStart(Date)'
123 public void testSet2DigitYearStart() {
124 SimpleDateFormat sdf = new SimpleDateFormat(md2);
125 sdf.set2DigitYearStart(date);
127 Date d = sdf.parse("Jan 15 04");
128 assertNotEqual(-1, d.toString().indexOf("2104"));
130 catch (ParseException pe) {
131 fail(pe.getMessage());
136 * Test method for 'com.ibm.icu.text.SimpleDateFormat.get2DigitYearStart()'
138 public void testGet2DigitYearStart() {
139 SimpleDateFormat sdf = new SimpleDateFormat(md2);
140 sdf.set2DigitYearStart(date);
141 assertEquals(date, sdf.get2DigitYearStart());
145 * Test method for 'com.ibm.icu.text.SimpleDateFormat.toPattern()'
147 public void testToPattern() {
148 SimpleDateFormat sdf = new SimpleDateFormat(mdy);
149 assertEquals(mdy, sdf.toPattern());
153 * Test method for 'com.ibm.icu.text.SimpleDateFormat.toLocalizedPattern()'
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);
164 * Test method for 'com.ibm.icu.text.SimpleDateFormat.applyPattern(String)'
166 public void testApplyPattern() {
167 SimpleDateFormat sdf = new SimpleDateFormat();
168 sdf.setTimeZone(tzc);
169 sdf.applyPattern(hmzmdy);
170 assertEquals(hmzmdyStr, sdf.format(date));
174 * Test method for 'com.ibm.icu.text.SimpleDateFormat.applyLocalizedPattern(String)'
176 public void testApplyLocalizedPattern() {
177 SimpleDateFormat sdf = new SimpleDateFormat();
178 sdf.setTimeZone(tzc);
179 sdf.applyLocalizedPattern(hmzmdy);
180 assertEquals(hmzmdyStr, sdf.format(date));
184 * Test method for 'com.ibm.icu.text.SimpleDateFormat.getDateFormatSymbols()'
186 public void testGetDateFormatSymbols() {
187 DateFormatSymbols dfs = new DateFormatSymbols(Locale.US);
188 SimpleDateFormat sdf = new SimpleDateFormat(mdy, dfs);
189 assertEquals(dfs, sdf.getDateFormatSymbols());
193 * Test method for 'com.ibm.icu.text.SimpleDateFormat.setDateFormatSymbols(DateFormatSymbols)'
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"));