1 /*****************************************************************************************
3 * Copyright (C) 1996-2012, International Business Machines
4 * Corporation and others. All Rights Reserved.
8 * Port From: JDK 1.4b1 : java.text.Format.IntlTestDateFormatAPI
9 * Source File: java/text/format/IntlTestDateFormatAPI.java
14 @summary test International Date Format API
17 package com.ibm.icu.dev.test.format;
19 import java.text.FieldPosition;
20 import java.text.ParseException;
21 import java.text.ParsePosition;
22 import java.util.Date;
23 import java.util.Locale;
25 import com.ibm.icu.text.DateFormat;
26 import com.ibm.icu.text.NumberFormat;
27 import com.ibm.icu.util.Calendar;
28 import com.ibm.icu.util.TimeZone;
29 import com.ibm.icu.util.VersionInfo;
31 public class IntlTestDateFormatAPI extends com.ibm.icu.dev.test.TestFmwk
33 public static void main(String[] args) throws Exception {
34 new IntlTestDateFormatAPI().run(args);
37 // Test that the equals method works correctly.
38 public void TestEquals()
40 // Create two objects at different system times
41 DateFormat a = DateFormat.getInstance();
42 Date start = Calendar.getInstance().getTime();
44 // changed to remove compiler warnings.
45 if (!start.equals(Calendar.getInstance().getTime())) {
46 break; // Wait for time to change
49 DateFormat b = DateFormat.getInstance();
52 errln("FAIL: DateFormat objects created at different times are unequal.");
54 // Why has this test been disabled??? - aliu
55 // if (b instanceof SimpleDateFormat)
57 // //double ONE_YEAR = 365*24*60*60*1000.0; //The variable is never used
59 // ((SimpleDateFormat)b).setTwoDigitStartDate(start.getTime() + 50*ONE_YEAR);
61 // errln("FAIL: DateFormat objects with different two digit start dates are equal.");
63 // catch (Exception e) {
64 // errln("FAIL: setTwoDigitStartDate failed.");
69 // This test checks various generic API methods in DateFormat to achieve 100% API coverage.
72 logln("DateFormat API test---"); logln("");
73 Locale.setDefault(Locale.ENGLISH);
76 // ======= Test constructors
78 logln("Testing DateFormat constructors");
80 DateFormat def = DateFormat.getInstance();
81 DateFormat fr = DateFormat.getTimeInstance(DateFormat.FULL, Locale.FRENCH);
82 DateFormat it = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.ITALIAN);
83 DateFormat de = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, Locale.GERMAN);
85 // ======= Test equality
87 logln("Testing equality operator");
90 errln("ERROR: equals failed");
93 // ======= Test various format() methods
95 logln("Testing various format() methods");
97 Date d = new Date((long)837039928046.0);
99 StringBuffer res1 = new StringBuffer();
100 StringBuffer res2 = new StringBuffer();
101 String res3 = new String();
102 FieldPosition pos1 = new FieldPosition(0);
103 FieldPosition pos2 = new FieldPosition(0);
105 res1 = fr.format(d, res1, pos1);
106 logln("" + d.getTime() + " formatted to " + res1);
108 res2 = it.format(d, res2, pos2);
109 logln("" + d.getTime() + " formatted to " + res2);
112 logln("" + d.getTime() + " formatted to " + res3);
114 // ======= Test parse()
116 logln("Testing parse()");
118 String text = new String("02/03/76, 2:50 AM, CST");
119 Object result1 = new Date();
120 Date result2 = new Date();
121 Date result3 = new Date();
122 ParsePosition pos = new ParsePosition(0);
123 ParsePosition pos01 = new ParsePosition(0);
125 result1 = def.parseObject(text, pos);
126 if (result1 == null) {
127 errln("ERROR: parseObject() failed for " + text);
129 logln(text + " parsed into " + ((Date)result1).getTime());
132 result2 = def.parse(text);
134 catch (ParseException e) {
135 errln("ERROR: parse() failed");
137 logln(text + " parsed into " + result2.getTime());
139 result3 = def.parse(text, pos01);
140 if (result3 == null) {
141 errln("ERROR: parse() failed for " + text);
143 logln(text + " parsed into " + result3.getTime());
146 // ======= Test getters and setters
148 logln("Testing getters and setters");
150 final Locale[] locales = DateFormat.getAvailableLocales();
151 long count = locales.length;
152 logln("Got " + count + " locales" );
155 // These locales should be included in the result
156 boolean java7orLater = (VersionInfo.javaVersion().compareTo(VersionInfo.getInstance(1, 7)) >= 0);
157 final Locale[] samples = {
158 new Locale("zh", "CN"),
159 new Locale("zh", "TW"),
160 new Locale("zh", "HK"),
161 new Locale("sr", "RS"),
163 boolean[] available = new boolean[samples.length];
164 for(int i = 0; i < count; i++) {
166 name = locales[i].getDisplayName();
168 for (int j = 0; j < samples.length; j++) {
169 if (locales[i].equals(samples[j])) {
175 for (int i = 0; i < available.length; i++) {
178 // Java 7 supports script field, so zh_Hans_CN is included
179 // in the available locale list.
180 logln("INFO: missing Locale: " + samples[i]);
182 errln("ERROR: missing Locale: " + samples[i]);
187 fr.setLenient(it.isLenient());
188 if(fr.isLenient() != it.isLenient()) {
189 errln("ERROR: setLenient() failed");
192 final Calendar cal = def.getCalendar();
193 Calendar newCal = (Calendar) cal.clone();
194 de.setCalendar(newCal);
195 it.setCalendar(newCal);
196 if( ! de.getCalendar().equals(it.getCalendar())) {
197 errln("ERROR: set Calendar() failed");
200 final NumberFormat nf = def.getNumberFormat();
201 NumberFormat newNf = (NumberFormat) nf.clone();
202 de.setNumberFormat(newNf);
203 it.setNumberFormat(newNf);
204 if( ! de.getNumberFormat().equals(it.getNumberFormat())) {
205 errln("ERROR: set NumberFormat() failed");
208 final TimeZone tz = def.getTimeZone();
209 TimeZone newTz = (TimeZone) tz.clone();
210 de.setTimeZone(newTz);
211 it.setTimeZone(newTz);
212 if( ! de.getTimeZone().equals(it.getTimeZone())) {
213 errln("ERROR: set TimeZone() failed");
216 // ======= Test getStaticClassID()
218 // logln("Testing instanceof()");
221 // DateFormat test = new SimpleDateFormat();
223 // if (! (test instanceof SimpleDateFormat)) {
224 // errln("ERROR: instanceof failed");
227 // catch (Exception e) {
228 // errln("ERROR: Couldn't create a DateFormat");