1 处理字符串过滤
public static String filterDealwithAssignmentString(String s){
String result = "";
try{
if(s == null){
result = "";
}
else{
result = s.trim().replaceAll("\r\n", "");
result = s.replaceAll("\r", "");
result = result.replaceAll("\n", "");
result.replaceAll("\\r\\n", "");
s = s.replaceAll("\\'", "\\\\\\'");
s = s.replaceAll("\\:", "\\\\\\:");
s = s.replaceAll("\\\"", "\\\\\\\"");
s = s.replace('~', ' ').replace(':', ' ').replaceAll(" ","");
Pattern p = Pattern.compile("\\s*|\t|\r|\n");
Matcher m = p.matcher(s);
s = m.replaceAll("");
result=s; }
}
catch(Exception e){
e.printStackTrace();
result = "";
}
return result;
}
2 过滤字符串空格换行
public static String filterStringNK(String s){
String result = "";
try{
if(s == null){
result = "";
}
else{
// result = s.replaceAll("\r\n", "");
result = s.replaceAll("\r", "");
result = result.replaceAll("\n", "");
result = result.replaceAll(" ", "");
}
}
catch(Exception e){
e.printStackTrace();
result = "";
}
return result;
}
3 处理时间过滤
public static String filterDealwithAssignmentDate(Date date){
String result = "";
try{
if(date == null){
result = "";
}
else{
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
result = simpleDateFormat.format(date);
}
}
catch(Exception e){
e.printStackTrace();
result = "";
}
return result;
}
4 转化天时分秒
public static String shifen(int f)
{
if(f==0){
return "0";
}
String result="",tian="",shi="",fen="",miao="";
if (f / 60 / 60 >= 24) {
tian = f / 60 / 60 / 24 + "天";
shi = f / 60 / 60 % 24 + "时";
fen = f / 60 % 60 + "分";
} else {
if (f / 60 >= 60) {
shi = f / 60 / 60 + "时";
fen = f / 60 % 60 + "分";
} else {
fen = f / 60 + "分";
}
}
miao = f % 60 + "秒";
if ("0".equals(tian) || "0天".equals(tian)) {
shi = "";
}
if("0".equals(shi) || "0时".equals(shi))
{
shi = "";
}
if("0".equals(fen) || "0分".equals(fen))
{
fen = "";
}
if ("0".equals(miao) || "0秒".equals(miao)){
miao = "";
}
result=tian + shi + fen + miao;
return result;
}
5 数字转换成分/秒
public static String fenmiao(int f){
String result="",fen="",miao="";
if (f / 60 >= 60) {
fen = f / 60+ "分";
} else {
fen = f / 60 + "分";
}
miao = f % 60 + "秒";
if("0".equals(fen) || "0分".equals(fen))
{
fen = "";
}
if ("0".equals(miao) || "0秒".equals(miao)){
miao = "";
}
result= fen + miao;
return result;
}
6 处理任务时间过滤
public static String dateToString(Date date, String format){
String result = "";
try{
if(date == null){
result = "";
}
else{
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
result = simpleDateFormat.format(date);
}
}
catch(Exception e){
e.printStackTrace();
result = "";
}
return result;
}
public static String dateToString(Object date, String format){
String result = "";
try{
if(date == null){
result = "";
}
else{
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
result = simpleDateFormat.format(date);
}
}
catch(Exception e){
e.printStackTrace();
result = "";
}
return result;
}
public static Date objectToDate(Object date, String format){
try {
if(date == null || "".equals(date)){
return null;
}
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
return simpleDateFormat.parse(date.toString());
}
catch (ParseException e) {
e.printStackTrace();
}
return null;
}
7 过滤字符串
public static String filterString1(Object s){
if(s == null){
return "";
}
else{
return s.toString().replaceAll("\\t|\r|\n", "");
}
}
8 转Double
public static Double toDouble(Object o){
if(o == null){
return null;
}
if("".equals(o.toString())){
return null;
}
return Double.parseDouble(o.toString());
}
9 判断输入的字符串只包含数字,可以匹配整数和浮点数
public static boolean IsNumber(String input) throws Exception{
String str= "^-?\\d+$|^(-?\\d+)(\\.\\d+)?$";
Pattern pattern = Pattern.compile(str);
Matcher matcher = pattern.matcher(input);
if (matcher.find())
{
// System.out.println(matcher.group());
return true;
}
return false;
}
10 只允许字母和数字,清除掉所有特殊字符
public static String StringFilter2(String str){
try{
String regEx"[`~!@$%^&()+=|{}':;',\\[\\].<>/?~!@¥%……&()——+|{}【】‘;:”“’。,、?]"; Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher(str);
str =m.replaceAll("").trim();
return str.replace("\"", ""); //去掉引号
}catch(Exception e){
e.printStackTrace();
return null;
}
}
11 打散获取最后一个值
public static String getSplitLast(String str,String splitValue){
String[] s = str.split(splitValue);
StringBuilder sb = new StringBuilder();
for(int i=0;i<s.length;i++){
sb.setLength(0);
sb.append(s[i]);
}
return sb.toString();
}
12 获取当月第一天
public static String getFirstDayOfMonth(){
String str = "";
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
Calendar lastDate = Calendar.getInstance();
lastDate.set(Calendar.DATE,1);//设为当前月的1号
str=sdf.format(lastDate.getTime());
return str;
}
13 计算当月最后一天,返回字符串
public static String getDefaultDay(){
String str = "";
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
Calendar lastDate = Calendar.getInstance();
lastDate.set(Calendar.DATE,1);//设为当前月的1号
lastDate.add(Calendar.MONTH,1);//加一个月,变为下月的1号
lastDate.add(Calendar.DATE,-1);//减去一天,变为当月最后一天
str=sdf.format(lastDate.getTime());
return str;
}
14 保留两位有效数字
public static Double toroundDouble(Double d){
BigDecimal b = new BigDecimal(d);
d=b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
return d;
}