/** * <p>Format an object into a <code>String</code> using * the default Locale.</p> * * @param value The value validation is being performed on. * @param timeZone The Time Zone used to format the date, * system default if null (unless value is a <code>Calendar</code>. * @return The value formatted as a <code>String</code>. */ public String format(Object value, TimeZone timeZone) { return format(value, (String)null, (Locale)null, timeZone); }
/** * <p>Checks if the value is valid against a specified pattern.</p> * * @param value The value validation is being performed on. * @param pattern The pattern used to validate the value against, or the * default for the <code>Locale</code> if <code>null</code>. * @param locale The locale to use for the date format, system default if null. * @param timeZone The Time Zone used to parse the date, system default if null. * @return The parsed value if valid or <code>null</code> if invalid. */ protected Object parse(String value, String pattern, Locale locale, TimeZone timeZone) { value = (value == null ? null : value.trim()); if (value == null || value.length() == 0) { return null; } DateFormat formatter = (DateFormat)getFormat(pattern, locale); if (timeZone != null) { formatter.setTimeZone(timeZone); } return parse(value, formatter); }
result = calculateCompareResult(value, compare, Calendar.YEAR); if (result != 0 || field == Calendar.YEAR) { return result; return calculateCompareResult(value, compare, Calendar.WEEK_OF_YEAR); return calculateCompareResult(value, compare, Calendar.DAY_OF_YEAR); result = calculateCompareResult(value, compare, Calendar.MONTH); if (result != 0 || field == Calendar.MONTH) { return result; return calculateCompareResult(value, compare, Calendar.WEEK_OF_MONTH); result = calculateCompareResult(value, compare, Calendar.DATE); if (result != 0 || (field == Calendar.DATE || field == Calendar.DAY_OF_WEEK || return compareTime(value, compare, field);
/** * <p>Format an object using the specified pattern and/or * <code>Locale</code>. * * @param value The value validation is being performed on. * @param pattern The pattern used to format the value. * @param locale The locale to use for the Format. * @param timeZone The Time Zone used to format the date, * system default if null (unless value is a <code>Calendar</code>. * @return The value formatted as a <code>String</code>. */ public String format(Object value, String pattern, Locale locale, TimeZone timeZone) { DateFormat formatter = (DateFormat)getFormat(pattern, locale); if (timeZone != null) { formatter.setTimeZone(timeZone); } else if (value instanceof Calendar) { formatter.setTimeZone(((Calendar)value).getTimeZone()); } return format(value, formatter); }
/** * Test Invalid Dates with "locale" validation */ public void testFormat() { // Create a Date or Calendar Object test = validator.parse("2005-11-28", "yyyy-MM-dd", null, null); assertNotNull("Test Date ", test); assertEquals("Format pattern", "28.11.05", validator.format(test, "dd.MM.yy")); assertEquals("Format locale", "11/28/05", validator.format(test, Locale.US)); }
/** * Test Invalid Dates with "pattern" validation */ public void testPatternInvalid() { for (int i = 0; i < patternInvalid.length; i++) { String text = i + " value=[" +patternInvalid[i]+"] passed "; Object date = validator.parse(patternInvalid[i], "yy-MM-dd", null, null); assertNull("validateObj() " + text + date, date); assertFalse("isValid() " + text, validator.isValid(patternInvalid[i], "yy-MM-dd")); } }
/** * <p>Compare Quarters (quarter and year).</p> * * @param value The <code>Calendar</code> value to check. * @param compare The <code>Calendar</code> to compare the value to. * @param monthOfFirstQuarter The month that the first quarter starts. * @return Zero if the quarters are equal, -1 if first * parameter's quarter is less than the seconds and +1 if the first * parameter's quarter is greater than. */ @Override public int compareQuarters(Calendar value, Calendar compare, int monthOfFirstQuarter) { return super.compareQuarters(value, compare, monthOfFirstQuarter); }
/** * <p>Validate using the specified <code>Locale</code>. * * @param value The value validation is being performed on. * @param pattern The pattern used to format the value. * @param locale The locale to use for the Format, defaults to the default * @return <code>true</code> if the value is valid. */ @Override public boolean isValid(String value, String pattern, Locale locale) { Object parsedValue = parse(value, pattern, locale, (TimeZone)null); return (parsedValue == null ? false : true); }
/** * <p>Compares a calendar's quarter value to another, indicating whether it is * equal, less then or more than the specified quarter.</p> * * @param value The Calendar value. * @param compare The <code>Calendar</code> to check the value against. * @param monthOfFirstQuarter The month that the first quarter starts. * @return Zero if the first quarter is equal to the second, -1 * if it is less than the second or +1 if it is greater than the second. */ protected int compareQuarters(Calendar value, Calendar compare, int monthOfFirstQuarter) { int valueQuarter = calculateQuarter(value, monthOfFirstQuarter); int compareQuarter = calculateQuarter(compare, monthOfFirstQuarter); if (valueQuarter < compareQuarter) { return -1; } else if (valueQuarter > compareQuarter) { return 1; } else { return 0; } }
/** * <p>Returns a <code>DateFormat</code> for the specified <i>pattern</i> * and/or <code>Locale</code>.</p> * * @param pattern The pattern used to validate the value against or * <code>null</code> to use the default for the <code>Locale</code>. * @param locale The locale to use for the currency format, system default if null. * @return The <code>DateFormat</code> to created. */ @Override protected Format getFormat(String pattern, Locale locale) { DateFormat formatter = null; boolean usePattern = (pattern != null && pattern.length() > 0); if (!usePattern) { formatter = (DateFormat)getFormat(locale); } else if (locale == null) { formatter = new SimpleDateFormat(pattern); } else { DateFormatSymbols symbols = new DateFormatSymbols(locale); formatter = new SimpleDateFormat(pattern, symbols); } formatter.setLenient(false); return formatter; }
result = calculateCompareResult(value, compare, Calendar.HOUR_OF_DAY); if (result != 0 || (field == Calendar.HOUR || field == Calendar.HOUR_OF_DAY)) { return result; result = calculateCompareResult(value, compare, Calendar.MINUTE); if (result != 0 || field == Calendar.MINUTE) { return result; result = calculateCompareResult(value, compare, Calendar.SECOND); if (result != 0 || field == Calendar.SECOND) { return result; return calculateCompareResult(value, compare, Calendar.MILLISECOND);
/** * <p>Format an object using the specified pattern and/or * <code>Locale</code>. * * @param value The value validation is being performed on. * @param pattern The pattern used to format the value. * @param locale The locale to use for the Format. * @param timeZone The Time Zone used to format the date, * system default if null (unless value is a <code>Calendar</code>. * @return The value formatted as a <code>String</code>. */ public String format(Object value, String pattern, Locale locale, TimeZone timeZone) { DateFormat formatter = (DateFormat)getFormat(pattern, locale); if (timeZone != null) { formatter.setTimeZone(timeZone); } else if (value instanceof Calendar) { formatter.setTimeZone(((Calendar)value).getTimeZone()); } return format(value, formatter); }
/** * Test Invalid Dates with "locale" validation */ public void testLocaleInvalid() { for (int i = 0; i < localeInvalid.length; i++) { String text = i + " value=[" +localeInvalid[i]+"] passed "; Object date = validator.parse(localeInvalid[i], null, Locale.US, null); assertNull("validateObj() " + text + date, date); assertFalse("isValid() " + text, validator.isValid(localeInvalid[i], Locale.US)); } }
/** * <p>Compare Quarters (quarter and year).</p> * * @param value The <code>Date</code> value to check. * @param compare The <code>Date</code> to compare the value to. * @param timeZone The Time Zone used to compare the dates, system default if null. * @param monthOfFirstQuarter The month that the first quarter starts. * @return Zero if the quarters are equal, -1 if first * parameter's quarter is less than the seconds and +1 if the first * parameter's quarter is greater than. */ public int compareQuarters(Date value, Date compare, TimeZone timeZone, int monthOfFirstQuarter) { Calendar calendarValue = getCalendar(value, timeZone); Calendar calendarCompare = getCalendar(compare, timeZone); return super.compareQuarters(calendarValue, calendarCompare, monthOfFirstQuarter); }
/** * <p>Validate using the specified <code>Locale</code>. * * @param value The value validation is being performed on. * @param pattern The pattern used to format the value. * @param locale The locale to use for the Format, defaults to the default * @return <code>true</code> if the value is valid. */ public boolean isValid(String value, String pattern, Locale locale) { Object parsedValue = parse(value, pattern, locale, (TimeZone)null); return (parsedValue == null ? false : true); }
/** * <p>Compares a calendar's quarter value to another, indicating whether it is * equal, less then or more than the specified quarter.</p> * * @param value The Calendar value. * @param compare The <code>Calendar</code> to check the value against. * @param monthOfFirstQuarter The month that the first quarter starts. * @return Zero if the first quarter is equal to the second, -1 * if it is less than the second or +1 if it is greater than the second. */ protected int compareQuarters(Calendar value, Calendar compare, int monthOfFirstQuarter) { int valueQuarter = calculateQuarter(value, monthOfFirstQuarter); int compareQuarter = calculateQuarter(compare, monthOfFirstQuarter); if (valueQuarter < compareQuarter) { return -1; } else if (valueQuarter > compareQuarter) { return 1; } else { return 0; } }
/** * <p>Returns a <code>DateFormat</code> for the specified <i>pattern</i> * and/or <code>Locale</code>.</p> * * @param pattern The pattern used to validate the value against or * <code>null</code> to use the default for the <code>Locale</code>. * @param locale The locale to use for the currency format, system default if null. * @return The <code>DateFormat</code> to created. */ protected Format getFormat(String pattern, Locale locale) { DateFormat formatter = null; boolean usePattern = (pattern != null && pattern.length() > 0); if (!usePattern) { formatter = (DateFormat)getFormat(locale); } else if (locale == null) { formatter = new SimpleDateFormat(pattern); } else { DateFormatSymbols symbols = new DateFormatSymbols(locale); formatter = new SimpleDateFormat(pattern, symbols); } formatter.setLenient(false); return formatter; }
result = calculateCompareResult(value, compare, Calendar.HOUR_OF_DAY); if (result != 0 || (field == Calendar.HOUR || field == Calendar.HOUR_OF_DAY)) { return result; result = calculateCompareResult(value, compare, Calendar.MINUTE); if (result != 0 || field == Calendar.MINUTE) { return result; result = calculateCompareResult(value, compare, Calendar.SECOND); if (result != 0 || field == Calendar.SECOND) { return result; return calculateCompareResult(value, compare, Calendar.MILLISECOND);
/** * <p>Format an object using the specified pattern and/or * <code>Locale</code>. * * @param value The value validation is being performed on. * @param pattern The pattern used to format the value. * @param locale The locale to use for the Format. * @return The value formatted as a <code>String</code>. */ @Override public String format(Object value, String pattern, Locale locale) { return format(value, pattern, locale, (TimeZone)null); }
/** * <p>Format an object using the specified pattern and/or * <code>Locale</code>. * * @param value The value validation is being performed on. * @param pattern The pattern used to format the value. * @param locale The locale to use for the Format. * @param timeZone The Time Zone used to format the date, * system default if null (unless value is a <code>Calendar</code>. * @return The value formatted as a <code>String</code>. */ public String format(Object value, String pattern, Locale locale, TimeZone timeZone) { DateFormat formatter = (DateFormat)getFormat(pattern, locale); if (timeZone != null) { formatter.setTimeZone(timeZone); } else if (value instanceof Calendar) { formatter.setTimeZone(((Calendar)value).getTimeZone()); } return format(value, formatter); }