本文整理汇总了Java中java.text.DateFormat.setLenient方法的典型用法代码示例。如果您正苦于以下问题:Java DateFormat.setLenient方法的具体用法?Java DateFormat.setLenient怎么用?Java DateFormat.setLenient使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.text.DateFormat
的用法示例。
在下文中一共展示了DateFormat.setLenient方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: parse
import java.text.DateFormat; //导入方法依赖的package包/类
/**
* Parse a String into a <code>Calendar</code> object
* using the specified <code>DateFormat</code>.
*
* @param sourceType The type of the value being converted
* @param targetType The type to convert the value to
* @param value The String date value.
* @param format The DateFormat to parse the String value.
*
* @return The converted Calendar object.
* @throws ConversionException if the String cannot be converted.
*/
private Calendar parse(final Class<?> sourceType, final Class<?> targetType, final String value, final DateFormat format) {
logFormat("Parsing", format);
format.setLenient(false);
final ParsePosition pos = new ParsePosition(0);
final Date parsedDate = format.parse(value, pos); // ignore the result (use the Calendar)
if (pos.getErrorIndex() >= 0 || pos.getIndex() != value.length() || parsedDate == null) {
String msg = "Error converting '" + toString(sourceType) + "' to '" + toString(targetType) + "'";
if (format instanceof SimpleDateFormat) {
msg += " using pattern '" + ((SimpleDateFormat)format).toPattern() + "'";
}
if (log().isDebugEnabled()) {
log().debug(" " + msg);
}
throw new ConversionException(msg);
}
final Calendar calendar = format.getCalendar();
return calendar;
}
示例2: isValid
import java.text.DateFormat; //导入方法依赖的package包/类
@Override
public boolean isValid(CharSequence sequence, ConstraintValidatorContext context) {
if (sequence == null || sequence.length() == 0) {
return true;
}
final String value = sequence.toString();
for (String pattern : annotation.patterns()) {
final DateFormat format = new SimpleDateFormat(pattern);
format.setLenient(annotation.lenient());
try {
format.parse(value);
return true;
} catch (@SuppressWarnings("unused") ParseException e) {
continue;
}
}
return false;
}
示例3: parseDateFormat
import java.text.DateFormat; //导入方法依赖的package包/类
/**
* 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;
}
示例4: parse
import java.text.DateFormat; //导入方法依赖的package包/类
/**
* Convert the specified locale-sensitive input object into an output object of the
* specified type.
*
* @param value The input object to be converted
* @param pattern The pattern is used for the convertion
* @return the converted Date value
*
* @throws org.apache.commons.beanutils.ConversionException
* if conversion cannot be performed successfully
* @throws ParseException if an error occurs parsing
*/
@Override
protected Object parse(final Object value, String pattern) throws ParseException {
// Handle Date
if (value instanceof java.util.Date) {
return value;
}
// Handle Calendar
if (value instanceof java.util.Calendar) {
return ((java.util.Calendar)value).getTime();
}
if (locPattern) {
pattern = convertLocalizedPattern(pattern, locale);
}
// Create Formatter - use default if pattern is null
final DateFormat formatter = pattern == null ? DateFormat.getDateInstance(DateFormat.SHORT, locale)
: new SimpleDateFormat(pattern, locale);
formatter.setLenient(isLenient);
// Parse the Date
final ParsePosition pos = new ParsePosition(0);
final String strValue = value.toString();
final Object parsedValue = formatter.parseObject(strValue, pos);
if (pos.getErrorIndex() > -1) {
throw new ConversionException("Error parsing date '" + value +
"' at position="+ pos.getErrorIndex());
}
if (pos.getIndex() < strValue.length()) {
throw new ConversionException("Date '" + value +
"' contains unparsed characters from position=" + pos.getIndex());
}
return parsedValue;
}
示例5: initialValue
import java.text.DateFormat; //导入方法依赖的package包/类
@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;
}
示例6: initialValue
import java.text.DateFormat; //导入方法依赖的package包/类
@Override protected DateFormat initialValue() {
// Date format specified by RFC 7231 section 7.1.1.1.
DateFormat rfc1123 = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss 'GMT'", Locale.US);
rfc1123.setLenient(false);
rfc1123.setTimeZone(UTC);
return rfc1123;
}
示例7: tryConvert
import java.text.DateFormat; //导入方法依赖的package包/类
public Date tryConvert(String text, String pattern) {
DateFormat dateFormat = new SimpleDateFormat(pattern);
dateFormat.setLenient(false);
try {
return dateFormat.parse(text);
} catch (ParseException ex) {
logger.debug(ex.getMessage(), ex);
}
return null;
}
示例8: getDateFormat
import java.text.DateFormat; //导入方法依赖的package包/类
protected DateFormat getDateFormat(Locale locale) {
DateFormat dateFormat = createDateFormat(locale);
if (this.timeZone != null) {
dateFormat.setTimeZone(this.timeZone);
}
dateFormat.setLenient(this.lenient);
return dateFormat;
}
示例9: getFormat
import java.text.DateFormat; //导入方法依赖的package包/类
/**
* <p>Returns a <code>DateFormat</code> for the specified Locale.</p>
*
* @param locale The locale a <code>DateFormat</code> is required for,
* system default if null.
* @return The <code>DateFormat</code> to created.
*/
protected Format getFormat(Locale locale) {
DateFormat formatter = null;
if (dateStyle >= 0 && timeStyle >= 0) {
if (locale == null) {
formatter = DateFormat.getDateTimeInstance(dateStyle, timeStyle);
} else {
formatter = DateFormat.getDateTimeInstance(dateStyle, timeStyle, locale);
}
} else if (timeStyle >= 0) {
if (locale == null) {
formatter = DateFormat.getTimeInstance(timeStyle);
} else {
formatter = DateFormat.getTimeInstance(timeStyle, locale);
}
} else {
int useDateStyle = dateStyle >= 0 ? dateStyle : DateFormat.SHORT;
if (locale == null) {
formatter = DateFormat.getDateInstance(useDateStyle);
} else {
formatter = DateFormat.getDateInstance(useDateStyle, locale);
}
}
formatter.setLenient(false);
return formatter;
}
示例10: initBinder
import java.text.DateFormat; //导入方法依赖的package包/类
@InitBinder
public void initBinder(WebDataBinder binder) {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
dateFormat.setLenient(true);
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
}
示例11: initialValue
import java.text.DateFormat; //导入方法依赖的package包/类
protected DateFormat initialValue() {
DateFormat rfc1123 = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss 'GMT'", Locale.US);
rfc1123.setLenient(false);
rfc1123.setTimeZone(HttpDate.access$000());
return rfc1123;
}
示例12: setDateFromString
import java.text.DateFormat; //导入方法依赖的package包/类
/**
* Sets the date from a string representation.
*
* @param dateString
* the date to set
* @throws MbedCloudException
* if string cannot be interpreted as a date
*/
public synchronized void setDateFromString(String dateString) throws MbedCloudException {
final DateFormat format = REQUEST_DATE_FORMAT;
format.setLenient(true);
setDate(TranslationUtils.convertTimestamp(dateString, format));
}