@ApiOperation("Convert使用:类型转换工具类")@GetMapping(value ="/covert")publicCommonResultcovert(){//转换成字符串int a =1;String aStr =Convert.toStr(a);//转换为指定类型数组String[] b ={"1","2","3","4"};Integer[] bArr =Convert.toIntArray(b);//转换成日期对象String dateStr ="2017-07-22";Date date =Convert.toDate(dateStr);//转换成列表String[] strArr ={"a","b","c","d"};List<String> strList =Convert.toList(String.class, strArr);returnCommonResult.success(null,"操作成功");}
2、日期时间
@ApiOperation("DateUtil使用:日期时间工具")@GetMapping("/dateUtil")publicCommonResultdateUtil(){//Date、long、Calendar之间的转换//当前时间Date date =DateUtil.date();//Calendar转Date
date =DateUtil.date(Calendar.getInstance());//时间戳转Date
date =DateUtil.date(System.currentTimeMillis());//自动识别格式转换String dateStr ="2012-12-21";
date =DateUtil.parse(dateStr);//自定义格式转换DateUtil.parse(dateStr,"yyyy-MM-dd");//格式化输出日期String format =DateUtil.format(date,"yyyy-MM-dd");//获取年的部分int year =DateUtil.year(date);//获取月份,从0开始。int month =DateUtil.month(date);//获取某天的开始时间 2012-12-21 23:59:59Date beginOfDay =DateUtil.beginOfDay(date);//获取某天的结束时间 2012-12-21 00:00:00Date endOfDay =DateUtil.endOfDay(date);//计算偏移后的日期Date newDate =DateUtil.offset(date,DateField.DAY_OF_MONTH,2);//计算日期中间的偏移量long betweenDay =DateUtil.between(date, newDate,DateUnit.DAY);//2天returnCommonResult.success(null,"操作成功!");}
@ApiOperation("Validator使用:字段验证器")@GetMapping("/validator")publicCommonResultvalidator(){//判断是否为邮箱地址boolean result =Validator.isEmail("wangnaixing@qq.com");
LOGGER.info("Validator isEmail:{}",result);//判断是否为手机号码
result =Validator.isMobile("18154622909");
LOGGER.info("Validator isMobile:{}",result);//判断是否为IPV4地址
result =Validator.isIpv4("192.168.3.101");
LOGGER.info("Validator isIpv4:{}",result);//判断是否为汉字
result =Validator.isChinese("你好");
LOGGER.info("Validator isChinese:{}",result);//判断是否为身份证号(18位中国)
result =Validator.isCitizenId("123456");
LOGGER.info("Validator isCitizenId:{}",result);//判断是否为URL
result =Validator.isUrl("http://www.baidu.com");
LOGGER.info("Validator isUrl:{}",result);//判断是否为生日
result =Validator.isBirthday("2020-12-14");
LOGGER.info("Validator isBirthday:{}",result);returnCommonResult.success(null,"操作成功!");}
12、密码加密
@ApiOperation("DigestUtil使用:摘要算法工具类")@GetMapping("/digestUtil")publicCommonResultdigestUtil(){String password ="123456";//计算MD5摘要值,并转为16进制字符串String result =DigestUtil.md5Hex(password);
LOGGER.info("digestUtil md5Hex:{}",result);//计算SHA-256摘要值,并转为16进制字符串
result =DigestUtil.sha256Hex(password);
LOGGER.info("digestUtil sha256Hex:{}",result);//生成Bcrypt加密后的密文,并检验。String hashPwd =DigestUtil.bcrypt(password);boolean check =DigestUtil.bcryptCheck(password, hashPwd);
LOGGER.info("digestUtil bcryptCheck:{}",check);returnCommonResult.success(null,"操作成功!");}