Path: blob/master/test/jdk/java/util/Formatter/BasicDateTime.java
41149 views
/*1* Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* Type-specific source code for unit test24*25* Regenerate the BasicX classes via genBasic.sh whenever this file changes.26* We check in the generated source files so that the test tree can be used27* independently of the rest of the source tree.28*/2930// -- This file was mechanically generated: Do not edit! -- //3132import java.io.*;33import java.math.BigDecimal;34import java.math.BigInteger;35import java.text.DateFormatSymbols;36import java.util.*;3738import static java.util.Calendar.*;3940import static java.util.SimpleTimeZone.*;41import java.util.regex.Pattern;424344public class BasicDateTime extends Basic {4546private static void test(String fs, String exp, Object ... args) {47Formatter f = new Formatter(new StringBuilder(), Locale.US);48f.format(fs, args);49ck(fs, exp, f.toString());5051f = new Formatter(new StringBuilder(), Locale.US);52f.format("foo " + fs + " bar", args);53ck(fs, "foo " + exp + " bar", f.toString());54}5556private static void test(Locale l, String fs, String exp, Object ... args)57{58Formatter f = new Formatter(new StringBuilder(), l);59f.format(fs, args);60ck(fs, exp, f.toString());6162f = new Formatter(new StringBuilder(), l);63f.format("foo " + fs + " bar", args);64ck(fs, "foo " + exp + " bar", f.toString());65}6667private static void test(String fs, Object ... args) {68Formatter f = new Formatter(new StringBuilder(), Locale.US);69f.format(fs, args);70ck(fs, "fail", f.toString());71}7273private static void test(String fs) {74Formatter f = new Formatter(new StringBuilder(), Locale.US);75f.format(fs, "fail");76ck(fs, "fail", f.toString());77}7879private static void testSysOut(String fs, String exp, Object ... args) {80FileOutputStream fos = null;81FileInputStream fis = null;82try {83PrintStream saveOut = System.out;84fos = new FileOutputStream("testSysOut");85System.setOut(new PrintStream(fos));86System.out.format(Locale.US, fs, args);87fos.close();8889fis = new FileInputStream("testSysOut");90byte [] ba = new byte[exp.length()];91int len = fis.read(ba);92String got = new String(ba);93if (len != ba.length)94fail(fs, exp, got);95ck(fs, exp, got);9697System.setOut(saveOut);98} catch (FileNotFoundException ex) {99fail(fs, ex.getClass());100} catch (IOException ex) {101fail(fs, ex.getClass());102} finally {103try {104if (fos != null)105fos.close();106if (fis != null)107fis.close();108} catch (IOException ex) {109fail(fs, ex.getClass());110}111}112}113114private static void tryCatch(String fs, Class<?> ex) {115boolean caught = false;116try {117test(fs);118} catch (Throwable x) {119if (ex.isAssignableFrom(x.getClass()))120caught = true;121}122if (!caught)123fail(fs, ex);124else125pass();126}127128private static void tryCatch(String fs, Class<?> ex, Object ... args) {129boolean caught = false;130try {131test(fs, args);132} catch (Throwable x) {133if (ex.isAssignableFrom(x.getClass()))134caught = true;135}136if (!caught)137fail(fs, ex);138else139pass();140}141142143private static void testDateTime(String fs, String exp, Calendar c) {144testDateTime(fs, exp, c, true);145}146147private static void testDateTime(String fs, String exp, Calendar c, boolean upper) {148//---------------------------------------------------------------------149// Date/Time conversions applicable to Calendar, Date, and long.150//---------------------------------------------------------------------151152// Calendar153test(fs, exp, c);154test((Locale)null, fs, exp, c);155test(Locale.US, fs, exp, c);156157// Date/long do not have timezone information so they will always use158// the default timezone.159String nexp = (fs.equals("%tZ") || fs.equals("%TZ")160|| fs.equals("%tc") || fs.equals("%Tc")161? exp.replace("PST", "GMT-08:00")162: exp);163164// Date (implemented via conversion to Calendar)165Date d = c.getTime();166test(fs, nexp, d);167test((Locale)null, fs, nexp, d);168test(Locale.US, fs, nexp, d);169170// long (implemented via conversion to Calendar)171long l = c.getTimeInMillis();172test(fs, nexp, l);173test((Locale)null, fs, nexp, l);174test(Locale.US, fs, nexp, l);175176if (upper)177// repeat all tests for upper case variant (%T)178testDateTime(Pattern.compile("t").matcher(fs).replaceFirst("T"),179exp.toUpperCase(), c, false);180}181182private static void testHours() {183for (int i = 0; i < 24; i++) {184// GregorianCalendar(int year, int month, int dayOfMonth,185// int hourOfDay, int minute, int second);186Calendar c = new GregorianCalendar(1995, MAY, 23, i, 48, 34);187188//-----------------------------------------------------------------189// DateTime.HOUR_OF_DAY - 'k' (0 - 23) -- like H190//-----------------------------------------------------------------191String exp = Integer.toString(i);192testDateTime("%tk", exp, c);193194//-----------------------------------------------------------------195// DateTime.HOUR - 'l' (1 - 12) -- like I196//-----------------------------------------------------------------197int v = i % 12;198v = (v == 0 ? 12 : v);199String exp2 = Integer.toString(v);200testDateTime("%tl", exp2, c);201202//-----------------------------------------------------------------203// DateTime.HOUR_OF_DAY_0 - 'H' (00 - 23) [zero padded]204//-----------------------------------------------------------------205if (exp.length() < 2) exp = "0" + exp;206testDateTime("%tH", exp, c);207208//-----------------------------------------------------------------209// DateTime.HOUR_0 - 'I' (01 - 12)210//-----------------------------------------------------------------211if (exp2.length() < 2) exp2 = "0" + exp2;212testDateTime("%tI", exp2, c);213214//-----------------------------------------------------------------215// DateTime.AM_PM - (am or pm)216//-----------------------------------------------------------------217testDateTime("%tp", (i <12 ? "am" : "pm"), c);218}219}220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337public static void test() {338TimeZone.setDefault(TimeZone.getTimeZone("GMT-0800"));339340// Any characters not explicitly defined as conversions, date/time341// conversion suffixes, or flags are illegal and are reserved for342// future extensions. Use of such a character in a format string will343// cause an UnknownFormatConversionException or344// UnknownFormatFlagsException to be thrown.345tryCatch("%q", UnknownFormatConversionException.class);346tryCatch("%t&", UnknownFormatConversionException.class);347tryCatch("%&d", UnknownFormatConversionException.class);348tryCatch("%^b", UnknownFormatConversionException.class);349350//---------------------------------------------------------------------351// Formatter.java class javadoc examples352//---------------------------------------------------------------------353test(Locale.FRANCE, "e = %+10.4f", "e = +2,7183", Math.E);354test("%4$2s %3$2s %2$2s %1$2s", " d c b a", "a", "b", "c", "d");355test("Amount gained or lost since last statement: $ %,(.2f",356"Amount gained or lost since last statement: $ (6,217.58)",357(new BigDecimal("-6217.58")));358Calendar c = new GregorianCalendar(1969, JULY, 20, 16, 17, 0);359testSysOut("Local time: %tT", "Local time: 16:17:00", c);360361test("Unable to open file '%1$s': %2$s",362"Unable to open file 'food': No such file or directory",363"food", "No such file or directory");364Calendar duke = new GregorianCalendar(1995, MAY, 23, 19, 48, 34);365duke.set(Calendar.MILLISECOND, 584);366test("Duke's Birthday: %1$tB %1$te, %1$tY",367"Duke's Birthday: May 23, 1995",368duke);369test("Duke's Birthday: %1$tB %1$te, %1$tY",370"Duke's Birthday: May 23, 1995",371duke.getTime());372test("Duke's Birthday: %1$tB %1$te, %1$tY",373"Duke's Birthday: May 23, 1995",374duke.getTimeInMillis());375376test("%4$s %3$s %2$s %1$s %4$s %3$s %2$s %1$s",377"d c b a d c b a", "a", "b", "c", "d");378test("%s %s %<s %<s", "a b b b", "a", "b", "c", "d");379test("%s %s %s %s", "a b c d", "a", "b", "c", "d");380test("%2$s %s %<s %s", "b a a b", "a", "b", "c", "d");381382//---------------------------------------------------------------------383// %b384//385// General conversion applicable to any argument.386//---------------------------------------------------------------------387test("%b", "true", true);388test("%b", "false", false);389test("%B", "TRUE", true);390test("%B", "FALSE", false);391test("%b", "true", Boolean.TRUE);392test("%b", "false", Boolean.FALSE);393test("%B", "TRUE", Boolean.TRUE);394test("%B", "FALSE", Boolean.FALSE);395test("%14b", " true", true);396test("%-14b", "true ", true);397test("%5.1b", " f", false);398test("%-5.1b", "f ", false);399400test("%b", "true", "foo");401test("%b", "false", (Object)null);402403// Boolean.java hardcodes the Strings for "true" and "false", so no404// localization is possible.405test(Locale.FRANCE, "%b", "true", true);406test(Locale.FRANCE, "%b", "false", false);407408// If you pass in a single array to a varargs method, the compiler409// uses it as the array of arguments rather than treating it as a410// single array-type argument.411test("%b", "false", (Object[])new String[2]);412test("%b", "true", new String[2], new String[2]);413414int [] ia = { 1, 2, 3 };415test("%b", "true", ia);416417//---------------------------------------------------------------------418// %b - errors419//---------------------------------------------------------------------420tryCatch("%#b", FormatFlagsConversionMismatchException.class);421tryCatch("%-b", MissingFormatWidthException.class);422// correct or side-effect of implementation?423tryCatch("%.b", UnknownFormatConversionException.class);424tryCatch("%,b", FormatFlagsConversionMismatchException.class);425426//---------------------------------------------------------------------427// %c428//429// General conversion applicable to any argument.430//---------------------------------------------------------------------431test("%c", "i", 'i');432test("%C", "I", 'i');433test("%4c", " i", 'i');434test("%-4c", "i ", 'i');435test("%4C", " I", 'i');436test("%-4C", "I ", 'i');437test("%c", "i", new Character('i'));438test("%c", "H", (byte) 72);439test("%c", "i", (short) 105);440test("%c", "!", (int) 33);441test("%c", "\u007F", Byte.MAX_VALUE);442test("%c", new String(Character.toChars(Short.MAX_VALUE)),443Short.MAX_VALUE);444test("%c", "null", (Object) null);445446//---------------------------------------------------------------------447// %c - errors448//---------------------------------------------------------------------449tryCatch("%c", IllegalFormatConversionException.class,450Boolean.TRUE);451tryCatch("%c", IllegalFormatConversionException.class,452(float) 0.1);453tryCatch("%c", IllegalFormatConversionException.class,454new Object());455tryCatch("%c", IllegalFormatCodePointException.class,456Byte.MIN_VALUE);457tryCatch("%c", IllegalFormatCodePointException.class,458Short.MIN_VALUE);459tryCatch("%c", IllegalFormatCodePointException.class,460Integer.MIN_VALUE);461tryCatch("%c", IllegalFormatCodePointException.class,462Integer.MAX_VALUE);463464tryCatch("%#c", FormatFlagsConversionMismatchException.class);465tryCatch("%,c", FormatFlagsConversionMismatchException.class);466tryCatch("%(c", FormatFlagsConversionMismatchException.class);467tryCatch("%$c", UnknownFormatConversionException.class);468tryCatch("%.2c", IllegalFormatPrecisionException.class);469470//---------------------------------------------------------------------471// %s472//473// General conversion applicable to any argument.474//---------------------------------------------------------------------475test("%s", "Hello, Duke", "Hello, Duke");476test("%S", "HELLO, DUKE", "Hello, Duke");477test("%20S", " HELLO, DUKE", "Hello, Duke");478test("%20s", " Hello, Duke", "Hello, Duke");479test("%-20s", "Hello, Duke ", "Hello, Duke");480test("%-20.5s", "Hello ", "Hello, Duke");481test("%s", "null", (Object)null);482483StringBuffer sb = new StringBuffer("foo bar");484test("%s", sb.toString(), sb);485test("%S", sb.toString().toUpperCase(), sb);486487//---------------------------------------------------------------------488// %s - errors489//---------------------------------------------------------------------490tryCatch("%-s", MissingFormatWidthException.class);491tryCatch("%--s", DuplicateFormatFlagsException.class);492tryCatch("%#s", FormatFlagsConversionMismatchException.class, 0);493tryCatch("%#s", FormatFlagsConversionMismatchException.class, 0.5f);494tryCatch("%#s", FormatFlagsConversionMismatchException.class, "hello");495tryCatch("%#s", FormatFlagsConversionMismatchException.class, null);496497//---------------------------------------------------------------------498// %h499//500// General conversion applicable to any argument.501//---------------------------------------------------------------------502test("%h", Integer.toHexString("Hello, Duke".hashCode()),503"Hello, Duke");504test("%10h", " ddf63471", "Hello, Duke");505test("%-10h", "ddf63471 ", "Hello, Duke");506test("%-10H", "DDF63471 ", "Hello, Duke");507test("%10h", " 402e0000", 15.0);508test("%10H", " 402E0000", 15.0);509510//---------------------------------------------------------------------511// %h - errors512//---------------------------------------------------------------------513tryCatch("%#h", FormatFlagsConversionMismatchException.class);514515//---------------------------------------------------------------------516// flag/conversion errors517//---------------------------------------------------------------------518tryCatch("%F", UnknownFormatConversionException.class);519520tryCatch("%#g", FormatFlagsConversionMismatchException.class);5215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663//---------------------------------------------------------------------1664// %t1665//1666// Date/Time conversions applicable to Calendar, Date, and long.1667//---------------------------------------------------------------------1668test("%tA", "null", (Object)null);1669test("%TA", "NULL", (Object)null);16701671//---------------------------------------------------------------------1672// %t - errors1673//---------------------------------------------------------------------1674tryCatch("%t", UnknownFormatConversionException.class);1675tryCatch("%T", UnknownFormatConversionException.class);1676tryCatch("%tP", UnknownFormatConversionException.class);1677tryCatch("%TP", UnknownFormatConversionException.class);1678tryCatch("%.5tB", IllegalFormatPrecisionException.class);1679tryCatch("%#tB", FormatFlagsConversionMismatchException.class);1680tryCatch("%-tB", MissingFormatWidthException.class);168116821683//---------------------------------------------------------------------1684// %t - create test Calendar1685//---------------------------------------------------------------------16861687// Get the supported ids for GMT-08:00 (Pacific Standard Time)1688String[] ids = TimeZone.getAvailableIDs(-8 * 60 * 60 * 1000);1689// With tzdata2020b, the test fails for the mentioned zones expecting1690// "PST" but JDK has Zone name "MST" for JRE locale provider.1691// Therefore excluding them as the test is only looking for a GMT-08:001692// time zone ID. See JDK-8254865.1693final List<String> list = new ArrayList<String>();1694Collections.addAll(list, ids);1695list.remove("America/Dawson");1696list.remove("America/WhiteHorse");1697list.remove("Canada/Yukon");1698ids = list.toArray(new String[list.size()]);16991700// Create a Pacific Standard Time time zone1701SimpleTimeZone tz = new SimpleTimeZone(-8 * 60 * 60 * 1000, ids[0]);1702// public GregorianCalendar(TimeZone zone, Locale aLocale);1703Calendar c0 = new GregorianCalendar(tz, Locale.US);1704// public final void set(int year, int month, int date,1705// int hourOfDay, int minute, int second);1706c0.set(1995, MAY, 23, 19, 48, 34);1707c0.set(Calendar.MILLISECOND, 584);17081709//---------------------------------------------------------------------1710// %t - Minutes, {nano,milli}*seconds1711//1712// testDateTime() verifies the expected output for all applicable types1713// (Calendar, Date, and long). It also verifies output for "%t" and1714// "%T". Thus it is sufficient to invoke that method once per1715// conversion/expected output.1716//---------------------------------------------------------------------1717testDateTime("%tM", "48", c0);1718testDateTime("%tN", "584000000", c0);1719testDateTime("%tL", "584", c0);1720// testDateTime("%tQ", "801283714584", c0);17211722testDateTime("%ts", String.valueOf(c0.getTimeInMillis() / 1000), c0);1723testDateTime("%tS", "34", c0);1724testDateTime("%tT", "19:48:34", c0);17251726//---------------------------------------------------------------------1727// %t - Hours, morning/afternoon markers1728//1729// testHours() iterates through all twenty-four hours to verify1730// numeric return value and morning/afternoon markers.1731//---------------------------------------------------------------------1732testHours();17331734//---------------------------------------------------------------------1735// %t - Portions of date [ day, month, dates, weeks ]1736//---------------------------------------------------------------------1737testDateTime("%ta", "Tue", c0);1738testDateTime("%tA", "Tuesday", c0);1739testDateTime("%tb", "May", c0);1740testDateTime("%tB", "May", c0);1741testDateTime("%tC", "19", c0);1742testDateTime("%td", "23", c0);1743testDateTime("%te", "23", c0);1744testDateTime("%th", "May", c0);1745testDateTime("%tj", "143", c0);1746testDateTime("%tm", "05", c0);1747testDateTime("%ty", "95", c0);1748testDateTime("%tY", "1995", c0);17491750//---------------------------------------------------------------------1751// %t - TimeZone1752//---------------------------------------------------------------------1753testDateTime("%tz", "-0800", c0);1754testDateTime("%tZ", "PST", c0);17551756//---------------------------------------------------------------------1757// %tz should always adjust for DST1758//---------------------------------------------------------------------1759TimeZone dtz = TimeZone.getDefault();17601761// Artificial TimeZone based on PST with 3:15 DST always in effect1762TimeZone atz = new SimpleTimeZone(-8 * 60 * 60 * 1000, "AlwaysDST",1763JANUARY, 1, 0, 0, STANDARD_TIME,1764// 24hrs - 1m = 60 * 60 * 1000 * 24 - 11765DECEMBER, 31, 0, 60 * 60 * 1000 * 24 - 1, STANDARD_TIME,1766(int)(60 * 60 * 1000 * 3.25));1767TimeZone.setDefault(atz);1768testDateTime("%tz", "-0445", Calendar.getInstance(atz));17691770// Restore the TimeZone and verify1771TimeZone.setDefault(dtz);1772if (atz.hasSameRules(TimeZone.getDefault()))1773throw new RuntimeException("Default TimeZone not restored");17741775//---------------------------------------------------------------------1776// %t - Composites1777//---------------------------------------------------------------------1778testDateTime("%tr", "07:48:34 PM", c0);1779testDateTime("%tR", "19:48", c0);1780testDateTime("%tc", "Tue May 23 19:48:34 PST 1995", c0);1781testDateTime("%tD", "05/23/95", c0);1782testDateTime("%tF", "1995-05-23", c0);1783testDateTime("%-12tF", "1995-05-23 ", c0);1784testDateTime("%12tF", " 1995-05-23", c0);178517861787//---------------------------------------------------------------------1788// %n1789//---------------------------------------------------------------------1790test("%n", System.getProperty("line.separator"), (Object)null);1791test("%n", System.getProperty("line.separator"), "");17921793tryCatch("%,n", IllegalFormatFlagsException.class);1794tryCatch("%.n", UnknownFormatConversionException.class);1795tryCatch("%5.n", UnknownFormatConversionException.class);1796tryCatch("%5n", IllegalFormatWidthException.class);1797tryCatch("%.7n", IllegalFormatPrecisionException.class);1798tryCatch("%<n", IllegalFormatFlagsException.class);17991800//---------------------------------------------------------------------1801// %%1802//---------------------------------------------------------------------1803test("%%", "%", (Object)null);1804test("%%", "%", "");18051806test("%5%", " %", (Object)null);1807test("%5%", " %", "");1808test("%-5%", "% ", (Object)null);1809test("%-5%", "% ", "");18101811tryCatch("%.5%", IllegalFormatPrecisionException.class);1812tryCatch("%5.5%", IllegalFormatPrecisionException.class);18131814tryCatch("%%%", UnknownFormatConversionException.class);1815// perhaps an IllegalFormatArgumentIndexException should be defined?1816tryCatch("%<%", IllegalFormatFlagsException.class);1817}1818}181918201821