📄 commontools.java
字号:
sum = sum + (c -48)*w[i];
}
if(c > 57){
sum = sum + (c -55)*w[i];
}
}
int c9 = 11- sum%11;
String c9Str = String.valueOf(c9);
if(c9 == 10)
c9Str = "X";
if(c9 == 11)
c9Str = "0";
bl = orgId.endsWith(c9Str);
if(!bl){
errorStr = "组织机构代码输入不对["+orgId+"]。";
return bl;
}
//System.out.println("sum:"+sum+":"+c9);
System.out.println(bl+":"+errorStr);
return bl;
}
/**
* 年份格式验证(长度为六位 例:200211)
* @return
*/
public boolean year(String year)
{
Pattern patternYear = Pattern.compile("^((19|(2\\d))\\d{2})$");
Matcher matcherYear = patternYear.matcher(year);
if ( !matcherYear.matches() )
{
ActionContext.getActionContext().addSimpleError("年份格式不正确!");
return false;
}
return true;
}
/**
* 报文日期验证(长度为六位 例:200211)
* @return
*/
public boolean dateYm(String date)
{
Pattern patternRptDate = Pattern.compile("^((19|(2\\d))\\d{2})(0[1-9]|1[012])$");
Matcher matcherRptDate = patternRptDate.matcher(date);
if ( !matcherRptDate.matches() )
{
ActionContext.getActionContext().addSimpleError("年月日期不正确!");
return false;
}
return true;
}
/**
* 手机号码验证(139 159开头)
* @return
*/
public boolean mobilePhone(String mobilephone)
{
if (mobilephone.length()!=0){
Pattern patternMobilePhone = Pattern.compile("^0?1(3\\d|59)\\d{8}$");
Matcher matcherMobilePhone = patternMobilePhone.matcher(mobilephone);
if (!matcherMobilePhone.matches())
{
ActionContext.getActionContext().addSimpleError("手机号格式不正确!");
return false;
}
}
return true;
}
/**
* 固定电话验证
* @return
*/
public boolean phone(String phone)
{
if (phone.length()!=0){
Pattern patternPhone = Pattern.compile("(0?13\\d{9})|(0[1-9]\\d{1,2}-?[1-9]\\d{6,7})(-[1-9]\\d{1,2})?");
Matcher matcherPhone = patternPhone.matcher(phone);
if (!matcherPhone.matches() )
{
ActionContext.getActionContext().addSimpleError("电话号格式不正确!");
return false;
}
}
return true;
}
/**
* 邮件格式验证
* @return
*/
public boolean email(String email)
{
if (email.length()!=0){
Pattern patternEmail = Pattern.compile("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$");
Matcher matcherEmail = patternEmail.matcher(email);
if (!matcherEmail.matches())
{
ActionContext.getActionContext().addSimpleError("EMAIL地址格式不正确!");
return false;
}
}
return true;
}
/**
* 网址格式验证
* @return
*/
public boolean web(String web)
{
if (web.length()!=0){
Pattern patternWeb = Pattern.compile("^\\w+([-+.]\\w+)*\\.\\w+([-+.]\\w+)*\\.\\w+([-+.]\\w+)*$");
Matcher matcherWeb = patternWeb.matcher(web);
if(!matcherWeb.matches())
{
ActionContext.getActionContext().addSimpleError("网址格式不正确!");
return false;
}
}
return true;
}
/**
* 邮证编码格式验证
* @return
*/
public boolean zipCode(String zipcode)
{
if (zipcode.length()!=0){
Pattern patternZipCode = Pattern.compile("^[1-9]\\d{5}$");
Matcher matcherZipCode = patternZipCode.matcher(zipcode);
if (!matcherZipCode.matches()){
ActionContext.getActionContext().addSimpleError("邮编格式不正确!");
return false;
}
}
return true;
}
/**
* 贷款卡编码格式验证
* @return
*/
public boolean isLoanCard(String cardno){
String financeCode = cardno.trim() ;
if (financeCode.length()!=0) {
Pattern pattern = Pattern.compile("^[0-9A-Z]{16}$");
Matcher matcher = pattern.matcher(financeCode);
if(matcher.matches()){
int[] w_i = new int[14];
int[] c_i = new int[14];
int s = 0;
w_i[0] = 1;
w_i[1] = 3;
w_i[2] = 5;
w_i[3] = 7;
w_i[4] = 11;
w_i[5] = 2;
w_i[6] = 13;
w_i[7] = 1;
w_i[8] = 1;
w_i[9] = 17;
w_i[10] = 19;
w_i[11] = 97;
w_i[12] = 23;
w_i[13] = 29;
for (int j = 0; j < 14; j++) {
if ( financeCode.charAt(j) >= '0' && financeCode.charAt(j) <= '9') {
c_i[j] = financeCode.charAt(j)-'0';
}
else if ( financeCode.charAt(j) >= 'A' && financeCode.charAt(j) <= 'Z') {
c_i[j] = financeCode.charAt(j) - 'A' + 10;
}
else{
System.out.println("贷款卡编码位数错误!");
return false;
}
s = s + w_i[j] * c_i[j];
}
int c = 1 + (s % 97);
int checkid = ( financeCode.charAt(14) - '0') * 10 + financeCode.charAt(15) - '0';
if ( c != checkid ) {
System.out.println("贷款卡编码错误!");
return false;
}
}else{
System.out.println("贷款卡编码不合法!");
return false;
}
}
return true;
}
/**
* 金融机构代码格式验证
* @return
*/
public boolean orgCode(String obj) {
String financecode = obj.trim();
if(financecode=="")
return true;
int s, M, i, temp, k;
M = 10;
s = M;
k = 9;
for (i = k; i >= 0; i--) {
if( financecode.charAt(k - i) >= '0' && financecode.charAt(k - i) <= '9' ){
temp = financecode.charAt(k - i) - '0';
}
else if( financecode.charAt(k - i) >= 'A' && financecode.charAt(k - i) <= 'Z' ){
temp = 0;
}
else if( financecode.charAt(k - i) >= 'a' && financecode.charAt(k - i) <= 'z' ) {
temp = 0;
}
else {
ActionContext.getActionContext().addSimpleError("金融机构代码错误!");
return false;
}
if (((s + temp) % M) == 0) {
// s = (M * 2) % (M + 1); ??
s = 9;
} else {
s = (((s + temp) % M) * 2) % (M + 1);
}
}
s = M + 1 - s;
if (s == 10) {
s = 0;
}
if ( ( s == financecode.charAt(10) - '0' ) ||
s == 11 && financecode.charAt(10) == 'X' ) {
return true;
} else {
ActionContext.getActionContext().addSimpleError("金融机构代码错误!");
return false;
}
}
public String strToStrDate(String str){
String strDate="1900-01-01";
if(str.length()==8){
strDate=str.substring(0, 4)+"-"+str.substring(4, 6)+"-"+str.substring(6, 8);
}
return strDate;
}
public static boolean wuComOrgId(String orgId){
boolean wu = false;
if(orgId.length()==10){
if(orgId.substring(0, 2).equals("WU")&&orgId.substring(8, 10).equalsIgnoreCase("-0")){
wu = true;
}
}
return wu;
}
public static boolean wuLoanCard(String cardno){
boolean wu = false;
if(cardno.length()==16){
if(cardno.substring(0, 2).equals("WU")){
wu = true;
}
}
return wu;
}
public static String intDate2Str(int dateInt){
String date = Integer.toString(dateInt);
StringBuffer bufferStr = new StringBuffer();
Integer.toString(dateInt);
bufferStr.append(date.substring(0, 4)).append("-").append(date.substring(4, 6)).append("-").append(date.substring(6, 8)).toString();
return bufferStr.toString();
}
public static void main(String []a){
try {
System.out.println(""+CommonTools.toOldIDCard(" 320106198008182828 "));
} catch (Exception e) {
e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -