import java.text.*; import java.util.*; publicclass StringFormat { privatestatic SimpleDateFormat dateFormat =new SimpleDateFormat(); privatestatic DecimalFormat numberFormat =new DecimalFormat(); /**//**//**//** * Returns true if the specified date string represents a valid * date in the specified format, using the default Locale. * * @param dateString a String representing a date/time. * @param dateFormatPattern a String specifying the format to be used * when parsing the dateString. The pattern is expressed with the * pattern letters defined for the java.text.SimpleDateFormat class. * @return true if valid, false otherwise */ publicstatic boolean isValidDate(String dateString, String dateFormatPattern) { Date validDate =null; synchronized (dateFormat) { try{ dateFormat.applyPattern(dateFormatPattern); dateFormat.setLenient(false); validDate = dateFormat.parse(dateString); } catch (ParseException e) { // Ignore and return null } } return validDate !=null; } /**//**//**//** * Returns true if the specified number string represents a valid * integer in the specified range, using the default Locale. * * @param numberString a String representing an integer * @param min the minimal value in the valid range * @param max the maximal value in the valid range * @return true if valid, false otherwise */ publicstatic boolean isValidInteger(String numberString, int min, int max) { Integer validInteger =null; try{ Number aNumber = numberFormat.parse(numberString); int anInt = aNumber.intValue(); if (anInt >= min && anInt <= max) { validInteger =new Integer(anInt); } } catch (ParseException e) { // Ignore and return null } return validInteger !=null; } /**//**//**//** * Returns true if the string is in the format of a valid SMTP * mail address: only one at-sign, except as the first or last * character, no white-space and at least one dot after the * at-sign, except as the first or last character. * <p> * Note! This rule is not always correct (e.g. on an intranet it may * be okay with just a name) and it does not guarantee a valid Internet * email address but it takes care of the most obvious SMTP mail * address format errors. * * @param mailAddr a String representing an email address * @return true if valid, false otherwise */ publicstatic boolean isValidEmailAddr(String mailAddr) { if (mailAddr ==null) { returnfalse; } boolean isValid =true; mailAddr = mailAddr.trim(); // Check at-sign and white-space usage int atSign = mailAddr.indexOf('@'); if (atSign ==-1|| atSign ==0|| atSign == mailAddr.length() -1|| mailAddr.indexOf('@', atSign +1) !=-1|| mailAddr.indexOf('') !=-1|| mailAddr.indexOf('\t') !=-1|| mailAddr.indexOf('\n') !=-1|| mailAddr.indexOf('\r') !=-1) { isValid =false; } // Check dot usage if (isValid) { mailAddr = mailAddr.substring(atSign +1); int dot = mailAddr.indexOf('.'); if (dot ==-1|| dot ==0|| dot == mailAddr.length() -1) { isValid =false; } } return isValid; } publicstatic Date toDate(String dateString, String dateFormatPattern) throws ParseException { Date date =null; if (dateFormatPattern ==null) { dateFormatPattern ="yyyy-MM-dd"; } synchronized (dateFormat) { dateFormat.applyPattern(dateFormatPattern); dateFormat.setLenient(false); date = dateFormat.parse(dateString); } return date; } /**//** * Converts a String to a Number, using the specified pattern. * (see java.text.NumberFormat for pattern description) and the * default Locale. * * @param numString the String to convert * @param numFormatPattern the pattern * @return the corresponding Number * @exception ParseException, if the String doesn't match the pattern */ publicstatic Number toNumber(String numString, String numFormatPattern) throws ParseException { Number number =null; if (numFormatPattern ==null) { numFormatPattern ="######.##"; } synchronized (numberFormat) { numberFormat.applyPattern(numFormatPattern); number = numberFormat.parse(numString); } return number; }