总结自己最近项目中常用的各种类型转换代码(高人勿喷)
2012-10-27 17:22
836 查看
1 处理字符串过滤
2 过滤字符串空格换行
3 处理时间过滤
4 转化天时分秒
5 数字转换成分/秒
6 处理任务时间过滤
7 过滤字符串
8 转Double
9 判断输入的字符串只包含数字,可以匹配整数和浮点数
10 只允许字母和数字,清除掉所有特殊字符
11 打散获取最后一个值
12 获取当月第一天
13 计算当月最后一天,返回字符串
14 保留两位有效数字
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; }
相关文章推荐
- 总结一下刚刚写的程序中的C++各种类型的转换string,QString,char*, BYTE等等
- 关于常用 MYSQL 聚合函数,其他函数 ,类型转换,运算符 总结
- 1 查找并总结:错误类型对应的http代码。2 总结一下常用web.config公共配置。
- c# 中各种类型的常用转换方式
- javascript各种类型数据在表达式中转换成布尔型值的规则总结
- [JAVA入门之错误总结-2]项目创建-各种项目类型之间的区别
- Android UI 常用项目总结------/*自己编写*/
- 2010.6.15 常用数据类型转换总结
- C++类中各种不同类型成员根据是否static 、是否const类型的初始化方法不尽相同,写代码时经常搞乱,网上搜了一下资料,全部总结一下。一、例子 [cpp] view plaincopy ---
- SVN 的使用:用两次就会了,很简单.注意总结【①做完自己代码,首先右键项目>team >“与资源库同步”,把队友的代码更新下来(而不是直接提交),②整合完设置"冲突已解决",才能提交】===
- ADF中常用各种类型转换
- 把项目中常用的小工具做个总结吧,方便自己以后用到
- 数据分析中常用数据类型转换总结
- ASP.NET MVC WebApi 返回数据类型序列化控制(json,xml) 用javascript在客户端删除某一个cookie键值对 input点击链接另一个页面,各种操作。 C# 往线程里传参数的方法总结 TCP/IP 协议 用C#+Selenium+ChromeDriver 生成我的咕咚跑步路线地图 (转)值得学习百度开源70+项目
- mybatis使用generator自己主动生成代码时的类型转换
- javascript各种类型数据在表达式中转换成布尔型值的规则总结
- VC++中各种类型的相互转换总结(二)
- javascript各种类型数据在表达式中转换成布尔型值的规则总结
- 最近项目中有遇到报表数据入库的情况,数据量说大不大,说小又赖得自己手动敲!于是自己动手写了一个入库的,下面直接上代码,如有同样的,请联系我删除此文
- c c++语言各种数据类型的转换总结