/**
* 判断是否为整数
* @param value
* @param allowNegative 是否允许负数
* @return
*/
public static boolean isInteger(String value, boolean allowNegative) {
String regex = "";
if (allowNegative) {
regex = "^0|(-)?[1-9]|(-)?[1-9]\\d*$";
} else {
regex = "^[1-9]\\d*|0$";
}
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(value);
return matcher.matches();
}
/**
* 判断是否为浮点数
* @param value
* @return
*/
public static boolean isFloatNumber(String value) {
Pattern pattern = Pattern.compile("^([1-9]\\d*|[1-9]\\d*\\.\\d*|0\\.\\d*|0)$");
Matcher matcher = pattern.matcher(value);
return matcher.matches();
}
/**
* 判断给定字符是否是中文
* @param value
* @return
*/
public static boolean isChinese(char value) {
Pattern pattern = Pattern.compile("[\u4e00-\u9fa5]");
Matcher matcher = pattern.matcher(String.valueOf(value));
return matcher.matches();
}
/***
* 返回 保留指定小数位数后处理的值
* @param digit 保留的小数位数
* @param value 要操作的值
* @return
*/
public static String digits(int digit , double value) {
NumberFormat nf = NumberFormat.getNumberInstance();
nf.setMaximumFractionDigits(digit);
nf.setMinimumFractionDigits(digit);
return nf.format(value);
}
/***
* 验证 的值 是否符合要保存小数位数的要求
* @param digit 要保存的小数位数
* param value 验证的值
* @return
*/
public static boolean digitsIsPattern(int digit, String value) {
boolean rs = true;
Pattern p = Pattern.compile("^*.[0-9]{"+digit+"}$");
Matcher m = p.matcher(value);
return m.matches();
}
/**
* 求得某个集合的平均值
* @param list
* @return
*/
public static double average(List<Double> list) {
double value = 0;
for (Iterator<Double> iterator = list.iterator(); iterator.hasNext();) {
value += iterator.next();
}
return value / list.size();
}
/**
* 返回指定格式的日期字符串
* @param date 日期
* @param style 样式
* @return
*/
public static String getFormattedDate(Date date, int style) {
String formatStyle = "";
switch (style) {
case STYLE1:
formatStyle = "yyyy-MM-dd";
break;
case STYLE2:
formatStyle = "yyyyMMdd";
break;
case STYLE3:
formatStyle = "yyyy/MM/dd";
break;
case STYLE4:
formatStyle = "yyyy-MM";
break;
case STYLE5:
formatStyle = "yyyy-MM-dd hh:mm:ss";
break;
}
DateFormat format = new SimpleDateFormat(formatStyle);
return format.format(date);
}
/**
* 得到与今天相差指定日的日期
* @param differ 相差数
* @return
*/
public static Date getDay(int differ) {
return new Date(new Date().getTime() + differ * 24 * 60 * 60 * 1000L);
}
/**
* 删除指定文件或文件夹
* @author wangjian
*
*/
public class DelAllFileUtil {
//删除指定文件夹下所有文件,param path 文件夹完整绝对路径
public static boolean delAllFile(String path) {
boolean flag = false;
File file = new File(path);
if (!file.exists()) {
return flag;
}
if (!file.isDirectory()) {
return flag;
}
String[] tempList = file.list();
File temp = null;
for (int i = 0; i < tempList.length; i++) {
if (path.endsWith(File.separator)) {
temp = new File(path + tempList[i]);
} else {
temp = new File(path + File.separator + tempList[i]);
}
if (temp.isFile()) {
temp.delete();
}
if (temp.isDirectory()) {
delAllFile(path + "/" + tempList[i]);//先删除文件夹里面的文件
// delFolder(path + "/" + tempList[i]);//再删除空文件夹
flag = true;
}
}
return flag;
}
//删除文件夹,param folderPath 文件夹完整绝对路径
public static void delFolder(String folderPath) {
try {
delAllFile(folderPath); //删除完里面所有内容
String filePath = folderPath;
filePath = filePath.toString();
java.io.File myFilePath = new java.io.File(filePath);
myFilePath.delete(); //删除空文件夹
} catch (Exception e) {
e.printStackTrace();
}
}
}
//过滤Vector中的重复元素
public static Vector filterOutVector(Vector vector){
Vector foVector = new Vector();
for(int i=0;i<vector.size();i++){
if(!foVector.contains(vector.get(i)))
foVector.add(vector.get(i));
}
return foVector;
}