You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
library_manage_system/src/javabean/Util.java

90 lines
3.7 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package javabean; // 定义包名,用于组织类和接口
import java.math.BigInteger; // 导入BigInteger类用于处理大整数
import java.security.MessageDigest; // 导入MessageDigest类用于生成MD5摘要
import java.security.NoSuchAlgorithmException; // 导入NoSuchAlgorithmException异常类用于处理没有指定算法的情况
import java.text.SimpleDateFormat; // 导入SimpleDateFormat类用于格式化日期
import net.sf.json.JSONObject; // 导入JSONObject类用于处理JSON数据
public class Util { // 定义一个公共类Util
/**
* 主要用来计算json字符串中对象的个数
* @param str 输入的json字符串
* @param contain 要计算的对象标识符
* @return 返回对象的数量
*/
public static int getCountString(String str, String contain) {
int count = ( str.length()-str.replace(contain, "").length() ) / contain.length(); // 计算包含特定字符串的次数
return count; // 返回计数结果
}
/**
* 数据库取出来的datetime有 .0 ,用来去掉
* @param dateTime 输入的日期时间字符串
* @return 返回去除.0后的日期时间字符串
*/
public static String getFormatDateTime(String dateTime) {
if(dateTime != null && dateTime.indexOf(".0") != -1) { // 如果字符串不为空且包含".0"
return dateTime.substring(0, dateTime.length()-2); // 去除最后两个字符(即".0"
}else if(dateTime != null) { // 如果字符串不为空但不包含".0"
return dateTime; // 直接返回原字符串
}
return null; // 如果字符串为空返回null
}
/**
* 获取当前时间
* @return 返回格式化后的当前时间字符串
*/
public static String getCurrentTimeString() {
java.util.Date date = new java.util.Date(); // 创建当前日期对象
SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); // 定义日期格式
return dateFormat.format(date); // 返回格式化后的日期字符串
}
/*
* 返回json数据
*/
public static String jsonResponse(int code, String msg, String data) {
JSONObject jsonObject = new JSONObject(); // 创建JSON对象
jsonObject.put("code", code); // 添加code字段
jsonObject.put("msg", msg); // 添加msg字段
if( data!=null ) { // 如果data不为空
jsonObject.put("data", data); // 添加data字段
}
return jsonObject.toString(); // 返回JSON字符串
}
/*
* md5加密
*/
public static String stringToMD5(String plainText) {
byte[] secretBytes = null; // 定义字节数组用于存储MD5摘要
try {
secretBytes = MessageDigest.getInstance("md5").digest(
plainText.getBytes()); // 生成MD5摘要
} catch (NoSuchAlgorithmException e) { // 捕获没有该算法的异常
throw new RuntimeException("没有这个md5算法"); // 抛出运行时异常
}
String md5code = new BigInteger(1, secretBytes).toString(16); // 将字节数组转换为16进制字符串
for (int i = 0; i < 32 - md5code.length(); i++) { // 如果长度不足32位前面补0
md5code = "0" + md5code;
}
return md5code; // 返回MD5字符串
}
public static String passMd5(String password) {
String salt = "ew!.E"; // 定义盐值
return Util.stringToMD5(password +salt); // 返回加盐后的MD5字符串
}
public static void main(String[] args) {
System.out.println(Util.passMd5("admin")); // 测试MD5加密函数输出加密后的字符串
//java.util.Date date = new java.util.Date(); // 注释掉的代码:创建当前日期对象
//SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); // 注释掉的代码:定义日期格式
//System.out.println(dateFormat.format(date)); // 注释掉的代码:输出格式化后的日期字符串
}
}