美文网首页NC65工作
检查字符串是否是时间格式和金额格式

检查字符串是否是时间格式和金额格式

作者: 铁皮农夫 | 来源:发表于2019-06-04 09:28 被阅读0次

/**

* 检查时间格式是否正确

* @param 要验证的日期格式

* @return true 格式正确  false 格式错误

* @author fenggfa

*/

public static boolean isValidDate(String str) {

boolean convertSuccess=true;

    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

    try {

    format.setLenient(false);

        format.parse(str);

    } catch (ParseException e) {

        convertSuccess=false;

    }

    return convertSuccess;

}

/**

* 判断字符串是否是金额

* @param str

* @return

*/

public static boolean isNumber(String str){

        java.util.regex.Pattern pattern=java.util.regex.Pattern.compile("^(([1-9]{1}\\d*)|([0]{1}))(\\.(\\d){0,2})?$"); // 判断小数点后2位的数字的正则表达式

        java.util.regex.Matcher match=pattern.matcher(str);

        if(match.matches()==false){

          return false;

        }

        else{

          return true;

        }

}

相关文章

网友评论

    本文标题:检查字符串是否是时间格式和金额格式

    本文链接:https://www.haomeiwen.com/subject/mpokxctx.html