package com.rui.util;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
/**
*
* @ClassName: StrUtils
* @Description: 工具类
* @author poseidon
* @date 2015年10月23日 下午8:13:45
* @version V1.0.0
*/
public class StrUtils {
/**
*
* @Title: isEmpty
* @Description: 空判断
* @param content
* @return boolean
*/
public static boolean isEmpty(String content){
return (content==null || content.equals(""))?true:false;
}
/**
*
* @Title: isNotEmpty
* @Description: 非空判断
* @param content
* @return boolean
*/
public static boolean isNotEmpty(String content){
return !isEmpty(content);
}
/**
*
* @Title: formatDate
* @Description: 格式化日期类
* @param date
* @param pattern
* @return String
*/
public static String formatDate(Date date,String pattern){
if(date!=null){
String dateString = new SimpleDateFormat(pattern).format(date);
return dateString;
}else{
return "";
}
}
/**
*
* @Title: getExt
* @Description: 获取文件的后缀
* @param name 文件名称
* @param flag true有点false没点
* @return String
*/
public static String getExt(String name,boolean flag){
if(isNotEmpty(name)){
String ext = null;
if(flag){
ext = name.substring(name.lastIndexOf("."), name.length());
}else{
ext = name.substring(name.lastIndexOf(".")+1, name.length());
}
return ext;
}else{
return "";
}
}
/**
*
* @Title: generateFileName
* @Description: 为上传文件自动分配文件名称,避免重复
* @param fileName
* @param randomNum
* @param dataPattern
* @return String
*/
public static String generateFileName(String fileName,int randomNum,String dataPattern) {
// 获得当前时间
DateFormat format = new SimpleDateFormat(dataPattern);
// 转换为字符串
String formatDate = format.format(new Date());
// 随机生成文件编号
int random = new Random().nextInt(randomNum);
// 获得文件后缀名称
int position = fileName.lastIndexOf(".");
String extension = fileName.substring(position);
// 组成一个新的文件名称
return formatDate + random + extension;
}
/**
*
* @Title: countFileSize
* @Description: 根据File文件的长度统计文件的大小
* @param fileSize
* @return String
*/
public static String countFileSize(long fileSize) {
String fileSizeString = "";
try {
DecimalFormat df = new DecimalFormat("#.00");
long fileS = fileSize;
if (fileS == 0) {
fileSizeString = "0KB";
} else if (fileS < 1024) {
fileSizeString = df.format((double) fileS) + "B";
} else if (fileS < 1048576) {
fileSizeString = df.format((double) fileS / 1024) + "KB";
} else if (fileS < 1073741824) {
fileSizeString = df
.format(((double) fileS / 1024 / 1024) - 0.01)
+ "MB";
} else {
fileSizeString = df.format((double) fileS / 1024 / 1024 / 1024)
+ "G";
}
} catch (Exception e) {
e.printStackTrace();
}
return fileSizeString;
}
/**
*
* @Title: conversionSpecialCharacters
* @Description: 把两个反斜线转换成正斜线
* @param string
* @return String
*/
public static String conversionSpecialCharacters(String string) {
return string.replaceAll("\\\\", "/");
}
public static void main(String[] args) {
}
}