獨(dú)自等待

          我等待,因?yàn)檫€有緣......
          posts - 1, comments - 0, trackbacks - 0, articles - 2
            BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          總結(jié)了一些工作上常用到的方法,貼上來(lái)與大家共享。
          /**
           * <p>Title: 通用方法類</p>
           * <p>Description: 常用的字符串及數(shù)字處理方法</p>
           * <p>Copyright: Copyright (c) 2004</p>
           * <p>Company: joyinter.com</p>
           * @author not attributable
           * @version 1.0
           */

          import java.text.*;
          import java.util.*;
          import java.io.*;
          import javax.servlet.http.*;

          public class CommonUtil {
          private static DecimalFormat df = new DecimalFormat("#############0.00");

            public static DecimalFormat getDecimalFormat()
              {
                  return df;
              }

            /*******************
             * 替換SQL語(yǔ)句中的'
             * @param s
             * @return
             */
            public static String quote(String s) {
              return "'" + s.replaceAll("'", "''") + "'";
            }

            /*********************
             * 格式化日期
             * @param pattern 日期格式
             * @param date 日期對(duì)象
             * @return
             */
            public static String dateFormat(String pattern, java.util.Date date) {
              SimpleDateFormat simpledateformat = new SimpleDateFormat(pattern);
              return simpledateformat.format(date);
            }

            /*********************
             * 無(wú)異常的parseInt方法
             * @param s
             * @return
             */
            public static int parseInt(String s) {
              if (s == null) {
                return 0;
              }
              try {
                return Integer.parseInt(s);
              }
              catch (NumberFormatException nfe) {
                return 0;
              }
            }

            /*********************
             * 無(wú)異常的parseLong方法
             * @param s
             * @return
             */
            public static long parseLong(String s) {
              if (s == null) {
                return 0;
              }
              try {
                return Long.parseLong(s);
              }
              catch (NumberFormatException nfe) {
                return 0;
              }
            }

            /*********************
             * 無(wú)異常的parseFloat方法
             * @param s
             * @return
             */
            public static float parseFloat(String s) {
              if (s == null) {
                return 0;
              }
              try {
                return Float.parseFloat(s);
              }
              catch (NumberFormatException nfe) {
                return 0;
              }
            }

            /*********************
             * 回車符轉(zhuǎn)換為<br>
             * @param text
             * @return
             */
            public static String nb2br(String text) {
              return text.replaceAll("\n", "<br>");
            }

            /*********************
             * 根據(jù)List對(duì)象生成<select>控件
             * @param l list
             * @param formFieldName form中的名稱
             * @param defaultValue 默認(rèn)值
             * @return
             */
            public static String genSelect(List l, String formFieldName,
                                           Object defaultValue) {
              StringBuffer buf = new StringBuffer();
              buf.append("<select name=\"" + formFieldName + "\">");
              if (formFieldName.equals("parentid")) {
                buf.append("<option>&lt;根&gt;</option>");
              }
              for (int i = 0; i < l.size(); i++) {
                boolean selected = false;
                HashMap row = (HashMap) l.get(i);
                Object key = row.keySet().iterator().next();
                Object value = row.get(key);
                if (key.equals(defaultValue)) {
                  selected = true;
                }
                buf.append("<option value=\"" + key + "\"");
                if (selected) {
                  buf.append(" selected");
                }
                buf.append(">" + value + "</option>");
              }
              buf.append("</select>");
              return buf.toString();
            }

            /*********************
             * 將首字母改成大寫
             * @param str
             * @return
             */
            public static String uppercaseFirstChar(String str) {
              str = str.substring(0, 1).toUpperCase() + str.substring(1);
              return str;
            }

            /*********************
             * 替換html中的特殊字符
             * @param text
             * @return
             */
            public static String htmlSpecialChars(String text) {
              if (text != null) {
                text = text.replaceAll("&", "&amp;");
                text = text.replaceAll("\"", "&quot;");
                text = text.replaceAll("<", "&lt;");
                text = text.replaceAll(">", "&gt;");
              }
              return text;
            }

            /*********************

             * 替換字符串中的\
             * @param text
             * @return
             */
            public static String addSlash(String text) {
              if (text != null) {
                text = text.replaceAll("\\\"", "\\\\\"")
                    .replaceAll("\\\r\\\n", "\\\\r\\\\n");
              }
              return text;
            }

            /*********************

             * 將回車轉(zhuǎn)換為<br>
             * @param text
             * @return
             */
            public static String nl2br(String text) {
              return text.replaceAll("\r\n", "<br>");
            }

            /*********************

             * 根據(jù)毫秒數(shù)得到中文時(shí)間
             * @param millisecond
             * @return
             */
            public static String second2CNdate(double millisecond){
              double second=Math.round(millisecond/1000);
              int year,month,day,hour,minute;
              String CNdate="";
              if (second<0) return "無(wú)";
              boolean flag=false;
              year=(int)Math.floor(second/31104000);
              if (year>0){
                CNdate=year+"年 ";
                flag=true;
                second-=year*31104000;
              }
              month=(int)Math.floor(second/2592000);
              if (month>0||flag){
                CNdate+=month+"月 ";
                flag=true;
                second-=month*2592000;
              }
              day=(int)Math.floor(second/86400);
              if (day>0||flag){
                CNdate+=day+"天 ";
                flag=true;
                second-=day*86400;
              }
              hour=(int)Math.floor(second/3600);
              if (hour>0||flag){
                CNdate+=hour+"小時(shí) ";
                flag=true;
                second-=hour*3600;
              }
              minute=(int)Math.floor(second/60);
              if (minute>0||flag){
                CNdate+=minute+"分 ";
                flag=true;
                second-=minute*60;
              }

              CNdate+=(int)second+"秒";
              return CNdate;
            }

            public static String getRelativePath(String absPath, HttpServletRequest request){
              String host = "http://"+request.getServerName();
              if(request.getServerPort() != 80){
                host += ":" + request.getServerPort();
              }
              return absPath.replaceAll(host, "");

            }

            public static String getDefinedLengthString(String str, int length, String addtion){
              if(str.length()<length){
                return str;
              }else{
                return str.substring(0, length)+addtion;
              }
            }

            public static float percentFormat(int i, int sum){
              if(sum==0){
                return 100;
              }else{
                return (float)((i*10000/sum)/100);
              }
            }

            public static String join(String[] arr, String seed){
              if(arr == null) return null;
              StringBuffer buf = new StringBuffer();
              for(int i=0; i<arr.length; i++){
                buf.append(arr[i]);
                if(i!=arr.length-1){
                  buf.append(seed);
                }
              }
              return buf.toString();
            }
            /**
             * check if the operating system is a windows system, otherwise, we suggest it as linux system
             * @return boolean
             */
            public static boolean isWindowsOS(){
              String osName = System.getProperty("os.name");
              if(osName.indexOf(" ")>=0){
                osName = osName.substring(0, osName.indexOf(" "));
              }
              if(osName.equalsIgnoreCase("WINDOWS")){
                return true;
              }else{
                return false;
              }
            }

            /**
             * 生成hash code
             * @param s String
             * @return int
             */
            public static long hCode(String s) {
              long hash = 0;
              for (int i = 0; i < s.length(); i++){
                hash = (31 * hash) + s.charAt(i);
                if((""+hash).length()>=5){
                  hash = parseLong((""+hash).substring((""+hash).length()-4));
                }
              }
              return hash;
            }

            public static String getJs(String s){
              StringBuffer buffer = new StringBuffer();
              buffer.append("<script>");
              buffer.append(s);
              buffer.append("</script>");
              return buffer.toString();
            }

            public static String  getJsLog(int type, String msgName, String msgContent){
              return getJs("window.parent.info.push("+type+", \""+msgName+"\", \""+msgContent+"\", new Date());");
            }
          }


          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 延边| 闽侯县| 筠连县| 高尔夫| 石阡县| 武义县| 蒙山县| 永昌县| 阿拉善右旗| 永登县| 交口县| 繁峙县| 历史| 太仆寺旗| 光泽县| 塘沽区| 西宁市| 广水市| 宁国市| 香河县| 麦盖提县| 綦江县| 鲁山县| 鄂伦春自治旗| 苏尼特右旗| 苍梧县| 顺平县| 绿春县| 屯留县| 贡觉县| 长子县| 霞浦县| 马关县| 彩票| 灌南县| 宜春市| 襄樊市| 岳池县| 阳江市| 吉木萨尔县| 凤阳县|