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. RBL, UBL, Uboot的关系

    RBL, UBL, Uboot的关系 1)RBL=ROM Bootloader,UBL=user Bootloader. 2)RBL为TI固化在芯片ROM中的bootloader,OMAP上电启动过后 ...

  2. c#提出中文首字母

           ; i < len; i++)             {                 myStr += getSpell(strText.Substring(i, ));   ...

  3. (转)linux下fork的运行机制

    转载http://www.cnblogs.com/leoo2sk/archive/2009/12/11/talk-about-fork-in-linux.html 给出如下C程序,在linux下使用g ...

  4. Unity重要的函数

    Awake 当一个脚本实例被载入时Awake被调用. Start Start仅在Update函数第一次被调用前调用. Update 当MonoBehaviour启用时,其Update在每一帧被调用. ...

  5. Linux下高效数据恢复软件extundelete应用实战

    作为一名运维人员,保证数据的安全是根本职责,所以在维护系统的时候,要慎之又慎,但是有时难免会出现数据被误删除的情况,在这个时候改如何快速.有效地恢复数据呢?本文我们就来介绍一下Linux系统下常用的几 ...

  6. IOS消息推送情况总结

      App没有启动的时候,接受到了消息通知.这个时候操作系统会按默认方式来展示一个alert,在App Icon上标记一个数字 .当程序处于关闭状态收到推送消息时,点击图标或消息栏会调用 - (BOO ...

  7. CI框架源代码阅读笔记3 全局函数Common.php

    从本篇開始.将深入CI框架的内部.一步步去探索这个框架的实现.结构和设计. Common.php文件定义了一系列的全局函数(一般来说.全局函数具有最高的载入优先权.因此大多数的框架中BootStrap ...

  8. 关于VS2008中的targetver.h文件

    targerver.h文件的作用: 定义程序运行的环境,如限制程序只能在XP下运行,限制程序在只能在Vin7下运行 或限制程序只能在XP以上系统运行,或限制程序只能在Server2003以上系统运行. ...

  9. SQLLoader4(数据文件中的列与表中列不一致情况-filler)

    A.数据文件中字段个数少于表中列字段个数,但数据文件中缺少的列,在表定义中可以为空.----- 这种情况是比较简单的,只需要将数据文件中数据对应的列的名字写到控制文件中即可.因为SQL*Loader是 ...

  10. 失效的URL访问限制(转)

    * 经常URL的保护仅仅是连接到该页面的链接不出现在未授权的用户面前.然而,一个有动机的.熟练的或者仅仅是幸运的黑客可能会找到并访问这些网页 , 调用这些功能并查看数据.在应用程序中,通过隐匿来实现安 ...