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

The following examples show how to use java.text.DateFormat#setLenient() . 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 File: BaseLocalizer.java    From grammaticus with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
    * 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 2
Source File: DatePropertyHandler.java    From olat with 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 File: DateFormatNormalizer.java    From zephyr with 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
Source File: DateTimeUtils.java    From Bats with 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 5
Source File: BaseLocalizer.java    From grammaticus with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
    * 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 6
Source File: AbstractSettings.java    From hasor with 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 7
Source File: OCSVTransformer.java    From orientdb-etl with 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 8
Source File: AddressBookResultHandler.java    From barterli_android with Apache License 2.0 5 votes vote down vote up
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 9
Source File: JapaneseLenientEraTest.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
@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 10
Source File: UserService.java    From JDeSurvey with GNU Affero General Public License v3.0 5 votes vote down vote up
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 11
Source File: HttpDate.java    From AndroidProjects with 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 12
Source File: GenericCalendarImporter.java    From sakai with Educational Community License v2.0 4 votes vote down vote up
static DateFormat timeFormatter()
{
	DateFormat rv = new SimpleDateFormat("hh:mm a");
	rv.setLenient(false);
	return rv;
}
 
Example 13
Source File: DateUtils.java    From search-spring-boot-starter with Apache License 2.0 4 votes vote down vote up
public static Date parserDate(String date) throws ParseException {
    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    dateFormat.setLenient(false);
    return dateFormat.parse(date);
}
 
Example 14
Source File: DatatypeConverter.java    From mpxj with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override protected DateFormat initialValue()
{
   DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
   df.setLenient(false);
   return df;
}
 
Example 15
Source File: SystemUserController.java    From mumu with 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 File: BaseLocalizer.java    From grammaticus with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
/**
 * 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 17
Source File: Formatter.java    From olat with 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;
}
 
Example 18
Source File: BaseLocalizer.java    From grammaticus with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
/**
 * 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 19
Source File: WebbUtils.java    From DavidWebb with 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 20
Source File: WebbUtils.java    From gama with 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;
}