Java Code Examples for java.text.DateFormat#setLenient()

The following examples show how to use java.text.DateFormat#setLenient() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: Bats   File: DateTimeUtils.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Parses a string using {@link SimpleDateFormat} and a given pattern. This
 * method parses a string at the specified parse position and if successful,
 * updates the parse position to the index after the last character used.
 * The parsing is strict and requires months to be less than 12, days to be
 * less than 31, etc.
 *
 * @param s       string to be parsed
 * @param dateFormat Date format
 * @param tz      time zone in which to interpret string. Defaults to the Java
 *                default time zone
 * @param pp      position to start parsing from
 * @return a Calendar initialized with the parsed value, or null if parsing
 * failed. If returned, the Calendar is configured to the GMT time zone.
 */
private static Calendar parseDateFormat(String s, DateFormat dateFormat,
    TimeZone tz, ParsePosition pp) {
  if (tz == null) {
    tz = DEFAULT_ZONE;
  }
  Calendar ret = Calendar.getInstance(tz, Locale.ROOT);
  dateFormat.setCalendar(ret);
  dateFormat.setLenient(false);

  final Date d = dateFormat.parse(s, pp);
  if (null == d) {
    return null;
  }
  ret.setTime(d);
  ret.setTimeZone(UTC_ZONE);
  return ret;
}
 
Example 2
Source Project: olat   File: DatePropertyHandler.java    License: Apache License 2.0 6 votes vote down vote up
/**
*/
  @Override
  public boolean isValidValue(final String value, final ValidationError validationError, final Locale locale) {
      if (StringHelper.containsNonWhitespace(value)) {
          final DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, locale);
          df.setLenient(false);
          try {
              df.parse(value.trim());
          } catch (final ParseException e) {
              validationError.setErrorKey(i18nFormElementLabelKey() + ".error");
              return false;
          }
          return true;
      }
      // null values are ok
      return true;
  }
 
Example 3
Source Project: zephyr   File: DateFormatNormalizer.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public String normalize(String value) throws NormalizationException {
    try {
        DateFormat in = getLocaleDateFormat(incomingFormat, incomingLocale);

        if (this.timezone != null)
            in.setTimeZone(this.timezone);
        in.setLenient(false);
        Date date = in.parse(value);
        DateFormat out = getLocaleDateFormat(outgoingFormat, outgoingLocale);
        out.setTimeZone(TimeZone.getTimeZone("GMT"));
        return out.format(date);
    } catch (Throwable t) {
        throw new NormalizationException(t);
    }
}
 
Example 4
/**
    * Static method to get time-only DateFormat for output.
    * @param style DateFormat style
    * @param locale locale
    * @param tz time zone
    * @return a DateFormat.
    */
public static DateFormat getLocaleTimeFormat(int style, Locale locale, TimeZone tz) {
	DateFormat df;
	switch (style) {
	case DateFormat.SHORT:
		df = getFormatProvider(locale).getTimeInstance(DateFormat.SHORT, locale);
		break;
	case DateFormat.MEDIUM:
		df = getFormatProvider(locale).getTimeInstance(DateFormat.MEDIUM, locale);
		break;
	case DateFormat.LONG:
		df = getFormatProvider(locale).getTimeInstance(DateFormat.LONG, locale);
		break;
	default:
		df = getFormatProvider(locale).getTimeInstance(DateFormat.SHORT, locale);

	}
	df = BaseLocalizer.convertTo4DigitYear(df, locale);
	df.setLenient(false);
	df.setTimeZone(tz);
	return df;
}
 
Example 5
Source Project: hasor   File: AbstractSettings.java    License: Apache License 2.0 6 votes vote down vote up
/** 解析全局配置参数,并且返回其{@link Date}形式对象。第二个参数为默认值。 */
public Date getDate(final String name, final String format, final Date defaultValue) {
    String oriData = this.getToType(name, String.class);
    if (oriData == null || oriData.length() == 0) {
        return defaultValue;
    }
    //
    DateFormat dateFormat = new SimpleDateFormat(format);
    ParsePosition pos = new ParsePosition(0);
    dateFormat.setLenient(false);
    Date parsedDate = dateFormat.parse(oriData, pos); // ignore the result (use the Calendar)
    if (pos.getErrorIndex() >= 0 || pos.getIndex() != oriData.length() || parsedDate == null) {
        return defaultValue;
    } else {
        return parsedDate;
    }
}
 
Example 6
/**
    * Static method to get date-time DateFormat for output. 
    * @param style DateFormat style
    * @param locale locale
    * @param tz time zone
    * @return a DateFormat instance 
    */
public static DateFormat getLocaleDateTimeFormat(int style, Locale locale, TimeZone tz) {
	DateFormat df;
	switch (style) {
	case DateFormat.SHORT:
		df = getFormatProvider(locale).getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, locale);
		break;
	case DateFormat.MEDIUM:
		df = getFormatProvider(locale).getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM, locale);
		break;
	case DateFormat.LONG:
		df = getFormatProvider(locale).getDateTimeInstance(DateFormat.SHORT, DateFormat.LONG, locale);
		break;
	default:
		df = getFormatProvider(locale).getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, locale);
	}
	df = BaseLocalizer.convertTo4DigitYear(df, locale);
	df.setLenient(false);
	df.setTimeZone(tz);
	return df;
}
 
Example 7
private static Date parseDate(String s) {
    for (DateFormat currentFomat : DATE_FORMATS) {
        synchronized (currentFomat) {
            currentFomat.setLenient(false);
            Date result = currentFomat.parse(s, new ParsePosition(0));
            if (result != null) {
                return result;
            }
        }
    }
    return null;
}
 
Example 8
@Test(dataProvider="lenientEra")
public void testLenientEra(String lenient, String strict) throws Exception {
    Calendar c = new Calendar.Builder()
        .setCalendarType("japanese")
        .build();
    DateFormat df = new SimpleDateFormat("GGGG y-M-d", Locale.ROOT);
    df.setCalendar(c);
    Date lenDate = df.parse(lenient + "-01-01");
    df.setLenient(false);
    Date strDate = df.parse(strict + "-01-01");
    assertEquals(lenDate, strDate);
}
 
Example 9
public static boolean isDateValid(String date, String dateFormat) 
{
        try {
            DateFormat df = new SimpleDateFormat(dateFormat);
            df.setLenient(false); 
            df.parse(date);
            return true;
        } catch (ParseException e) {
            return false;
        }
}
 
Example 10
Source Project: AndroidProjects   File: HttpDate.java    License: MIT License 5 votes vote down vote up
@Override protected DateFormat initialValue() {
  // RFC 2616 specified: RFC 822, updated by RFC 1123 format with fixed GMT.
  DateFormat rfc1123 = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss 'GMT'", Locale.US);
  rfc1123.setLenient(false);
  rfc1123.setTimeZone(UTC);
  return rfc1123;
}
 
Example 11
Source Project: orientdb-etl   File: OCSVTransformer.java    License: Apache License 2.0 5 votes vote down vote up
private Object transformToDate(String fieldStringValue) {
  // DATE
  DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
  df.setLenient(true);
  Object fieldValue;
  try {
    fieldValue = df.parse(fieldStringValue);
  } catch (ParseException pe) {
    fieldValue = null;
  }
  return fieldValue;
}
 
Example 12
static DateFormat timeFormatter()
{
	DateFormat rv = new SimpleDateFormat("hh:mm a");
	rv.setLenient(false);
	return rv;
}
 
Example 13
public static Date parserDate(String date) throws ParseException {
    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    dateFormat.setLenient(false);
    return dateFormat.parse(date);
}
 
Example 14
@Override protected DateFormat initialValue()
{
   DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
   df.setLenient(false);
   return df;
}
 
Example 15
Source Project: mumu   File: SystemUserController.java    License: Apache License 2.0 4 votes vote down vote up
@InitBinder
public void initBinder(WebDataBinder binder) {
	DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
	dateFormat.setLenient(true);
	binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
}
 
Example 16
Source Project: gama   File: WebbUtils.java    License: GNU General Public License v3.0 3 votes vote down vote up
/**
 * Creates a new instance of a <code>DateFormat</code> for RFC1123 compliant dates. <br>
 * Should be stored for later use but be aware that this DateFormat is not Thread-safe! <br>
 * If you have to deal with dates in this format with JavaScript, it's easy, because the JavaScript Date object has
 * a constructor for strings formatted this way.
 *
 * @return a new instance
 */
public static DateFormat getRfc1123DateFormat() {
	final DateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.ENGLISH);
	format.setLenient(false);
	format.setTimeZone(TimeZone.getTimeZone("UTC"));
	return format;
}
 
Example 17
Source Project: DavidWebb   File: WebbUtils.java    License: MIT License 3 votes vote down vote up
/**
 * Creates a new instance of a <code>DateFormat</code> for RFC1123 compliant dates.
 * <br>
 * Should be stored for later use but be aware that this DateFormat is not Thread-safe!
 * <br>
 * If you have to deal with dates in this format with JavaScript, it's easy, because the JavaScript
 * Date object has a constructor for strings formatted this way.
 * @return a new instance
 */
public static DateFormat getRfc1123DateFormat() {
    DateFormat format = new SimpleDateFormat(
            "EEE, dd MMM yyyy HH:mm:ss z", Locale.ENGLISH);
    format.setLenient(false);
    format.setTimeZone(TimeZone.getTimeZone("UTC"));
    return format;
}
 
Example 18
/**
 * This form of getLocalDateTimeFormat is used to render times in a specific locale. Used to send event notification emails
 * 
 * @param locale locale
 * @param tz time zone
 * @return a DateFormat instance with short time format
 */
public static DateFormat getLocaleTimeFormat(Locale locale, TimeZone tz) {
    DateFormat df = getFormatProvider(locale).getTimeInstance(DateFormat.SHORT, locale);
    df.setLenient(false);
    df.setTimeZone(tz);
    return df;
}
 
Example 19
/**
 * Static method to get date-only DateFormat for output.  Uses short date format, 4-digit year.
 * Also used by FilterItem to get DateFormat to store report dates in US locale.
 * 
 * @param locale lcoale
 * @param tz time zone
 * @return a DateFormat.
 */
public static DateFormat getLocaleDateFormat(Locale locale, TimeZone tz) {
    DateFormat df = BaseLocalizer.convertTo4DigitYear(getFormatProvider(locale).getDateInstance(DateFormat.SHORT,
        locale), locale);
    df.setLenient(false);
    df.setTimeZone(tz);
    return df;
}
 
Example 20
Source Project: olat   File: Formatter.java    License: Apache License 2.0 3 votes vote down vote up
/**
 * formats the given date so it is friendly to read
 * 
 * @param d
 *            the date
 * @return a String with the formatted date
 */
public String formatDate(Date d) {
    DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, locale);
    df.setLenient(false);
    String da = df.format(d);
    return da;
}