/** * Override this method in derived tests to change the construction of instances * * @param format the format string to use * @param timeZone the time zone to use * @param locale the locale to use * * @return the DateParser instance to use for testing */ protected DateParser getInstance(final String format, final TimeZone timeZone, final Locale locale) { return new FastDateParser(format, timeZone, locale, null); }
/** * <p>Constructs a new FastDateFormat.</p> * * @param pattern {@link java.text.SimpleDateFormat} compatible pattern * @param timeZone non-null time zone to use * @param locale non-null locale to use * @param centuryStart The start of the 100 year period to use as the "default century" for 2 digit year parsing. If centuryStart is null, defaults to now - 80 years * @throws NullPointerException if pattern, timeZone, or locale is null. */ protected FastDateFormat(final String pattern, final TimeZone timeZone, final Locale locale, final Date centuryStart) { printer= new FastDatePrinter(pattern, timeZone, locale); parser= new FastDateParser(pattern, timeZone, locale, centuryStart); }
final FastDateParser fdp = new FastDateParser(parsePattern, tz, lcl); calendar.clear(); try {
@Test public void testTzParses() throws Exception { // Check that all Locales can parse the time formats we use for(final Locale locale : Locale.getAvailableLocales()) { final FastDateParser fdp= new FastDateParser("yyyy/MM/dd z", TimeZone.getDefault(), locale); for(final TimeZone tz : new TimeZone[]{NEW_YORK, REYKJAVIK, GMT}) { final Calendar cal= Calendar.getInstance(tz, locale); cal.clear(); cal.set(Calendar.YEAR, 2000); cal.set(Calendar.MONTH, 1); cal.set(Calendar.DAY_OF_MONTH, 10); final Date expected= cal.getTime(); final Date actual = fdp.parse("2000/02/10 "+tz.getDisplayName(locale)); assertEquals("tz:"+tz.getID()+" locale:"+locale.getDisplayName(), expected, actual); } } }
@Test public void testInputHasMoreCharacters() { final FastDateParser parser = new FastDateParser("MM/dd", TimeZone.getDefault(), Locale.getDefault()); final ParsePosition parsePosition = new ParsePosition(0); final Date date = parser.parse("3/23/61", parsePosition); assertEquals(4, parsePosition.getIndex()); final Calendar calendar = Calendar.getInstance(); calendar.setTime(date); assertEquals(2, calendar.get(Calendar.MONTH)); assertEquals(23, calendar.get(Calendar.DATE)); }
@Test public void testParser() throws ParseException { final DateParser parser = new FastDateParser("YYYY-'W'ww-u", TimeZone.getDefault(), Locale.getDefault()); final Calendar cal = Calendar.getInstance(); cal.setMinimalDaysInFirstWeek(4); cal.setFirstDayOfWeek(Calendar.MONDAY); cal.clear(); parser.parse(isoForm, new ParsePosition(0), cal); assertEquals(vulgar.getTime(), cal.getTime()); }
@Test public void testInputHasWrongCharacters() { final FastDateParser parser = new FastDateParser("MM-dd-yyy", TimeZone.getDefault(), Locale.getDefault()); final ParsePosition parsePosition = new ParsePosition(0); assertNull(parser.parse("03/23/1961", parsePosition)); assertEquals(2, parsePosition.getErrorIndex()); }
@Test public void testInputHasLessCharacters() { final FastDateParser parser = new FastDateParser("MM/dd/yyy", TimeZone.getDefault(), Locale.getDefault()); final ParsePosition parsePosition = new ParsePosition(0); assertNull(parser.parse("03/23", parsePosition)); assertEquals(5, parsePosition.getErrorIndex()); }
@Test public void testInputHasWhitespace() { final FastDateParser parser = new FastDateParser("M/d/y", TimeZone.getDefault(), Locale.getDefault()); //SimpleDateFormat parser = new SimpleDateFormat("M/d/y"); final ParsePosition parsePosition = new ParsePosition(0); final Date date = parser.parse(" 3/ 23/ 1961", parsePosition); assertEquals(12, parsePosition.getIndex()); final Calendar calendar = Calendar.getInstance(); calendar.setTime(date); assertEquals(1961, calendar.get(Calendar.YEAR)); assertEquals(2, calendar.get(Calendar.MONTH)); assertEquals(23, calendar.get(Calendar.DATE)); }
@Test public void testInputHasPrecedingCharacters() { final FastDateParser parser = new FastDateParser("MM/dd", TimeZone.getDefault(), Locale.getDefault()); final ParsePosition parsePosition = new ParsePosition(0); final Date date = parser.parse("A 3/23/61", parsePosition); assertNull(date); assertEquals(0, parsePosition.getIndex()); assertEquals(0, parsePosition.getErrorIndex()); }
@Test // Check that all Locales can parse the formats we use public void testParses() throws Exception { for(final String format : new String[]{LONG_FORMAT, SHORT_FORMAT}) { for(final Locale locale : Locale.getAvailableLocales()) { for(final TimeZone tz : new TimeZone[]{NEW_YORK, REYKJAVIK, GMT}) { for(final int year : new int[]{2003, 1940, 1868, 1867, 1, -1, -1940}) { final Calendar cal= getEraStart(year, tz, locale); final Date centuryStart= cal.getTime(); cal.set(Calendar.MONTH, 1); cal.set(Calendar.DAY_OF_MONTH, 10); final Date in= cal.getTime(); final FastDateParser fdp= new FastDateParser(format, tz, locale, centuryStart); validateSdfFormatFdpParseEquality(format, locale, tz, fdp, in, year, centuryStart); } } } } }
@Test public void testLang1219() throws ParseException { final FastDateParser parser = new FastDateParser("dd.MM.yyyy HH:mm:ss z", TimeZone.getDefault(), Locale.GERMAN); final Date summer = parser.parse("26.10.2014 02:00:00 MESZ"); final Date standard = parser.parse("26.10.2014 02:00:00 MEZ"); assertNotEquals(summer.getTime(), standard.getTime()); } }
@Test public void testInputHasWrongTimeZone() { final FastDateParser parser = new FastDateParser("mm:ss z", NEW_YORK, Locale.US); final String input = "11:23 Pacific Standard Time"; final ParsePosition parsePosition = new ParsePosition(0); assertNotNull(parser.parse(input, parsePosition)); assertEquals(input.length(), parsePosition.getIndex()); parsePosition.setIndex(0); assertNull(parser.parse( "11:23 Pacific Standard ", parsePosition)); assertEquals(6, parsePosition.getErrorIndex()); }
@Test public void testInputHasWrongDay() { final FastDateParser parser = new FastDateParser("EEEE, MM/dd/yyy", NEW_YORK, Locale.US); final String input = "Thursday, 03/23/61"; final ParsePosition parsePosition = new ParsePosition(0); assertNotNull(parser.parse(input, parsePosition)); assertEquals(input.length(), parsePosition.getIndex()); parsePosition.setIndex(0); assertNull(parser.parse( "Thorsday, 03/23/61", parsePosition)); assertEquals(0, parsePosition.getErrorIndex()); } }
@Test public void testTimeZoneStrategyPattern() { for(final Locale locale : Locale.getAvailableLocales()) { final FastDateParser parser = new FastDateParser("z", TimeZone.getDefault(), locale); final String[][] zones = DateFormatSymbols.getInstance(locale).getZoneStrings(); for(final String[] zone : zones) { for(int t = 1; t<zone.length; ++t) { final String tzDisplay = zone[t]; if (tzDisplay == null) { break; } try { parser.parse(tzDisplay); } catch(final Exception ex) { fail("'" + tzDisplay + "'" + " Locale: '" + locale.getDisplayName() + "'" + " TimeZone: " + zone[0] + " offset: " + t + " defaultLocale: " + Locale.getDefault() + " defaultTimeZone: " + TimeZone.getDefault().getDisplayName() ); } } } } }
private void checkParsePosition(final String formattedDate) { final SimpleDateFormat sdf = new SimpleDateFormat(format, locale); sdf.setTimeZone(timeZone); final DateParser fdf = new FastDateParser(format, timeZone, locale);
private void checkParse(final String formattedDate) { final SimpleDateFormat sdf = new SimpleDateFormat(format, locale); sdf.setTimeZone(timeZone); final DateParser fdf = new FastDateParser(format, timeZone, locale); Date expectedTime=null; Class<?> sdfE = null;
/** * <p>Constructs a new FastDateFormat.</p> * * @param pattern {@link java.text.SimpleDateFormat} compatible pattern * @param timeZone non-null time zone to use * @param locale non-null locale to use * @param centuryStart The start of the 100 year period to use as the "default century" for 2 digit year parsing. If centuryStart is null, defaults to now - 80 years * @throws NullPointerException if pattern, timeZone, or locale is null. */ protected FastDateFormat(final String pattern, final TimeZone timeZone, final Locale locale, final Date centuryStart) { printer= new FastDatePrinter(pattern, timeZone, locale); parser= new FastDateParser(pattern, timeZone, locale, centuryStart); }
/** * <p>Constructs a new FastDateFormat.</p> * * @param pattern {@link java.text.SimpleDateFormat} compatible pattern * @param timeZone non-null time zone to use * @param locale non-null locale to use * @param centuryStart The start of the 100 year period to use as the "default century" for 2 digit year parsing. If centuryStart is null, defaults to now - 80 years * @throws NullPointerException if pattern, timeZone, or locale is null. */ protected FastDateFormat(final String pattern, final TimeZone timeZone, final Locale locale, final Date centuryStart) { printer= new FastDatePrinter(pattern, timeZone, locale); parser= new FastDateParser(pattern, timeZone, locale, centuryStart); }
/** * <p>Constructs a new FastDateFormat.</p> * * @param pattern {@link java.text.SimpleDateFormat} compatible pattern * @param timeZone non-null time zone to use * @param locale non-null locale to use * @param centuryStart The start of the 100 year period to use as the "default century" for 2 digit year parsing. If centuryStart is null, defaults to now - 80 years * @throws NullPointerException if pattern, timeZone, or locale is null. */ protected FastDateFormat(final String pattern, final TimeZone timeZone, final Locale locale, final Date centuryStart) { printer= new FastDatePrinter(pattern, timeZone, locale); parser= new FastDateParser(pattern, timeZone, locale, centuryStart); }