獨自等待

          我等待,因為還有緣......
          posts - 1, comments - 0, trackbacks - 0, articles - 2
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          總結了一些工作上常用到的方法,貼上來與大家共享。
          /**
           * <p>Title: 通用方法類</p>
           * <p>Description: 常用的字符串及數字處理方法</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語句中的'
             * @param s
             * @return
             */
            public static String quote(String s) {
              return "'" + s.replaceAll("'", "''") + "'";
            }

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

            /*********************
             * 無異常的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;
              }
            }

            /*********************
             * 無異常的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;
              }
            }

            /*********************
             * 無異常的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;
              }
            }

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

            /*********************
             * 根據List對象生成<select>控件
             * @param l list
             * @param formFieldName form中的名稱
             * @param defaultValue 默認值
             * @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;
            }

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

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

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

             * 根據毫秒數得到中文時間
             * @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 "無";
              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+"小時 ";
                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());");
            }
          }


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 米泉市| 崇信县| 孟津县| 渝北区| 甘孜县| 永胜县| 额尔古纳市| 甘泉县| 岳阳市| 万山特区| 孙吴县| 龙岩市| 济阳县| 东至县| 阿拉善左旗| 百色市| 石首市| 崇阳县| 郓城县| 光泽县| 墨江| 江津市| 绥滨县| 永胜县| 土默特右旗| 乾安县| 安仁县| 乌拉特前旗| 和政县| 呼玛县| 临桂县| 隆林| 柳州市| 永德县| 泾源县| 太谷县| 磐石市| 额尔古纳市| 嘉善县| 江西省| 邳州市|