1 /*****************************************************************************************
2 * (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
3 * (C) Copyright IBM Corp. 1996-2012 - All Rights Reserved
5 * The original version of this source code and documentation is copyrighted and
6 * owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These materials are
7 * provided under terms of a License Agreement between Taligent and Sun. This
8 * technology is protected by multiple US and International patents. This notice and
9 * attribution to Taligent may not be removed.
10 * Taligent is a registered trademark of Taligent, Inc.
14 * Port From: JDK 1.4b1 : java.text.Format.IntlTestSimpleDateFormatAPI
15 * Source File: java/text/format/IntlTestSimpleDateFormatAPI.java
18 package com.ibm.icu.dev.test.format;
20 import java.text.FieldPosition;
21 import java.text.Format;
22 import java.text.ParseException;
23 import java.text.ParsePosition;
24 import java.util.Date;
25 import java.util.Locale;
27 import com.ibm.icu.text.DateFormatSymbols;
28 import com.ibm.icu.text.SimpleDateFormat;
32 * @summary test International Simple Date Format API
34 public class IntlTestSimpleDateFormatAPI extends com.ibm.icu.dev.test.TestFmwk
36 public static void main(String[] args) throws Exception {
37 new IntlTestSimpleDateFormatAPI().run(args);
40 // This test checks various generic API methods in DecimalFormat to achieve 100% API coverage.
43 logln("SimpleDateFormat API test---"); logln("");
45 Locale.setDefault(Locale.ENGLISH);
47 // ======= Test constructors
49 logln("Testing SimpleDateFormat constructors");
51 SimpleDateFormat def = new SimpleDateFormat();
53 final String pattern = new String("yyyy.MM.dd G 'at' hh:mm:ss z");
54 SimpleDateFormat pat = new SimpleDateFormat(pattern);
56 SimpleDateFormat pat_fr = new SimpleDateFormat(pattern, Locale.FRENCH);
58 DateFormatSymbols symbols = new DateFormatSymbols(Locale.FRENCH);
60 SimpleDateFormat cust1 = new SimpleDateFormat(pattern, symbols);
62 // ======= Test clone() and equality
64 logln("Testing clone(), assignment and equality operators");
66 Format clone = (Format) def.clone();
67 if( ! clone.equals(def) ) {
68 errln("ERROR: Format clone or equals failed");
71 // ======= Test various format() methods
73 logln("Testing various format() methods");
75 Date d = new Date((long)837039928046.0);
77 StringBuffer res1 = new StringBuffer();
78 StringBuffer res2 = new StringBuffer();
79 FieldPosition pos1 = new FieldPosition(0);
80 FieldPosition pos2 = new FieldPosition(0);
82 res1 = def.format(d, res1, pos1);
83 logln( "" + d.getTime() + " formatted to " + res1);
85 res2 = cust1.format(d, res2, pos2);
86 logln("" + d.getTime() + " formatted to " + res2);
88 // ======= Test parse()
90 logln("Testing parse()");
92 String text = new String("02/03/76, 2:50 AM, CST");
93 Date result1 = new Date();
94 Date result2 = new Date();
95 ParsePosition pos= new ParsePosition(0);
96 result1 = def.parse(text, pos);
97 logln(text + " parsed into " + result1);
100 result2 = def.parse(text);
102 catch (ParseException e) {
103 errln("ERROR: parse() failed");
105 logln(text + " parsed into " + result2);
107 // ======= Test getters and setters
109 logln("Testing getters and setters");
111 final DateFormatSymbols syms = pat.getDateFormatSymbols();
112 def.setDateFormatSymbols(syms);
113 pat_fr.setDateFormatSymbols(syms);
114 if( ! pat.getDateFormatSymbols().equals(def.getDateFormatSymbols()) ) {
115 errln("ERROR: set DateFormatSymbols() failed");
119 DateFormatSymbols has not the method getTwoDigitStartDate();
120 //Date startDate = null; //The variable is never used
122 // startDate = pat.getTwoDigitStartDate();
124 catch (Exception e) {
125 errln("ERROR: getTwoDigitStartDate() failed");
129 // pat_fr.setTwoDigitStartDate(startDate);
131 catch (Exception e) {
132 errln("ERROR: setTwoDigitStartDate() failed");
135 // ======= Test applyPattern()
137 logln("Testing applyPattern()");
139 String p1 = new String("yyyy.MM.dd G 'at' hh:mm:ss z");
140 logln("Applying pattern " + p1);
141 pat.applyPattern(p1);
143 String s2 = pat.toPattern();
144 logln("Extracted pattern is " + s2);
145 if( ! s2.equals(p1) ) {
146 errln("ERROR: toPattern() result did not match pattern applied");
149 logln("Applying pattern " + p1);
150 pat.applyLocalizedPattern(p1);
151 String s3 = pat.toLocalizedPattern();
152 logln("Extracted pattern is " + s3);
153 if( ! s3.equals(p1) ) {
154 errln("ERROR: toLocalizedPattern() result did not match pattern applied");
157 // ======= Test for Ticket 5684 (Parsing patterns with 'Y' and 'e'
158 logln("Testing parse()");
160 String p2 = new String("YYYY'W'wwe");
161 logln("Applying pattern " + p2);
162 pat.applyPattern(p2);
163 Date dt = pat.parse("2007W014", new ParsePosition(0));
165 errln("ERROR: Parsing failed using 'Y' and 'e'");
169 // ======= Test getStaticClassID()
171 // logln("Testing instanceof");
174 // DateFormat test = new SimpleDateFormat();
176 // if (! (test instanceof SimpleDateFormat)) {
177 // errln("ERROR: instanceof failed");
180 // catch (Exception e) {
181 // errln("ERROR: Couldn't create a SimpleDateFormat");
185 // Jitterbug 4451, for coverage
186 public void TestCoverage(){
187 class StubDateFormat extends SimpleDateFormat{
191 private static final long serialVersionUID = 8460897119491427934L;
194 if (!zeroPaddingNumber(12, 4, 6).equals("0012")){
195 errln("SimpleDateFormat(zeroPaddingNumber(long , int , int )");
199 new StubDateFormat().run();