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. 【转】vim文件编码和乱码处理

    原文网址:http://edyfox.codecarver.org/html/vim_fileencodings_detection.html 在 Vim 中,有四个与编码有关的选项,它们是:file ...

  2. Ubuntu 16.04 LTS 正式发布:系统将持续更新5年

    Canonical 刚刚正式发布了Ubuntu 16.04 LTS (Xenial Xerus),这是一个长期支持版本,官方会提供长达5年的技术支持(包括常规更新/Bug修复/安全升级),一直到202 ...

  3. Jquery局部打印插件

    局部打印插件 jquery.PrintArea.js js代码 (function ($) {     var printAreaCount = 0;     $.fn.printArea = fun ...

  4. py练习

    功能类似pop def myPop():    l=[]    a=raw_input('a:')    for i in a:        l.append(i)    l.pop()    pr ...

  5. Ant命令行操作

    Ant命令行操作 Ant构建文件可以将项目编译,打包,測试,它是Apache软件基金会jakarta文件夹中的一个子项目,具有跨平台性,操作简单,并且非常easy上手. 关于Ant执行,能够在项目中找 ...

  6. RMAN-FORMAT-CONFIGURE及动态性能表

    一.FORMAT参数在备份过程中,可指定format参数来自定义备份片段的命令规则,比如: RMAN> BACKUP DATABASE FORMAT 'D:\BACKUP\%U'; RMAN&g ...

  7. BFC,IFC,GFC,FFC的定义及功能

    What's FC?一定不是KFC,FC的全称是:Formatting Contexts,是W3C CSS2.1规范中的一个概念.它是页面中的一块渲染区域,并且有一套渲染规则,它决定了其子元素将如何定 ...

  8. PHP session 跨子域问题总结

    Session主要分两部分: 一个是Session数据,该数据默认情况下是存放在服务器的tmp文件下的,是以文件形式存在 另一个是标志着Session数据的Session Id,Session ID, ...

  9. mvc Html.RenderAction方法解析

    @{Html.RenderAction("Listview--控制器里面方法", "Home--控制器名", new { cid = "bda347f ...

  10. React-Native做一个文本输入框组件

    我又回来啦! 由于最近一直在做公司的项目,而且比较急.如今项目已经迭代到第三期,可以缓一缓了... 说实话,最近一直再用android做开发,而且时间也不宽裕,react-native有点生疏了. 好 ...