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

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

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

SimpleDateFormat.setLenient介绍

暂无

代码示例

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

/**
 * Creates a new date formatter with Farrago specific options. Farrago
 * parsing is strict and does not allow values such as day 0, month 13, etc.
 *
 * @param format {@link SimpleDateFormat}  pattern
 */
public static SimpleDateFormat newDateFormat(String format) {
  SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.ROOT);
  sdf.setLenient(false);
  return sdf;
}

代码示例来源:origin: opentripplanner/OpenTripPlanner

public synchronized static Date parseDate(SimpleDateFormat sdf, String string) {
  sdf.setLenient(false);
  try {
    return sdf.parse(string);
  } catch (Exception e) {
    // log.debug(string + " was not recognized by " + format.toString());
  }
  return null;
}

代码示例来源:origin: pentaho/pentaho-kettle

private boolean IsDate( String str ) {
 // TODO: What about other dates? Maybe something for a CRQ
 try {
  SimpleDateFormat fdate = new SimpleDateFormat( "yyyy/MM/dd" );
  fdate.setLenient( false );
  fdate.parse( str );
 } catch ( Exception e ) {
  return false;
 }
 return true;
}

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

public static Date parse(final String str) throws ParseException {
    SimpleDateFormat parser = new SimpleDateFormat(DATE_TIME_FORMAT, Locale.US);
    parser.setLenient(false);
    return parser.parse(str);
  }
}

代码示例来源:origin: com.thoughtworks.xstream/xstream

public Object newInstance() {
  SimpleDateFormat dateFormat = new SimpleDateFormat(formatString, locale);
  dateFormat.setLenient(lenient);
  return dateFormat;
}

代码示例来源:origin: ZHENFENG13/My-Blog

public static Date convertToDate(String input) {
  Date date = null;
  if(null == input) {
    return null;
  } else {
    Iterator var2 = dateFormats.iterator();
    while(var2.hasNext()) {
      SimpleDateFormat format = (SimpleDateFormat)var2.next();
      try {
        format.setLenient(false);
        date = format.parse(input);
      } catch (ParseException var5) {
        ;
      }
      if(date != null) {
        break;
      }
    }
    return date;
  }
}

代码示例来源:origin: pentaho/pentaho-kettle

public static int decodeTime( String s, String dateFormat ) throws Exception {
 SimpleDateFormat f = new SimpleDateFormat( dateFormat );
 TimeZone utcTimeZone = TimeZone.getTimeZone( "UTC" );
 f.setTimeZone( utcTimeZone );
 f.setLenient( false );
 ParsePosition p = new ParsePosition( 0 );
 Date d = f.parse( s, p );
 if ( d == null ) {
  throw new Exception( "Invalid time value " + dateFormat + ": \"" + s + "\"." );
 }
 return (int) d.getTime();
}

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

@Override
 protected SimpleDateFormat initialValue() {
  SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
  simpleDateFormat.setLenient(false);
  simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
  return simpleDateFormat;
 }
};

代码示例来源:origin: javaee-samples/javaee7-samples

@Override
  public Person processItem(Object t) {
    System.out.println("processItem: " + t);

    StringTokenizer tokens = new StringTokenizer((String) t, ",");

    String name = tokens.nextToken();
    String date;

    try {
      date = tokens.nextToken();
      format.setLenient(false);
      format.parse(date);
    } catch (ParseException e) {
      return null;
    }

    return new Person(id++, name, date);
  }
}

代码示例来源:origin: pentaho/pentaho-kettle

/**
 * 
 * @param dateString
 *          date string for parse
 * @param dateFormat
 *          format which should be applied for string
 * @return {@link java.util.Date} converted from dateString by format
 * @throws ParseException
 *           if we can not parse date string
 */
public static Date getDateFromStringByFormat( String dateString, String dateFormat ) throws ParseException {
 if ( dateFormat == null ) {
  throw new ParseException( "Unknown date format. Format is null. ", 0 );
 }
 if ( dateString == null ) {
  throw new ParseException( "Unknown date string. Date string is null. ", 0 );
 }
 SimpleDateFormat simpleDateFormat = new SimpleDateFormat( dateFormat );
 simpleDateFormat.setLenient( false ); // Don't automatically convert invalid date.
 return simpleDateFormat.parse( dateString );
}

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

@Override
 protected SimpleDateFormat initialValue() {
  SimpleDateFormat 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;
 };
};

代码示例来源:origin: org.eclipse.jgit/org.eclipse.jgit

private static Date parse_simple(String dateStr,
    ParseableSimpleDateFormat f, Locale locale)
    throws ParseException {
  SimpleDateFormat dateFormat = getDateFormat(f, locale);
  dateFormat.setLenient(false);
  return dateFormat.parse(dateStr);
}

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

/**
 * Attempt to parse an input string with the allowed formats, returning
 * null if none of the formats work.
 */
private static Date testFormats (String in, String[] formats)
 throws ParseException
{
 for (int i = 0; i <formats.length; i++)
 {
  try
  {
   SimpleDateFormat dateFormat = new SimpleDateFormat(formats[i]);
   dateFormat.setLenient(false);          
   return dateFormat.parse(in);
  }
  catch (ParseException pe)
  {
  }
 }
 return null;
}

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

public static String format(final Date date) {
  SimpleDateFormat parser = new SimpleDateFormat(DATE_TIME_FORMAT, Locale.US);
  parser.setLenient(false);
  return parser.format(date);
}

代码示例来源:origin: nice-swa/my-site

public static Date convertToDate(String input) {
  Date date = null;
  if(null == input) {
    return null;
  } else {
    Iterator var2 = dateFormats.iterator();
    while(var2.hasNext()) {
      SimpleDateFormat format = (SimpleDateFormat)var2.next();
      try {
        format.setLenient(false);
        date = format.parse(input);
      } catch (ParseException var5) {
        ;
      }
      if(date != null) {
        break;
      }
    }
    return date;
  }
}

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

/**
 *  Get the full name or abbreviation of the month or day.
 */
private static String getNameOrAbbrev(String in, 
                   String[] formatsIn,
                   String formatOut)
 throws ParseException
{
 for (int i = 0; i <formatsIn.length; i++) // from longest to shortest.
 {
  try
  {
   SimpleDateFormat dateFormat = new SimpleDateFormat(formatsIn[i], Locale.ENGLISH);
   dateFormat.setLenient(false);
   Date dt = dateFormat.parse(in);          
   dateFormat.applyPattern(formatOut);
   return dateFormat.format(dt);
  }
  catch (ParseException pe)
  {
  }
 }
 return "";
}
/**

代码示例来源:origin: org.apache.ant/ant

/**
 * return a lenient date format set to GMT time zone.
 * @param pattern the pattern used for date/time formatting.
 * @return the configured format for this pattern.
 */
private static DateFormat createDateFormat(String pattern) {
  SimpleDateFormat sdf = new SimpleDateFormat(pattern);
  TimeZone gmt = TimeZone.getTimeZone("GMT");
  sdf.setTimeZone(gmt);
  sdf.setLenient(true);
  return sdf;
}

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

/**
 * <p>Checks if the field is a valid date.  The pattern is used with
 * <code>java.text.SimpleDateFormat</code>.  If strict is true, then the
 * length will be checked so '2/12/1999' will not pass validation with
 * the format 'MM/dd/yyyy' because the month isn't two digits.
 * The setLenient method is set to <code>false</code> for all.</p>
 *
 * @param value The value validation is being performed on.
 * @param datePattern The pattern passed to <code>SimpleDateFormat</code>.
 * @param strict Whether or not to have an exact match of the datePattern.
 * @return true if the date is valid.
 */
public boolean isValid(String value, String datePattern, boolean strict) {
  if (value == null
      || datePattern == null
      || datePattern.length() <= 0) {
    return false;
  }
  SimpleDateFormat formatter = new SimpleDateFormat(datePattern);
  formatter.setLenient(false);
  try {
    formatter.parse(value);
  } catch(ParseException e) {
    return false;
  }
  if (strict && (datePattern.length() != value.length())) {
    return false;
  }
  return true;
}

代码示例来源:origin: xuxueli/xxl-job

@InitBinder
public void initBinder(WebDataBinder binder) {
  SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  dateFormat.setLenient(false);
  binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
}

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

SimpleDateFormat parser = new SimpleDateFormat();
parser.setLenient(lenient);
ParsePosition pos = new ParsePosition(0);
for (int i = 0; i < parsePatterns.length; i++) {
  Date date = parser.parse(str2, pos);
  if (date != null && pos.getIndex() == str2.length()) {
    return date;

相关文章

SimpleDateFormat类方法