java.text.DateFormat.setLenient()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(7.1k)|赞(0)|评价(0)|浏览(159)

本文整理了Java中java.text.DateFormat.setLenient()方法的一些代码示例,展示了DateFormat.setLenient()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。DateFormat.setLenient()方法的具体详情如下:
包路径:java.text.DateFormat
类名称:DateFormat
方法名:setLenient

DateFormat.setLenient介绍

[英]Specifies whether or not date/time parsing shall be lenient. With lenient parsing, the parser may use heuristics to interpret inputs that do not precisely match this object's format. With strict parsing, inputs must match this object's format.
[中]指定日期/时间解析是否应宽松。通过宽松的解析,解析器可以使用启发式来解释与该对象格式不完全匹配的输入。通过严格解析,输入必须与此对象的格式匹配。

代码示例

代码示例来源:origin: square/okhttp

@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;
 }
};

代码示例来源:origin: stackoverflow.com

final static String DATE_FORMAT = "dd-MM-yyyy";

public static boolean isDateValid(String date) 
{
    try {
      DateFormat df = new SimpleDateFormat(DATE_FORMAT);
      df.setLenient(false);
      df.parse(date);
      return true;
    } catch (ParseException e) {
      return false;
    }
}

代码示例来源:origin: stackoverflow.com

Date parseDate(String maybeDate, String format, boolean lenient) {
 Date date = null;
 // test date string matches format structure using regex
 // - weed out illegal characters and enforce 4-digit year
 // - create the regex based on the local format string
 String reFormat = Pattern.compile("d+|M+").matcher(Matcher.quoteReplacement(format)).replaceAll("\\\\d{1,2}");
 reFormat = Pattern.compile("y+").matcher(reFormat).replaceAll("\\\\d{4}");
 if ( Pattern.compile(reFormat).matcher(maybeDate).matches() ) {
  // date string matches format structure, 
  // - now test it can be converted to a valid date
  SimpleDateFormat sdf = (SimpleDateFormat)DateFormat.getDateInstance();
  sdf.applyPattern(format);
  sdf.setLenient(lenient);
  try { date = sdf.parse(maybeDate); } catch (ParseException e) { }
 } 
 return date;
} 
// used like this:
Date date = parseDate( "21/5/2009", "d/M/yyyy", false);

代码示例来源:origin: stackoverflow.com

localSimpleDateFormat = new ThreadLocal<SimpleDateFormat>() {
  protected SimpleDateFormat initialValue() {
    return new SimpleDateFormat();
localSimpleDateFormat = new ThreadLocal<SimpleDateFormat>() {
  protected SimpleDateFormat initialValue() {
    return new SimpleDateFormat(pattern);
localSimpleDateFormat = new ThreadLocal<SimpleDateFormat>() {
  protected SimpleDateFormat initialValue() {
    return new SimpleDateFormat(pattern, formatSymbols);
return localSimpleDateFormat.get().parse(source);
localSimpleDateFormat.get().setLenient(lenient);

代码示例来源:origin: commons-beanutils/commons-beanutils

: new SimpleDateFormat(pattern, locale);
formatter.setLenient(isLenient);

代码示例来源:origin: stackoverflow.com

SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy");
df.setLenient(false);
df.parse("03/88/2013"); // Throws an exception

代码示例来源:origin: org.assertj/assertj-core

private static DateFormat strictDateFormatForPattern(String pattern) {
 DateFormat dateFormat = new SimpleDateFormat(pattern);
 dateFormat.setLenient(false);
 return dateFormat;
}

代码示例来源:origin: wildfly/wildfly

: new SimpleDateFormat(pattern, locale);
formatter.setLenient(isLenient);

代码示例来源:origin: apache/flink

dateFormat.setLenient(false);
final Date d = dateFormat.parse(s, pp);
if (null == d) {
  return null;

代码示例来源:origin: stackoverflow.com

DateFormat fromFormat = new SimpleDateFormat("yyyy-MM-dd");
fromFormat.setLenient(false);
DateFormat toFormat = new SimpleDateFormat("dd-MM-yyyy");
toFormat.setLenient(false);
String dateStr = "2011-07-09";
Date date = fromFormat.parse(dateStr);
System.out.println(toFormat.format(date));

代码示例来源:origin: prestodb/presto

@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;
 }
};

代码示例来源:origin: commons-validator/commons-validator

formatter.setLenient(false);
  formatter.parse(value);
} catch(ParseException e) {
  return false;

代码示例来源:origin: stackoverflow.com

public static Date getTwitterDate(String date) throws ParseException {

 final String TWITTER="EEE MMM dd HH:mm:ss ZZZZZ yyyy";
 SimpleDateFormat sf = new SimpleDateFormat(TWITTER);
 sf.setLenient(true);
 return sf.parse(date);
 }

public static void main (String[] args) throws java.lang.Exception
  {
   System.out.println(getTwitterDate("Thu Dec 3 18:26:07 +0000 2010"));          
  }

代码示例来源:origin: joel-costigliola/assertj-core

private static DateFormat strictDateFormatForPattern(String pattern) {
 DateFormat dateFormat = new SimpleDateFormat(pattern);
 dateFormat.setLenient(false);
 return dateFormat;
}

代码示例来源:origin: commons-validator/commons-validator

formatterShort.setLenient(false);
formatterDefault.setLenient(false);
  date = formatterShort.parse(value);
} catch (ParseException e) {
  date = formatterDefault.parse(value);

代码示例来源:origin: stackoverflow.com

public static boolean isValid(String text) {
  if (text == null || !text.matches("\\d{4}-[01]\\d-[0-3]\\d"))
    return false;
  SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
  df.setLenient(false);
  try {
    df.parse(text);
    return true;
  } catch (ParseException ex) {
    return false;
  }
}

代码示例来源:origin: com.squareup.okhttp3/okhttp

@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;
 }
};

代码示例来源:origin: commons-beanutils/commons-beanutils

/**
 * 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;
}

代码示例来源:origin: stackoverflow.com

SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy, HH:mm");
formatter.setLenient(false);

Date curDate = new Date();
long curMillis = curDate.getTime();
String curTime = formatter.format(curDate);

String oldTime = "05.01.2011, 12:45";
Date oldDate = formatter.parse(oldTime);
long oldMillis = oldDate.getTime();

代码示例来源:origin: apache/hive

@Override
 protected DateFormat initialValue() {
  DateFormat val = new SimpleDateFormat("yyyy-MM-dd");
  val.setLenient(false); // Without this, 2020-20-20 becomes 2021-08-20.
  val.setTimeZone(TimeZone.getTimeZone("UTC"));
  return val;
 }
};

相关文章