Chinaunix首页 | 论坛 | 博客
  • 博客访问: 535386
  • 博文数量: 298
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 3077
  • 用 户 组: 普通用户
  • 注册时间: 2019-06-17 10:57
文章分类

全部博文(298)

文章存档

2022年(96)

2021年(201)

2019年(1)

我的朋友

分类: Java

2021-09-22 11:25:45


点击(此处)折叠或打开


  1. package org.fh.util;

  2. import java.util.Random;
  3. import java.util.regex.Matcher;
  4. import java.util.regex.Pattern;

  5. /**
  6.  * 说明:常用工具
  7.  * 作者:FH Admin
  8.  * from:fhadmin.cn
  9.  */
  10. public class Tools {
  11.     
  12.     /**
  13.      * 随机生成六位数验证码
  14.      * @return
  15.      */
  16.     public static int getRandomNum(){
  17.          Random r = new Random();
  18.          return r.nextInt(900000)+100000;//(Math.random()*(999999-100000)+100000)
  19.     }
  20.     
  21.     /**
  22.      * 随机生成四位数验证码
  23.      * @return
  24.      */
  25.     public static int getRandomNum4(){
  26.          Random r = new Random();
  27.          return r.nextInt(9000)+1000;
  28.     }
  29.     
  30.     /**
  31.      * 检测字符串是否不为空(null,"","null")
  32.      * @param s
  33.      * @return 不为空则返回true,否则返回false
  34.      */
  35.     public static boolean notEmpty(String s){
  36.         return s!=null && !"".equals(s) && !"null".equals(s);
  37.     }
  38.     
  39.     /**
  40.      * 检测字符串是否为空(null,"","null")
  41.      * @param s
  42.      * @return 为空则返回true,不否则返回false
  43.      */
  44.     public static boolean isEmpty(String s){
  45.         return s==null || "".equals(s) || "null".equals(s);
  46.     }
  47.     
  48.     /**
  49.      * 字符串转换为字符串数组
  50.      * @param str 字符串
  51.      * @param splitRegex 分隔符
  52.      * @return
  53.      */
  54.     public static String[] str2StrArray(String str,String splitRegex){
  55.         if(isEmpty(str)){
  56.             return null;
  57.         }
  58.         return str.split(splitRegex);
  59.     }
  60.     
  61.     /**
  62.      * 用默认的分隔符(,)将字符串转换为字符串数组
  63.      * @param str    字符串
  64.      * @return
  65.      */
  66.     public static String[] str2StrArray(String str){
  67.         return str2StrArray(str,",\\s*");
  68.     }
  69.     
  70.     /**
  71.      * 验证邮箱
  72.      * @param email
  73.      * @return
  74.      */
  75.      public static boolean checkEmail(String email){
  76.      boolean flag = false;
  77.      try{
  78.      String check = "^([a-z0-9A-Z]+[-|_|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
  79.      Pattern regex = Pattern.compile(check);
  80.      Matcher matcher = regex.matcher(email);
  81.      flag = matcher.matches();
  82.      }catch(Exception e){
  83.      flag = false;
  84.      }
  85.      return flag;
  86.      }
  87.     
  88.      /**
  89.      * 验证手机号码
  90.      * @param mobiles
  91.      * @return
  92.      */
  93.      public static boolean checkMobileNumber(String mobileNumber){
  94.      boolean flag = false;
  95.      try{
  96.      Pattern regex = Pattern.compile("^(((13[0-9])|(15([0-3]|[5-9]))|(18[0,5-9]))\\d{8})|(0\\d{2}-\\d{8})|(0\\d{3}-\\d{7})$");
  97.      Matcher matcher = regex.matcher(mobileNumber);
  98.      flag = matcher.matches();
  99.      }catch(Exception e){
  100.      flag = false;
  101.      }
  102.      return flag;
  103.      }
  104.     
  105.     /**
  106.      * 检测KEY是否正确
  107.      * @param paraname 传入参数
  108.      * @param FKEY        接收的 KEY
  109.      * @return 为空则返回true,不否则返回false
  110.      */
  111.     public static boolean checkKey(String paraname, String FKEY){
  112.         paraname = (null == paraname)? "":paraname;
  113.         return MD5.md5(paraname+DateUtil.getDays()+",fh,").equals(FKEY);
  114.     }
  115.     
  116.     public static void main(String[] args) {
  117.         System.out.println(getRandomNum());
  118.     }
  119.     
  120. }


阅读(627) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~