final FastDateParser fdp = new FastDateParser(parsePattern, tz, lcl); calendar.clear(); try { if (fdp.parse(str, pos, calendar) && pos.getIndex()==str.length()) { return calendar.getTime();
/** * Construct a Strategy that parses a Text field * @param field The Calendar field * @param definingCalendar The calendar to obtain the short and long values * @return a TextStrategy for the field and Locale */ private Strategy getLocaleSpecificStrategy(final int field, final Calendar definingCalendar) { final ConcurrentMap<Locale, Strategy> cache = getCache(field); Strategy strategy = cache.get(locale); if (strategy == null) { strategy = field == Calendar.ZONE_OFFSET ? new TimeZoneStrategy(locale) : new CaseInsensitiveTextStrategy(field, definingCalendar, locale); final Strategy inCache = cache.putIfAbsent(locale, strategy); if (inCache != null) { return inCache; } } return strategy; }
return DAY_OF_YEAR_STRATEGY; case 'E': return getLocaleSpecificStrategy(Calendar.DAY_OF_WEEK, definingCalendar); case 'F': return DAY_OF_WEEK_IN_MONTH_STRATEGY; case 'G': return getLocaleSpecificStrategy(Calendar.ERA, definingCalendar); case 'H': // Hour in day (0-23) return HOUR_OF_DAY_STRATEGY; return HOUR_STRATEGY; case 'M': return width>=3 ?getLocaleSpecificStrategy(Calendar.MONTH, definingCalendar) :NUMBER_MONTH_STRATEGY; case 'S': return MILLISECOND_STRATEGY; return WEEK_OF_MONTH_STRATEGY; case 'a': return getLocaleSpecificStrategy(Calendar.AM_PM, definingCalendar); case 'd': return DAY_OF_MONTH_STRATEGY; return getLocaleSpecificStrategy(Calendar.ZONE_OFFSET, definingCalendar);
@Override public Date parse(final String source, final ParsePosition pos) { return parser.parse(source, pos); }
/** * 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); }
/** * Create the object after serialization. This implementation reinitializes the * transient properties. * * @param in ObjectInputStream from which the object is being deserialized. * @throws IOException if there is an IO issue. * @throws ClassNotFoundException if a class cannot be found. */ private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException { in.defaultReadObject(); final Calendar definingCalendar = Calendar.getInstance(timeZone, locale); init(definingCalendar); }
@Override public Object parseObject(final String source, final ParsePosition pos) { return parser.parseObject(source, pos); }
/** * Get the short and long values displayed for a field * @param cal The calendar to obtain the short and long values * @param locale The locale of display names * @param field The field of interest * @param regex The regular expression to build * @return The map of string display names to field values */ private static Map<String, Integer> appendDisplayNames(final Calendar cal, final Locale locale, final int field, final StringBuilder regex) { final Map<String, Integer> values = new HashMap<>(); final Map<String, Integer> displayNames = cal.getDisplayNames(field, Calendar.ALL_STYLES, locale); final TreeSet<String> sorted = new TreeSet<>(LONGER_FIRST_LOWERCASE); for (final Map.Entry<String, Integer> displayName : displayNames.entrySet()) { final String key = displayName.getKey().toLowerCase(locale); if (sorted.add(key)) { values.put(key, displayName.getValue()); } } for (final String symbol : sorted) { simpleQuote(regex, symbol).append('|'); } return values; }
@Override public Date parse(final String source) throws ParseException { return parser.parse(source); }
/** * <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); }
startYear= centuryStartYear - century; init(definingCalendar);
@Override public Object parseObject(final String source, final ParsePosition pos) { return parser.parseObject(source, pos); }
/** * Get the short and long values displayed for a field * @param cal The calendar to obtain the short and long values * @param locale The locale of display names * @param field The field of interest * @param regex The regular expression to build * @return The map of string display names to field values */ private static Map<String, Integer> appendDisplayNames(final Calendar cal, final Locale locale, final int field, final StringBuilder regex) { final Map<String, Integer> values = new HashMap<>(); final Map<String, Integer> displayNames = cal.getDisplayNames(field, Calendar.ALL_STYLES, locale); final TreeSet<String> sorted = new TreeSet<>(LONGER_FIRST_LOWERCASE); for (final Map.Entry<String, Integer> displayName : displayNames.entrySet()) { final String key = displayName.getKey().toLowerCase(locale); if (sorted.add(key)) { values.put(key, displayName.getValue()); } } for (final String symbol : sorted) { simpleQuote(regex, symbol).append('|'); } return values; }
@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()); } }
@Override public Object parseObject(final String source, final ParsePosition pos) { return parse(source, pos); }
@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()); }
/** * Create the object after serialization. This implementation reinitializes the * transient properties. * * @param in ObjectInputStream from which the object is being deserialized. * @throws IOException if there is an IO issue. * @throws ClassNotFoundException if a class cannot be found. */ private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException { in.defaultReadObject(); final Calendar definingCalendar = Calendar.getInstance(timeZone, locale); init(definingCalendar); }
@Override public Object parseObject(final String source, final ParsePosition pos) { return parser.parseObject(source, pos); }
/** * Construct a Strategy that parses a Text field * @param field The Calendar field * @param definingCalendar The calendar to obtain the short and long values * @return a TextStrategy for the field and Locale */ private Strategy getLocaleSpecificStrategy(final int field, final Calendar definingCalendar) { final ConcurrentMap<Locale, Strategy> cache = getCache(field); Strategy strategy = cache.get(locale); if (strategy == null) { strategy = field == Calendar.ZONE_OFFSET ? new TimeZoneStrategy(locale) : new CaseInsensitiveTextStrategy(field, definingCalendar, locale); final Strategy inCache = cache.putIfAbsent(locale, strategy); if (inCache != null) { return inCache; } } return strategy; }
/** * Get the short and long values displayed for a field * @param cal The calendar to obtain the short and long values * @param locale The locale of display names * @param field The field of interest * @param regex The regular expression to build * @return The map of string display names to field values */ private static Map<String, Integer> appendDisplayNames(final Calendar cal, final Locale locale, final int field, final StringBuilder regex) { final Map<String, Integer> values = new HashMap<>(); final Map<String, Integer> displayNames = cal.getDisplayNames(field, Calendar.ALL_STYLES, locale); final TreeSet<String> sorted = new TreeSet<>(LONGER_FIRST_LOWERCASE); for (final Map.Entry<String, Integer> displayName : displayNames.entrySet()) { final String key = displayName.getKey().toLowerCase(locale); if (sorted.add(key)) { values.put(key, displayName.getValue()); } } for (final String symbol : sorted) { simpleQuote(regex, symbol).append('|'); } return values; }