import java.text.ParseException;
import java.util.Collection;
import java.util.Map;

/**
* 常用的验证方法帮助类,提供对字符串,集合,数组,数值的验证 *
*
*/
public class ValidateHelper {
/**
* 判断一个字符串是否不是一个空字符串
*
* @param s 要判断的字符串
* @return 如果不为空返回true,否则返回false
*/
public static boolean isNotEmpty(String s) {
return ((s != null) && s.length() > 0);
}

/**
* 判断一个字符串是否是一个空字符串
*
* @param s 要判断的字符串
* @return 如果为空返回true,否则返回false
*/
public static boolean isEmpty(String s) {
return ((s == null) || (s.length() == 0));
}

/**
* 判断一个Collection类型的集合是否不是一个空集合
*
* @param c 要判断集合
* @return 如果不为空返回true,否则返回false
*/
public static boolean isNotEmpty(Collection c) {
return ((c != null) && (c.size() > 0));
}

/**
* 判断一个Collection类型的集合是否是一个空集合
*
* @param c 要判断集合
* @return 如果为空返回true,否则返回false
*/
public static boolean isEmpty(Collection c) {
return ((c == null) || (c.size() == 0));
}

/**
* 判断一个Map类型的集合是否不是一个空集合
*
* @param c 要判断的集合
* @return 如果不为空返回true,否则返回false
*/
public static boolean isNotEmpty(Map m) {
return ((m != null) && (m.size() > 0));
}

/**
* 判断一个Map类型的集合是否是一个空集合
*
* @param c 要判断的集合
* @return 如果为空返回true,否则返回false
*/
public static boolean isEmpty(Map m) {
return ((m == null) || (m.size() == 0));
}

/**
* 判断一个int类型的数组是否不是一个空数组
*
* @param c 要判断的数组
* @return 如果不为空返回true,否则返回false
*/
public static boolean isNotEmpty(int[] i) {
return ((i != null) && (i.length > 0));
}

/**
* 判断一个int类型的数组是否是一个空数组
*
* @param c 要判断的数组
* @return 如果为空返回true,否则返回false
*/
public static boolean isEmpty(int[] i) {
return ((i == null) || (i.length == 0));
}

/**
* 判断一个String类型的数组是否不是一个空数组
*
* @param c 要判断的数组
* @return 如果不为空返回true,否则返回false
*/
public static boolean isNotEmpty(String[] s) {
return ((s != null) && (s.length > 0));
}

/**
* 判断一个String类型的数组是否是一个空数组
*
* @param c 要判断的数组
* @return 如果为空返回true,否则返回false
*/
public static boolean isEmpty(String[] s) {
return ((s == null) || (s.length == 0));
}

/**
* 验证一个字符串是否是Double类型
*
* @param s 要验证的字符串
* @return 如果是Double类型则返回true,否则返回false
*/
public static boolean isDouble(String s) {
if (s == null || s.equals(""))
return false;
String num = "0123456789.";
if (s.indexOf('.') >= 0)
if (s.indexOf('.', s.indexOf('.') + 1) > 0)
return false;
for (int i = 0; i < s.length(); i++) {
if (num.indexOf(s.charAt(i)) < 0) {
return false;
} else {
try {
Double.parseDouble(s);
} catch (NumberFormatException e) {
return false;
}
}
}
return true;
}

/**
* 验证一个字符串是否是Float类型
*
* @param s 要验证的字符串
* @return 如果是Float类型则返回true,否则返回false
*/
public static boolean isFloat(String s) {
if (s == null || s.equals(""))
return false;
String num = "0123456789.";
if (s.indexOf('.') >= 0)
if (s.indexOf('.', s.indexOf('.') + 1) > 0)
return false;
for (int i = 0; i < s.length(); i++) {
if (num.indexOf(s.charAt(i)) < 0) {
return false;
} else {
try {
Float.parseFloat(s);
} catch (NumberFormatException e) {
return false;
}
}
}
return true;
}

/**
* 验证一个字符串是否是整形
*
* @param s 要验证的字符串
* @return 如果是整形则返回true,否则返回false
*/
public static boolean isInteger(String s) {
if (s == null || s.length() == 0) {
return false;
} else {
String str = "0123456789";
String num = "-0123456789";
if (num.indexOf(s.charAt(0)) < 0)
return false;
for (int i = 1; i < s.length(); i++) {
if (str.indexOf(s.charAt(i)) < 0) {
return false;
} else {
try {
Integer.parseInt(s);
} catch (NumberFormatException e) {
return false;
}
}
}
}
return true;
}

/**
* 验证一个字符串是否是一个.和一组数字组成
*
* @param s 要传入的数字字符串
* @return 如果是一个长类型数字返回true,否则返回false
*/
public static boolean isLongNumber(String s) {
if (s == null || s.equals(""))
return false;
String num = "0123456789.";
if (s.indexOf('.') >= 0)
if (s.indexOf('.', s.indexOf('.') + 1) > 0)
return false;
for (int i = 0; i < s.length(); i++) {
if (num.indexOf(s.charAt(i)) < 0)
return false;
}
return true;
}

/**
* 验证一个字符串是否是数字组成
*
* @param s 要验证的字符串
* @return 如果字符串是数字组成的则返回true,否则返回false
*/
public static boolean isNumber(String s) {
if (s == null || s.equals(""))
return false;
String num = "0123456789";
for (int i = 0; i < s.length(); i++) {
if (num.indexOf(s.charAt(i)) < 0)
return false;
}
return true;
}

/**
* 验证一个字符串是否一个合法日期,日期格式:yyyy-MM-dd
*
* @param date 要验证的字符串日期
* @return 如果字符串是一个合法的日期返回true,否则返回false
*/
public static boolean isDate(String date) {
java.text.SimpleDateFormat df = new java.text.SimpleDateFormat("yyyy-MM-dd");
try {
df.setLenient(false);
df.parse(date);
return true;
} catch (ParseException e) {
return false;
}
}

/**
* 验证一个字符串是否一个合法日期时间,日期时间格式:yyyy-MM-dd HH:mm:ss
*
* @param date 要验证的字符串日期时间
* @return 如果字符串是一个合法的日期时间返回true,否则返回false
*/
public static boolean isTimestamp(String time) {
java.text.SimpleDateFormat df = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
df.setLenient(false);
df.parse(time);
return true;
} catch (ParseException e) {
return false;
}
}

/**
* 根据字节数组指定的开始和结尾长度来计算字符串值
*
* @param bytes 字节数组
* @param begin 开始索引
* @param end 结束索引
* @return 转换后的字符串结果
*/
public static String getString(byte[] bytes, int begin, int end) throws RuntimeException {
byte[] newBytes = new byte[end - begin];
for (int i = begin, j = 0; i < end; i++, j++) {
byte c = bytes[i];
newBytes[j] = c;
}
return (new String(newBytes));
}

/**
* 根据字节数组指定的开始和结尾长度来计算字符串的字节长度
*
* @param bytes 字节数组
* @param begin 开始索引
* @param end 结束索引
* @return 转换后的字符串长度
*/
public static int getLength(byte[] bytes, int begin, int end) {
byte[] newBytes = new byte[end - begin];
try {
for (int i = begin, j = 0; i < end; i++, j++) {
byte b = bytes[i];
newBytes[j] = b;
}
} catch (RuntimeException ex) {
ex.printStackTrace();
}
return (newBytes.length);
}

}

java 常用的验证方法帮助类的更多相关文章

  1. Java国际化号码验证方法,国内手机号正则表达式

    Java国际化号码验证方法,国内手机号正则表达式 中国电信号段 133.149.153.173.177.180.181.189.199 中国联通号段 130.131.132.145.155.156.1 ...

  2. Java常用的输入输出方法

    对于经常上机刷题的来说,首先得解决输入输出方法,Java的输入输出流在Java学习过程的后面部分才会接触,但是我们可以掌握一些简单的,常用的输入输出方法 首先输出 大家最熟悉的莫过于输出方法,直接用S ...

  3. Java常用正则表达式验证工具类RegexUtils.java

    import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexUtils{ /** * 正则表达式 ...

  4. rails 常用的验证方法 validates (转)

    Agile Web Development with Rails 17.4 validation validate              在save的时候激活validate_on_create  ...

  5. java自定义注解注解方法、类、属性等等【转】

    http://anole1982.iteye.com/blog/1450421 http://www.open-open.com/doc/view/51fe76de67214563b20b385320 ...

  6. JAVA中域、方法、类的可见性

    多态在域的问题上是特殊的.我理解不了中文版的书直接叫域,看了英文原版,原版写的是fields,直接翻译虽然没错,但是出问题的变量不是域.特地查了what is the meaning of field ...

  7. java 常用类库:操作系统System类,运行时环境Runtime

    System类: System 类代表Java程序的运行平台,程序不能创建System类的对象, System类提供了一些类变量和类方法,允许直接通过 System 类来调用这些类变量和类方法. Sy ...

  8. java常用加密和解密工具类EncryptUtil.java

    package cn.util; import java.io.UnsupportedEncodingException; import java.security.MessageDigest; im ...

  9. java常用的正则表达式的工具类

    import com.google.common.base.Strings; import java.util.regex.Matcher;import java.util.regex.Pattern ...

随机推荐

  1. EBS R12 查询EBS用户相关SQL

    --R12查询EBS在线用户SQL  SELECT U.USER_NAME,        APP.APPLICATION_SHORT_NAME,        FAT.APPLICATION_NAM ...

  2. unix c 02

    环境变量 - 存储在内存中的信息,格式是映射,作用就是 帮助系统 进行一些工作,一般是 查找某个东西. 预处理指令:#warning #error #pragma 使用程序直接调用库文件的函数(动态编 ...

  3. hdu4010-Query on The Trees(lct分裂合并加值查询最大值)

    代码 #include<cstdio> #include<cstring> #include<string> #include<vector> #inc ...

  4. Mysql数据库乱码与编码问题筛查

    最近接连遇到数据库编码问题,让你的系统本来像个美丽的姑娘却忽然发现她不识字一样难受,其实很多时候是编码的问题,而mysql(特别地)设计编码的地方很多,在这里做一个筛查: 1 mysql编码 用下面的 ...

  5. PHP批量审核后台

    /*批量审核方法*/ function setOn_all() { if($_POST) { $p=M('news'); $data=array(); $i=0; foreach ($_POST as ...

  6. python学习之路-11 多线程、多进程、协程

    python内置队列模块 queue queue的四种队列 q = queue.Queue() # 先进先出队列 q = queue.LifoQueue() # 后进先出队列 q = queue.Pr ...

  7. AJAX实现google搜索建议实战

    搜索建议实战的目标是为了输入搜索内容,动态的进行匹配,效果图如下: 整体思路: 在客户端搜索框中触发onkeyup事件, 随时向PHP服务器请求当前输入框中的内容, PHP服务器获取到keywords ...

  8. eclipse里添加类似myeclipse打开当前操作目录

    1.开打eclipse ide,依次run->external tools->external tools configuration 2.在Program下,new一个自己定义的prog ...

  9. Android控件TextView的实现原理分析

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/8636153 在前面一个系列的文章中,我们以窗口 ...

  10. (转)swfobject.js 详细解说

    一直想对这个应用做个总结,今天偶然百度到这个效果,为此做个笔记. 用这个js的好处: 1.IE中没有讨厌的虚框问题了.2.提供了完善的版本检测功能,如果版本不够则显示其他东西,比如图片或文字.3.易于 ...