本文整理匯總了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));
}