这是我在做Java的项目开发过程中整理的一个单例的完整工具类,不同的项目会用到不同的方法,我把一些比较有用的方法都整理在了一起,其中大部分的方法都是我自己写的,但也参考了一些项目中的高手写代码的方法和思路,希望这些方法能对大家有帮助。以前都是参考别人的代码,这次我慢慢将自己在项目中写过的代码发布出来,也为大家做一点贡献。
祝大家新年快乐!
2010.10.28
这个类我又进行了一些修改,把与日期相关的操作整理到了《操作日期和时间的工具类》中,修正了利用MD5算法对字符串进行加密的方法,使其支持JDK1.6,并且还新增了一个MD5加密字符串的方法和一个可以去掉字符串两端全角和半角空格的方法。
我写代码Verstion都是从0.1开始的,这个是最新的版本(v1.1)也是就第十一个版本,如果以后有了更改再更新代码和版本。
大家如果发现什么Bug或有什么建议都可以提出来,为我的下一个版本添砖加瓦,谢谢!
祝大家新年快乐!
2010.10.28
这个类我又进行了一些修改,把与日期相关的操作整理到了《操作日期和时间的工具类》中,修正了利用MD5算法对字符串进行加密的方法,使其支持JDK1.6,并且还新增了一个MD5加密字符串的方法和一个可以去掉字符串两端全角和半角空格的方法。
/*
* @(#)CommonUtilities.java 2010-10-26
*
* Copyright 2010 BianJing,All rights reserved.
*/
package com.pagination.utils;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 用于处理公共数据的工具类
*
* @author BianJing<br/>
* E-mail:vipbooks@163.com
* @version 1.4 2010-10-26
*/
public class CommonUtilities {
private CommonUtilities() {
}
/**
* 检查字符串是否为Null或空字符串
*
* @param str
* 要检查的字符串
* @return boolean true:为空,false:不为空
*/
public static boolean isNull(String str) {
if (str == null || str.trim().length() == 0) {
return true;
}
return false;
}
/**
* 检查Collection是否为Null或没有元素
*
* @param 要检查的Collection及其子类
* @return boolean true:为空,false:不为空
*/
public static boolean isNull(Collection<?> collection) {
if (collection == null || collection.isEmpty()) {
return true;
}
return false;
}
/**
* 检查Map是否为Null或没有元素
*
* @param 要检查的Map及其子类
* @return boolean true:为空,false:不为空
*/
public static boolean isNull(Map<?, ?> map) {
if (map == null || map.isEmpty()) {
return true;
}
return false;
}
/**
* 检查指定字符串是否全数字
*
* @param str
* 要检查的字符串
* @return true:全数字,false:非全数字
*/
public static boolean validatorNumber(String str) {
str = str.trim();
int length = str.length();
char c = '\u0000';
for (int i = 0; i < length; i++) {
c = str.charAt(i);
if (c < '0' || c > '9') {
return false;
}
}
return true;
}
/**
* 让指定字符串进行给定的正则表达式验证
*
* @param str
* 要验证的字符串
* @param regExp
* 给定的正则表达式
* @return boolean
*/
public static boolean RegExpVerify(String str, String regExp) {
Pattern pattern = Pattern.compile(regExp);
Matcher matcher = pattern.matcher(str);
return matcher.find();
}
/**
* 字符串如果为空就给其一个指定的值
*
* @param source
* 原字符串值
* @param target
* 如果为空则替换成该目标值
* @return 字符串
*/
public static String changeNull(String source, String target) {
if (isNull(source)) {
return target;
} else {
return source;
}
}
/**
* 过滤网页文本
*
* @param html
* 要过滤的网页文本
* @return 过滤后的网页文本
*/
public static String filterHtml(String html) {
html = html.replaceAll("&", "&");
html = html.replaceAll("<", "<");
html = html.replaceAll(">", ">");
html = html.replaceAll(" ", " ");
html = html.replaceAll("'", "''");
html = html.replaceAll("\"", """);
html = html.replaceAll("\n", "<br/>");
return html;
}
/**
* 将文本内容过滤成网页文本
*
* @param text
* 要过滤的文本
* @return 过滤后的网页文本
*/
public static String toHtml(String text) {
text = text.replaceAll("&", "&");
text = text.replaceAll("<", "<");
text = text.replaceAll(">", ">");
text = text.replaceAll(" ", " ");
text = text.replaceAll("''", "'");
text = text.replaceAll(""", "\"");
text = text.replaceAll("<br/>", "\n");
return text;
}
/**
* 将整数转换成货币格式的字符串(#,##0)
*
* @param money
* 需转换的整数 (long)
* @return 货币格式字符串(#,##0)
*/
public static String getMoneyFormat(long money) {
DecimalFormat df = new DecimalFormat("#,##0");
return df.format(money);
}
/**
* 将浮点数转换成货币格式的字符串(#,##0.00)
*
* @param money
* 需转换的浮点数 (double)
* @return 货币格式字符串(#,##0.00)
*/
public static String getMoneyFormat(double money) {
DecimalFormat df = new DecimalFormat("#,##0.00");
return df.format(money);
}
/**
* 利用MD5算法对字符串进行加密
*
* @param str
* 待加密的字符串
* @return 加密后的字符串
*/
public static String md5CodeEncrypt(String str) {
char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F' };
byte[] tempStr = str.getBytes();
MessageDigest md = null;
try {
// 使用MD5创建MessageDigest对象
md = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
// 没有这种产生消息摘要的算法
e.printStackTrace();
}
md.update(tempStr);
byte[] result = md.digest();
int length = result.length;
char md5Code[] = new char[length * 2];
int k = 0;
for (int i = 0; i < length; i++) {
byte b = result[i];
md5Code[k++] = hexDigits[b >> 4 & 0xf];
md5Code[k++] = hexDigits[b & 0xf];
}
return new String(md5Code);
}
/**
* 利用MD5算法对字符串进行加密,生成32位的16进制字串
*
* @param str
* @return 加密后的字符串
*/
public static String encodeMD5Hex(String str) {
MessageDigest md = null;
StringBuffer md5Buf = null;
try {
md = MessageDigest.getInstance("MD5");
md.update(str.getBytes("US-ASCII"));
} catch (Exception e) {
e.printStackTrace();
}
byte[] digest = md.digest();
md5Buf = new StringBuffer();
for (int i = 0; i < digest.length; i++) {
md5Buf.append(Character.forDigit((digest[i] & 0xF0) >> 4, 16));
md5Buf.append(Character.forDigit((digest[i] & 0xF), 16));
}
return md5Buf.toString();
}
/**
* 创建带前缀的ID值,例如:生成数据库中的ID主健值。
*
* @param maxId
* 所有连续ID值中最大的ID, 程序将根据maxId生成比该maxId值大1的连续ID值。 <br/>
* 如:有GB000001、GB000002、GB000003这三个连续的ID值, 那么最大的ID值就是GB000003, <br/>
* 这时程序生成的连续ID值就是GB000004。
* @param headId
* 头ID标识。如:规定要生成的ID值是GB000001这种形式的值, 那么GB就是头ID标识,即前缀。
* @return 生成比maxId值大1的连续ID值。
*/
public static String buildAssignedId(String maxId, String headId) {
// 手动指派的ID值
String buildId = null;
// 补零
StringBuffer fillZero = null;
// 数字位的位数
int numberBit = maxId.replaceFirst(headId, "").length();
// 获得当前数字
int number = Integer.parseInt(maxId.replaceFirst(headId, ""));
// 获得下一位数字的位数
int nextNumBit = String.valueOf(number + 1).length();
// 产生下一位数字
number += 1;
// 创建手动指派的ID值
if (numberBit - nextNumBit > 0) {
fillZero = new StringBuffer();
// 补零
for (int i = 0; i < numberBit - nextNumBit; i++) {
fillZero.append(0);
}
buildId = headId + fillZero.toString() + number;
} else {
buildId = headId + number;
}
return buildId;
}
/**
* 批量创建带前缀的ID值,例如:生成数据库中的ID主健值。
*
* @param maxId
* 所有连续ID值中最大的ID, 程序将根据该maxId生成比该maxId值大1的连续ID值。<br/>
* 如:有GB000001、GB000002、GB000003这三个连续ID值, 那么最大的ID值就是GB000003,<br/>
* 这时程序生成的连续ID值就是GB000004。
* @param headId
* 头ID标识,如:规定要生成的ID值是GB000001这种形式的值, 那么GB就是头ID标识,即前缀。
* @param idNum
* 要批量生成的连续ID的数量。如要批量生成5个连续的ID,则该数量应该是5
* @return 生成比maxId大1的连续ID值的List列表
*/
public static List<String> buildAssignedIds(String maxId, String headId,
int idNum) {
// 已创建的ID列表
List<String> idList = new ArrayList<String>();
// 手动指派的ID值
String buildId = null;
// 补零
StringBuffer fillZero = null;
// 数字位的位数
int numberBit = maxId.replaceFirst(headId, "").length();
// 获得当前数字
int number = Integer.parseInt(maxId.replaceFirst(headId, ""));
// 获得下一位数字的位数
int nextNumBit = String.valueOf(number + 1).length();
// 产生下一位数字
for (int i = 0; i < idNum; i++) {
number += 1;
if (numberBit - nextNumBit > 0) {
if (fillZero == null)
fillZero = new StringBuffer();
// 补零
for (int j = 0; j < numberBit - nextNumBit; j++) {
fillZero.append(0);
}
buildId = headId + fillZero.toString() + number;
// 获得下一位数字的位数
nextNumBit = String.valueOf(number + 1).length();
// 重置fillZero
fillZero.delete(0, fillZero.length());
idList.add(buildId);
} else {
buildId = headId + number;
// 获得下一位数字的位数
nextNumBit = String.valueOf(number + 1).length();
idList.add(buildId);
}
}
return idList;
}
/**
* 去除字符串前后的半角和全角空格
*
* @param str
* 要去空格的字符串
* @return 去除前后的半角和全角空格后的字符串
*/
public static String trim(String str) {
int start = 0;
int len = str.length();
int end = len;
while ((start < len)
&& (str.charAt(start) == ' ' || str.charAt(start) == ' ')) {
start++;
}
while ((start < end)
&& (str.charAt(end - 1) == ' ' || str.charAt(end - 1) == ' ')) {
end--;
}
return ((start > 0) || (end < len)) ? str.substring(start, end) : str;
}
}
我写代码Verstion都是从0.1开始的,这个是最新的版本(v1.1)也是就第十一个版本,如果以后有了更改再更新代码和版本。
大家如果发现什么Bug或有什么建议都可以提出来,为我的下一个版本添砖加瓦,谢谢!