此类中封装一些常用的字符串操作(和业务无关)。

来源:互联网 发布:近年来 网络爱国事件 编辑:程序博客网 时间:2024/04/28 03:44
  1. 来自:http://xuliduo.javaeye.com/blog/196937
  2. package com.wootion.idp.util;   
  3.   
  4. import java.io.IOException;   
  5. import java.io.OutputStream;   
  6. import java.security.Security;   
  7. import java.text.SimpleDateFormat;   
  8. import java.text.StringCharacterIterator;   
  9. import java.util.Calendar;   
  10. import java.util.Date;   
  11. import java.util.HashMap;   
  12. import java.util.StringTokenizer;   
  13.   
  14. import javax.crypto.Cipher;   
  15. import javax.crypto.KeyGenerator;   
  16. import javax.crypto.SecretKey;   
  17.   
  18. /**  
  19.  * <p>文件名称: StringUtil.java</p>  
  20.  * <p>文件描述: 此类中封装一些常用的字符串操作(和业务无关)。/p>  
  21.  * <p>版权所有: 版权所有(C)2001-2006</p>  
  22.  * <p>公   司: </p>  
  23.  * <p>内容摘要: </p>  
  24.  * <p>其他说明: </p>  
  25.  * <p>完成日期:Apr 1, 2007</p>  
  26.  * <p>修改记录0:无</p>  
  27.  * @version 1.0  
  28.  * @author  许力多  
  29.  */  
  30. public class StringUtil   
  31. {   
  32.     //定义 加密算法,可用 DES,DESede,Blowfish   
  33.     public static String DES                = "DES";   
  34.   
  35.     public static String DESEDE             = "DESede";   
  36.   
  37.     public static String BLOWFISH           = "Blowfish";   
  38.   
  39.     //dateSize中的日期参数    
  40.     /*  
  41.      * 近一日  
  42.      */  
  43.     public static char   DATE_SIZE_DAY      = 'd';   
  44.   
  45.     /*  
  46.      * 近一周  
  47.      */  
  48.     public static char   DATE_SIZE_WEEK     = 'w';   
  49.   
  50.     /*  
  51.      * 近一月  
  52.      */  
  53.     public static char   DATE_SIZE_MONTH    = 'm';   
  54.   
  55.     /*  
  56.      * 近一年  
  57.      */  
  58.     public static char   DATE_SIZE_YEAR     = 'y';   
  59.   
  60.     /*  
  61.      * 明天  
  62.      */  
  63.     public static char   DATE_SIZE_TOMORROW = 'a';   
  64.   
  65.     /**  
  66.      * 私有构造方法,防止类的实例化,因为工具类不需要实例化。  
  67.      */  
  68.     private StringUtil()   
  69.     {   
  70.     }   
  71.   
  72.     public static String randomName() throws Exception   
  73.     {   
  74.         String strName = null;   
  75.         //产生10位随机传   
  76.         RandomStrg rst = new RandomStrg();   
  77.         rst.setCharset("a-zA-Z0-9");   
  78.         rst.setLength("17");   
  79.         rst.generateRandomObject();   
  80.         strName = rst.getRandom() + System.currentTimeMillis();   
  81.   
  82.         return strName;   
  83.     }   
  84.   
  85.     /**  
  86.      * 取得时间  
  87.      * @param str 日期格式,可以为空(非null)  
  88.      * @param dates 时间/如果为空 那么取得当前时间  
  89.      * @return  
  90.      */  
  91.     public static String now(String str, java.sql.Date dates)   
  92.     {   
  93.         if ("".equals(str))   
  94.             str = "yyyy-MM-dd";   
  95.         java.util.Date date = dates;   
  96.         return now(str, date);   
  97.     }   
  98.   
  99.     public static String randomNum()   
  100.     {   
  101.         String strName = null;   
  102.         // 产生10位随机传   
  103.         RandomStrg rst = new RandomStrg();   
  104.         rst.setCharset("0-9");   
  105.         rst.setLength("6");   
  106.         try  
  107.         {   
  108.             rst.generateRandomObject();   
  109.         }   
  110.         catch (Exception e)   
  111.         {   
  112.             e.printStackTrace();   
  113.         }   
  114.         strName = StringUtil.now("yyyyMMdd"null) + rst.getRandom();   
  115.   
  116.         return strName;   
  117.     }   
  118.   
  119.     /**  
  120.      * 取得时间  
  121.      * @param str 日期格式,可以为空(非null)  
  122.      * @param dates 时间/如果为空 那么取得当前时间  
  123.      * @return  
  124.      */  
  125.     public static String now(String str, java.util.Date dates)   
  126.     {   
  127.         if ("".equals(str))   
  128.             str = "yyyy-MM-dd";   
  129.         java.text.SimpleDateFormat date = new java.text.SimpleDateFormat(str);   
  130.         String currentDate = date.format(dates != null ? dates : thisTime());   
  131.         return currentDate;   
  132.     }   
  133.   
  134.     /**  
  135.      * 取得当前时间  
  136.      * @return uitl.Data对象  
  137.      */  
  138.     public static java.util.Date thisTime()   
  139.     {   
  140.         return new Date(System.currentTimeMillis());   
  141.     }   
  142.   
  143.     /**  
  144.      * 此方法将给出的字符串source使用delim划分为单词数组。  
  145.      * @param source 需要进行划分的原字符串  
  146.      * @param delim 单词的分隔字符串  
  147.      * @return 划分以后的数组,如果source为null的时候返回以source为唯一元素的数组,  
  148.      *         如果delim为null则使用逗号作为分隔字符串。  
  149.      * @since  0.1  
  150.      */  
  151.     public static String[] split(String source, String delim)   
  152.     {   
  153.         String[] wordLists;   
  154.         if (source == null)   
  155.         {   
  156.             wordLists = new String[1];   
  157.             wordLists[0] = source;   
  158.             return wordLists;   
  159.         }   
  160.         if (delim == null)   
  161.         {   
  162.             delim = ",";   
  163.         }   
  164.         StringTokenizer st = new StringTokenizer(source, delim);   
  165.         int total = st.countTokens();   
  166.         wordLists = new String[total];   
  167.         for (int i = 0; i < total; i++)   
  168.         {   
  169.             wordLists[i] = st.nextToken();   
  170.         }   
  171.         return wordLists;   
  172.     }   
  173.   
  174.     /**  
  175.      * 此方法将给出的字符串source使用delim划分为单词数组。  
  176.      * @param source 需要进行划分的原字符串  
  177.      * @param delim 单词的分隔字符  
  178.      * @return 划分以后的数组,如果source为null的时候返回以source为唯一元素的数组。  
  179.      * @since  0.2  
  180.      */  
  181.     public static String[] split(String source, char delim)   
  182.     {   
  183.         return split(source, String.valueOf(delim));   
  184.     }   
  185.   
  186.     /**  
  187.      * 此方法将给出的字符串source使用逗号划分为单词数组。  
  188.      * @param source 需要进行划分的原字符串  
  189.      * @return 划分以后的数组,如果source为null的时候返回以source为唯一元素的数组。  
  190.      * @since  0.1  
  191.      */  
  192.     public static String[] split(String source)   
  193.     {   
  194.         return split(source, ",");   
  195.     }   
  196.   
  197.     /**  
  198.      * 循环打印字符串数组。  
  199.      * 字符串数组的各元素间以指定字符分隔,如果字符串中已经包含指定字符则在字符串的两端加上双引号。  
  200.      * @param strings 字符串数组  
  201.      * @param delim 分隔符  
  202.      * @param out 打印到的输出流  
  203.      * @since  0.4  
  204.      */  
  205.     public static void printStrings(String[] strings, String delim,   
  206.             OutputStream out)   
  207.     {   
  208.         try  
  209.         {   
  210.             if (strings != null)   
  211.             {   
  212.                 int length = strings.length - 1;   
  213.                 for (int i = 0; i < length; i++)   
  214.                 {   
  215.                     if (strings[i] != null)   
  216.                     {   
  217.                         if (strings[i].indexOf(delim) > -1)   
  218.                         {   
  219.                             out.write(("/"" + strings[i] + "/"" + delim)   
  220.                                     .getBytes());   
  221.                         }   
  222.                         else  
  223.                         {   
  224.                             out.write((strings[i] + delim).getBytes());   
  225.                         }   
  226.                     }   
  227.                     else  
  228.                     {   
  229.                         out.write("null".getBytes());   
  230.                     }   
  231.                 }   
  232.                 if (strings[length] != null)   
  233.                 {   
  234.                     if (strings[length].indexOf(delim) > -1)   
  235.                     {   
  236.                         out.write(("/"" + strings[length] + "/"").getBytes());   
  237.                     }   
  238.                     else  
  239.                     {   
  240.                         out.write(strings[length].getBytes());   
  241.                     }   
  242.                 }   
  243.                 else  
  244.                 {   
  245.                     out.write("null".getBytes());   
  246.                 }   
  247.             }   
  248.             else  
  249.             {   
  250.                 out.write("null".getBytes());   
  251.             }   
  252.             out.write(java.lang.Character.LINE_SEPARATOR);   
  253.         }   
  254.         catch (IOException e)   
  255.         {   
  256.   
  257.         }   
  258.     }   
  259.   
  260.     /**  
  261.      * 循环打印字符串数组到标准输出。  
  262.      * 字符串数组的各元素间以指定字符分隔,如果字符串中已经包含指定字符则在字符串的两端加上双引号。  
  263.      * @param strings 字符串数组  
  264.      * @param delim 分隔符  
  265.      * @since  0.4  
  266.      */  
  267.     public static void printStrings(String[] strings, String delim)   
  268.     {   
  269.         printStrings(strings, delim, System.out);   
  270.     }   
  271.   
  272.     /**  
  273.      * 循环打印字符串数组。  
  274.      * 字符串数组的各元素间以逗号分隔,如果字符串中已经包含逗号则在字符串的两端加上双引号。  
  275.      * @param strings 字符串数组  
  276.      * @param out 打印到的输出流  
  277.      * @since  0.2  
  278.      */  
  279.     public static void printStrings(String[] strings, OutputStream out)   
  280.     {   
  281.         printStrings(strings, ",", out);   
  282.     }   
  283.   
  284.     /**  
  285.      * 循环打印字符串数组到系统标准输出流System.out。  
  286.      * 字符串数组的各元素间以逗号分隔,如果字符串中已经包含逗号则在字符串的两端加上双引号。  
  287.      * @param strings 字符串数组  
  288.      * @since  0.2  
  289.      */  
  290.     public static void printStrings(String[] strings)   
  291.     {   
  292.         printStrings(strings, ",", System.out);   
  293.     }   
  294.   
  295.     /**  
  296.      * 将字符串中的变量使用values数组中的内容进行替换。  
  297.      * 替换的过程是不进行嵌套的,即如果替换的内容中包含变量表达式时不会替换。  
  298.      * @param prefix 变量前缀字符串  
  299.      * @param source 带参数的原字符串  
  300.      * @param values 替换用的字符串数组  
  301.      * @return 替换后的字符串。  
  302.      *         如果前缀为null则使用“%”作为前缀;  
  303.      *         如果source或者values为null或者values的长度为0则返回source;  
  304.      *         如果values的长度大于参数的个数,多余的值将被忽略;  
  305.      *         如果values的长度小于参数的个数,则后面的所有参数都使用最后一个值进行替换。  
  306.      * @since  0.2  
  307.      */  
  308.     public static String getReplaceString(String prefix, String source,   
  309.             String[] values)   
  310.     {   
  311.         String result = source;   
  312.         if (source == null || values == null || values.length < 1)   
  313.         {   
  314.             return source;   
  315.         }   
  316.         if (prefix == null)   
  317.         {   
  318.             prefix = "%";   
  319.         }   
  320.   
  321.         for (int i = 0; i < values.length; i++)   
  322.         {   
  323.             String argument = prefix + Integer.toString(i + 1);   
  324.             int index = result.indexOf(argument);   
  325.             if (index != -1)   
  326.             {   
  327.                 String temp = result.substring(0, index);   
  328.                 if (i < values.length)   
  329.                 {   
  330.                     temp += values[i];   
  331.                 }   
  332.                 else  
  333.                 {   
  334.                     temp += values[values.length - 1];   
  335.                 }   
  336.                 temp += result.substring(index + 2);   
  337.                 result = temp;   
  338.             }   
  339.         }   
  340.         return result;   
  341.     }   
  342.   
  343.     /**  
  344.      * 将字符串中的变量(以“%”为前导后接数字)使用values数组中的内容进行替换。  
  345.      * 替换的过程是不进行嵌套的,即如果替换的内容中包含变量表达式时不会替换。  
  346.      * @param source 带参数的原字符串  
  347.      * @param values 替换用的字符串数组  
  348.      * @return 替换后的字符串  
  349.      * @since  0.1  
  350.      */  
  351.     public static String getReplaceString(String source, String[] values)   
  352.     {   
  353.         return getReplaceString("%", source, values);   
  354.     }   
  355.   
  356.     /**  
  357.      * 字符串数组中是否包含指定的字符串。  
  358.      * @param strings 字符串数组  
  359.      * @param string 字符串  
  360.      * @param caseSensitive 是否大小写敏感  
  361.      * @return 包含时返回true,否则返回false  
  362.      * @since  0.4  
  363.      */  
  364.     public static boolean contains(String[] strings, String string,   
  365.             boolean caseSensitive)   
  366.     {   
  367.         for (int i = 0; i < strings.length; i++)   
  368.         {   
  369.             if (caseSensitive == true)   
  370.             {   
  371.                 if (strings[i].equals(string))   
  372.                 {   
  373.                     return true;   
  374.                 }   
  375.             }   
  376.             else  
  377.             {   
  378.                 if (strings[i].equalsIgnoreCase(string))   
  379.                 {   
  380.                     return true;   
  381.                 }   
  382.             }   
  383.         }   
  384.         return false;   
  385.     }   
  386.   
  387.     /**  
  388.      * 字符串数组中是否包含指定的字符串。大小写敏感。  
  389.      * @param strings 字符串数组  
  390.      * @param string 字符串  
  391.      * @return 包含时返回true,否则返回false  
  392.      * @since  0.4  
  393.      */  
  394.     public static boolean contains(String[] strings, String string)   
  395.     {   
  396.         return contains(strings, string, true);   
  397.     }   
  398.   
  399.     /**  
  400.      * 不区分大小写判定字符串数组中是否包含指定的字符串。  
  401.      * @param strings 字符串数组  
  402.      * @param string 字符串  
  403.      * @return 包含时返回true,否则返回false  
  404.      * @since  0.4  
  405.      */  
  406.     public static boolean containsIgnoreCase(String[] strings, String string)   
  407.     {   
  408.         return contains(strings, string, false);   
  409.     }   
  410.   
  411.     /**  
  412.      * 将字符串数组使用指定的分隔符合并成一个字符串。  
  413.      * @param array 字符串数组  
  414.      * @param delim 分隔符,为null的时候使用""作为分隔符(即没有分隔符)  
  415.      * @return 合并后的字符串  
  416.      * @since  0.4  
  417.      */  
  418.     public static String combineStringArray(String[] array, String delim)   
  419.     {   
  420.         int length = array.length - 1;   
  421.         if (delim == null)   
  422.         {   
  423.             delim = "";   
  424.         }   
  425.         StringBuffer result = new StringBuffer(length * 8);   
  426.         for (int i = 0; i < length; i++)   
  427.         {   
  428.             result.append(array[i]);   
  429.             result.append(delim);   
  430.         }   
  431.         result.append(array[length]);   
  432.         return result.toString();   
  433.     }   
  434.   
  435.     /**  
  436.      * 以指定的字符和长度生成一个该字符的指定长度的字符串。  
  437.      * @param c 指定的字符  
  438.      * @param length 指定的长度  
  439.      * @return 最终生成的字符串  
  440.      * @since  0.6  
  441.      */  
  442.     public static String fillString(char c, int length)   
  443.     {   
  444.         String ret = "";   
  445.         for (int i = 0; i < length; i++)   
  446.         {   
  447.             ret += c;   
  448.         }   
  449.         return ret;   
  450.     }   
  451.   
  452.     /**  
  453.      * 去除左边多余的空格。  
  454.      * @param value 待去左边空格的字符串  
  455.      * @return 去掉左边空格后的字符串  
  456.      * @since  0.6  
  457.      */  
  458.     public static String trimLeft(String value)   
  459.     {   
  460.         String result = value;   
  461.         if (result == null)   
  462.             return result;   
  463.         char ch[] = result.toCharArray();   
  464.         int index = -1;   
  465.         for (int i = 0; i < ch.length; i++)   
  466.         {   
  467.             if (Character.isWhitespace(ch[i]))   
  468.             {   
  469.                 index = i;   
  470.             }   
  471.             else  
  472.             {   
  473.                 break;   
  474.             }   
  475.         }   
  476.         if (index != -1)   
  477.         {   
  478.             result = result.substring(index + 1);   
  479.         }   
  480.         return result;   
  481.     }   
  482.   
  483.     /**  
  484.      * 去除右边多余的空格。  
  485.      * @param value 待去右边空格的字符串  
  486.      * @return 去掉右边空格后的字符串  
  487.      * @since  0.6  
  488.      */  
  489.     public static String trimRight(String value)   
  490.     {   
  491.         String result = value;   
  492.         if (result == null)   
  493.             return result;   
  494.         char ch[] = result.toCharArray();   
  495.         int endIndex = -1;   
  496.         for (int i = ch.length - 1; i > -1; i--)   
  497.         {   
  498.             if (Character.isWhitespace(ch[i]))   
  499.             {   
  500.                 endIndex = i;   
  501.             }   
  502.             else  
  503.             {   
  504.                 break;   
  505.             }   
  506.         }   
  507.         if (endIndex != -1)   
  508.         {   
  509.             result = result.substring(0, endIndex);   
  510.         }   
  511.         return result;   
  512.     }   
  513.   
  514.     /**  
  515.      * 根据转义列表对字符串进行转义。  
  516.      * @param source 待转义的字符串  
  517.      * @param escapeCharMap 转义列表  
  518.      * @return 转义后的字符串  
  519.      * @since  0.6  
  520.      */  
  521.     public static String escapeCharacter(String source, HashMap escapeCharMap)   
  522.     {   
  523.         if (source == null || source.length() == 0)   
  524.             return source;   
  525.         if (escapeCharMap.size() == 0)   
  526.             return source;   
  527.         StringBuffer sb = new StringBuffer();   
  528.         StringCharacterIterator sci = new StringCharacterIterator(source);   
  529.         for (char c = sci.first(); c != StringCharacterIterator.DONE; c = sci   
  530.                 .next())   
  531.         {   
  532.             String character = String.valueOf(c);   
  533.             if (escapeCharMap.containsKey(character))   
  534.                 character = (String) escapeCharMap.get(character);   
  535.             sb.append(character);   
  536.         }   
  537.         return sb.toString();   
  538.     }   
  539.   
  540.     /**  
  541.      * 得到字符串的字节长度。  
  542.      * @param source 字符串  
  543.      * @return 字符串的字节长度  
  544.      * @since  0.6  
  545.      */  
  546.     public static int getByteLength(String source)   
  547.     {   
  548.         int len = 0;   
  549.         for (int i = 0; i < source.length(); i++)   
  550.         {   
  551.             char c = source.charAt(i);   
  552.             int highByte = c >>> 8;   
  553.             len += highByte == 0 ? 1 : 2;   
  554.         }   
  555.         return len;   
  556.     }   
  557.   
  558.     /**  
  559.      * 得到字符串中的子串的个数。  
  560.      * @param source 字符串  
  561.      * @param sub 子串  
  562.      * @return 字符串中的子串的个数  
  563.      * @since  0.6  
  564.      */  
  565.     public static int getSubtringCount(String source, String sub)   
  566.     {   
  567.         if (source == null || source.length() == 0)   
  568.         {   
  569.             return 0;   
  570.         }   
  571.         int count = 0;   
  572.         int index = source.indexOf(sub);   
  573.         while (index >= 0)   
  574.         {   
  575.             count++;   
  576.             index = source.indexOf(sub, index + 1);   
  577.         }   
  578.         return count;   
  579.     }   
  580.   
  581.     /**  
  582.      * 判断是否为null 如果是 返回"",不然返回原串  
  583.      * @param source  
  584.      * @return  
  585.      */  
  586.     public static String checkNull(String source)   
  587.     {   
  588.         return (source = (source == null ? "" : source));   
  589.     }   
  590.   
  591.     /**  
  592.      * 仿javascript的escape方法  
  593.      * @param src  
  594.      * @return  
  595.      */  
  596.     public static String escape(String src)   
  597.     {   
  598.         int i;   
  599.         char j;   
  600.         StringBuffer tmp = new StringBuffer();   
  601.         tmp.ensureCapacity(src.length() * 6);   
  602.         for (i = 0; i < src.length(); i++)   
  603.         {   
  604.             j = src.charAt(i);   
  605.             if (Character.isDigit(j) || Character.isLowerCase(j)   
  606.                     || Character.isUpperCase(j))   
  607.             {   
  608.                 tmp.append(j);   
  609.             }   
  610.             else if (j < 256)   
  611.             {   
  612.                 tmp.append("%");   
  613.                 if (j < 16)   
  614.                 {   
  615.                     tmp.append("0");   
  616.                 }   
  617.                 tmp.append(Integer.toString(j, 16));   
  618.             }   
  619.             else  
  620.             {   
  621.                 tmp.append("%u");   
  622.                 tmp.append(Integer.toString(j, 16));   
  623.             }   
  624.         }   
  625.         return tmp.toString();   
  626.     }   
  627.   
  628.     /**  
  629.      * 仿javascript的unescape方法  
  630.      * @param src  
  631.      * @return  
  632.      */  
  633.     public static String unescape(String src)   
  634.     {   
  635.         StringBuffer tmp = new StringBuffer();   
  636.         tmp.ensureCapacity(src.length());   
  637.         int lastPos = 0 , pos = 0;   
  638.         char ch;   
  639.         while (lastPos < src.length())   
  640.         {   
  641.             pos = src.indexOf("%", lastPos);   
  642.             if (pos == lastPos)   
  643.             {   
  644.                 if (src.charAt(pos + 1) == 'u')   
  645.                 {   
  646.                     ch = (char) Integer.parseInt(src   
  647.                             .substring(pos + 2, pos + 6), 16);   
  648.                     tmp.append(ch);   
  649.                     lastPos = pos + 6;   
  650.                 }   
  651.                 else  
  652.                 {   
  653.                     ch = (char) Integer.parseInt(src   
  654.                             .substring(pos + 1, pos + 3), 16);   
  655.                     tmp.append(ch);   
  656.                     lastPos = pos + 3;   
  657.                 }   
  658.             }   
  659.             else  
  660.             {   
  661.                 if (pos == -1)   
  662.                 {   
  663.                     tmp.append(src.substring(lastPos));   
  664.                     lastPos = src.length();   
  665.                 }   
  666.                 else  
  667.                 {   
  668.                     tmp.append(src.substring(lastPos, pos));   
  669.                     lastPos = pos;   
  670.                 }   
  671.             }   
  672.         }   
  673.         return tmp.toString();   
  674.     }   
  675.   
  676.     /**  
  677.      * 在html里面显示加密过的中文  
  678.      * @param str  
  679.      * @return  
  680.      */  
  681.     public static String getHtmlEscape(String str)   
  682.     {   
  683.         return "<script>  document.write(unescape('" + escape(str)   
  684.                 + "')); </script>";   
  685.     }   
  686.   
  687.     /**  
  688.      * 获得2个关键字之间的内容  
  689.      * @param s  
  690.      * @param str1  
  691.      * @param str2  
  692.      * @return  
  693.      */  
  694.     public static String getBettown(String s, String str1, String str2)   
  695.     {   
  696.         return s.substring(s.indexOf(str1) + 1, s.indexOf(str2));   
  697.     }   
  698.   
  699.     /**flq  
  700.      * 字符串转换成时间  
  701.      * @param str 时间字符串  
  702.      * @return  
  703.      */  
  704.     public static Date strToDate(String str)   
  705.     {   
  706.   
  707.         Date date = null;   
  708.         try  
  709.         {   
  710.             SimpleDateFormat formatters = new SimpleDateFormat("yyyy-MM-dd");   
  711.             date = formatters.parse(str);   
  712.   
  713.         }   
  714.         catch (Exception e)   
  715.         {   
  716.             e.printStackTrace();   
  717.         }   
  718.         return date;   
  719.     }   
  720.   
  721.     /**  
  722.      * 自定义格式串转换date  
  723.      * since 西藏移动支撑系统  
  724.      * @param str  
  725.      * @param dateformat  
  726.      * @return  
  727.      */  
  728.     public static Date strToDate(String str, String dateformat)   
  729.     {   
  730.   
  731.         Date date = null;   
  732.         try  
  733.         {   
  734.             SimpleDateFormat formatters = new SimpleDateFormat(dateformat);   
  735.             date = formatters.parse(str);   
  736.   
  737.         }   
  738.         catch (Exception e)   
  739.         {   
  740.             e.printStackTrace();   
  741.         }   
  742.         return date;   
  743.   
  744.     }   
  745.   
  746.     /**  
  747.      * flq 转换成时间字符串  
  748.      *   
  749.      * @param time  
  750.      *            时间  
  751.      * @return  
  752.      */  
  753.     public static String dateToStr(Date time, String dateformat)   
  754.     {   
  755.   
  756.         String strDate = "";   
  757.         try  
  758.         {   
  759.             SimpleDateFormat formatters = new SimpleDateFormat(dateformat);   
  760.             strDate = formatters.format(time);   
  761.   
  762.         }   
  763.         catch (Exception e)   
  764.         {   
  765.             e.printStackTrace();   
  766.         }   
  767.         return strDate;   
  768.     }   
  769.   
  770.     /**flq  
  771.      * 取得日期范围  
  772.      * @param t d:近一日 w:近一周 m:近一月 y:近一年 a:下一天  
  773.      * @return Date  
  774.      */  
  775.     public static Date dateSize(char t)   
  776.     {   
  777.   
  778.         Date date = thisTime();   
  779.         Calendar calendar = Calendar.getInstance();   
  780.         calendar.setTime(date);   
  781.         switch (t)   
  782.         {   
  783.         case 'd' :   
  784.             //calendar.roll(calendar.DATE,false);   
  785.             break;   
  786.         case 'w' :   
  787.             calendar.roll(calendar.WEEK_OF_YEAR, false);   
  788.             break;   
  789.         case 'm' :   
  790.             calendar.roll(calendar.MONTH, false);   
  791.             break;   
  792.         case 'y' :   
  793.             calendar.roll(calendar.YEAR, false);   
  794.             break;   
  795.         case 'a' :   
  796.             calendar.roll(calendar.DATE, true);   
  797.             break;   
  798.         default :   
  799.             System.out.println("dateSize 函数参数错误");   
  800.         }   
  801.         date = calendar.getTime();   
  802.         return date;   
  803.     }   
  804.   
  805.     /**  
  806.      * 根据数组返回 str1,str2,str3,...strn的串  
  807.      * @param str  
  808.      * @return  
  809.      * @throws NullPointerException  
  810.      */  
  811.     public static String getStringFromArray(String str[])   
  812.             throws NullPointerException   
  813.     {   
  814.         if (str == null)   
  815.             throw new NullPointerException("请输入需要转换的数组");   
  816.         String strs = "";   
  817.         for (String s : str)   
  818.         {   
  819.             strs += s + ",";   
  820.         }   
  821.         if (strs.length() > 0)   
  822.             strs = strs.substring(0, strs.length() - 1);   
  823.         return strs;   
  824.     }   
  825.   
  826.     /**  
  827.      * 根据入口的String,返回截取后的String,其中,2个数字算一个中文  
  828.      * @param source  
  829.      * @return 截取后的新串 10个中文,后面是...  
  830.      */  
  831.     public static String interceptString(String source)   
  832.     {   
  833.         return interceptString(source, "..."10);   
  834.     }   
  835.   
  836.     /**  
  837.      * 根据入口的String,返回截取后的String,其中,2个数字算一个中文  
  838.      * @param source 需要变更的String  
  839.      * @param str 在String后面加的符号,默认为“...”  
  840.      * @param number 剩余的中文个数  
  841.      * @return 截取后的新串  
  842.      */  
  843.     public static String interceptString(String source, String str, int number)   
  844.     {   
  845.         source = checkNull(source);   
  846.         str = str == null ? "..." : str;   
  847.   
  848.         int len = strlen(source);//获得含有汉字的长度   
  849.         if (len <= number * 2)//2个汉字为一个字符   
  850.             return source;   
  851.         int k = 0;   
  852.         for (int i = source.length() - 1; i >= 0; i--)   
  853.         {   
  854.             char c = source.charAt(i);   
  855.             if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z')   
  856.                     || (c >= 'A' && c <= 'Z'))//数字   
  857.             {   
  858.                 k = +2;   
  859.             }   
  860.             else  
  861.             {   
  862.                 if (Character.isLetter(c))   
  863.                 { // 中文   
  864.                     continue;   
  865.                 }   
  866.                 else  
  867.                 { // 符号或控制字符   
  868.                     //k++;   
  869.                     continue;   
  870.                 }   
  871.             }   
  872.             if (k == 3)   
  873.             {   
  874.                 number++;   
  875.                 k = 0;   
  876.             }   
  877.         }   
  878.         if (k == 1)   
  879.             number++;   
  880.         return source.substring(0, number - 1) + str;   
  881.     }   
  882.   
  883.     /**  
  884.      * 计算字符串长度.   一个汉字的长度按2计算.   如果给定的字符串为null,   返回0.   
  885.      * @param str  
  886.      * @return  
  887.      */  
  888.     public static int strlen(String str)   
  889.     {   
  890.         if (str == null || str.length() <= 0)   
  891.         {   
  892.             return 0;   
  893.         }   
  894.         int len = 0;   
  895.         char c;   
  896.         for (int i = str.length() - 1; i >= 0; i--)   
  897.         {   
  898.             c = str.charAt(i);   
  899.             if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z')   
  900.                     || (c >= 'A' && c <= 'Z'))   
  901.             {   
  902.                 //字母,   数字      
  903.                 len++;   
  904.             }   
  905.             else  
  906.             {   
  907.                 if (Character.isLetter(c))   
  908.                 { //中文      
  909.                     len += 2;   
  910.                 }   
  911.                 else  
  912.                 { //符号或控制字符      
  913.                     len++;   
  914.                 }   
  915.             }   
  916.         }   
  917.         return len;   
  918.     }   
  919.   
  920.     /**  
  921.      * 加密用,初始化JCE  
  922.      */  
  923.     static  
  924.     {   
  925.         Security.addProvider(new com.sun.crypto.provider.SunJCE());   
  926.     }   
  927.   
  928.     /**  
  929.      * 字节码转换成16进制字符串  
  930.      * @param b 需要转换的字节流  
  931.      * @return 以“:”分割的字符串  
  932.      */  
  933.     public static String byte2hex(byte[] b)   
  934.     {   
  935.         String hs = "";   
  936.         String stmp = "";   
  937.         for (int n = 0; n < b.length; n++)   
  938.         {   
  939.             stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));   
  940.             if (stmp.length() == 1)   
  941.                 hs = hs + "0" + stmp;   
  942.             else  
  943.                 hs = hs + stmp;   
  944.             if (n < b.length - 1)   
  945.                 hs = hs + ":";   
  946.         }   
  947.         return hs.toUpperCase();   
  948.     }   
  949.   
  950.     /**  
  951.      * 16进制字符串转换字节码  
  952.      * @param str 需要转换的字符串,以“:”分割  
  953.      * @return 转换后的字节流  
  954.      */  
  955.     public static byte[] hex2byte(String str)   
  956.     {   
  957.         String stmp[] = str.split(":");   
  958.         byte[] btmp = new byte[stmp.length];   
  959.         for (int n = 0; n < stmp.length; n++)   
  960.         {   
  961.             int i = Integer.valueOf(stmp[n], 16);   
  962.             btmp[n] = (byte) (i & 0XFF);   
  963.         }   
  964.         return btmp;   
  965.     }   
  966.   
  967.     /**  
  968.      * 生成密生成密钥,此方法比较慢  
  969.      * @param algorithm 使用那个算法  
  970.      * @return byte[] 密钥  
  971.      * @throws Exception  
  972.      */  
  973.     public static byte[] getKey(String algorithm) throws Exception   
  974.     {   
  975.         KeyGenerator keygen = KeyGenerator.getInstance(algorithm);   
  976.         SecretKey deskey = keygen.generateKey();   
  977.   
  978.         return deskey.getEncoded();   
  979.     }   
  980.   
  981.     /**  
  982.      * 加密算法  
  983.      * @param input byte[]需要加密的byte流  
  984.      * @param key byte[]密钥  
  985.      * @param algorithm 使用那个算法  
  986.      * @return 加密后的byte流  
  987.      * @throws Exception  
  988.      */  
  989.     public static byte[] encode(byte[] input, byte[] key, String algorithm)   
  990.             throws Exception   
  991.     {   
  992.         SecretKey deskey = new javax.crypto.spec.SecretKeySpec(key, algorithm);   
  993.   
  994.         Cipher c1 = Cipher.getInstance(algorithm);   
  995.         c1.init(Cipher.ENCRYPT_MODE, deskey);   
  996.         byte[] cipherByte = c1.doFinal(input);//加密   
  997.   
  998.         return cipherByte;   
  999.     }   
  1000.   
  1001.     /**  
  1002.      * 封装好的加密算法  
  1003.      * @param input String 需要加密的串  
  1004.      * @param key String 密钥  
  1005.      * @param algorithm 使用那个算法  
  1006.      * @return 加密后的String  
  1007.      * @throws Exception  
  1008.      */  
  1009.     public static String encode(String input, String key, String algorithm)   
  1010.             throws Exception   
  1011.     {   
  1012.         return byte2hex(encode(input.getBytes(), key.getBytes(), algorithm));   
  1013.     }   
  1014.   
  1015.     /**  
  1016.      * 解密算法   
  1017.      * @param input byte[]需要解密的byte流  
  1018.      * @param key byte[] 密钥  
  1019.      * @param algorithm 使用那个算法  
  1020.      * @return 解密后的byte流  
  1021.      * @throws Exception  
  1022.      */  
  1023.     public static byte[] decode(byte[] input, byte[] key, String algorithm)   
  1024.             throws Exception   
  1025.     {   
  1026.         SecretKey deskey = new javax.crypto.spec.SecretKeySpec(key, algorithm);   
  1027.   
  1028.         Cipher c1 = Cipher.getInstance(algorithm);   
  1029.         c1.init(Cipher.DECRYPT_MODE, deskey);   
  1030.         byte[] clearByte = c1.doFinal(input);   
  1031.   
  1032.         return clearByte;   
  1033.     }   
  1034.   
  1035.     /**  
  1036.      * 封装好的解密算法  
  1037.      * @param input String需要解密的字符串   
  1038.      * @param key String 密钥  
  1039.      * @param algorithm 使用那个算法  
  1040.      * @return 解密后的字符串   
  1041.      * @throws Exception  
  1042.      */  
  1043.     public static String decode(String input, String key, String algorithm)   
  1044.             throws Exception   
  1045.     {   
  1046.         return new String(decode(hex2byte(input), key.getBytes(), algorithm));   
  1047.     }   
  1048.   
  1049.     /**  
  1050.      * MD5摘要,不可逆   
  1051.      * @param input byte[] 需要加密的byte流  
  1052.      * @return 加密后的byte流  
  1053.      * @throws Exception  
  1054.      */  
  1055.     public static byte[] md5(byte[] input) throws Exception   
  1056.     {   
  1057.         java.security.MessageDigest alg = java.security.MessageDigest   
  1058.                 .getInstance("MD5"); //or "SHA-1"   
  1059.   
  1060.         alg.update(input);   
  1061.         byte[] digest = alg.digest();   
  1062.   
  1063.         return digest;   
  1064.     }   
  1065.   
  1066.     /**  
  1067.      * 数字转换成人民币   
  1068.      * @param double 需要转换的人民币金额  
  1069.      * @return String  
  1070.      * @throws Exception  
  1071.      */  
  1072.     public static String changeToBig(double value)   
  1073.     {   
  1074.         char[] hunit = {'拾''佰''仟'}; // 段内位置表示   
  1075.         char[] vunit = {'万''亿'}; // 段名表示   
  1076.         char[] digit = {'零''壹''贰''叁''肆''伍''陆''柒''捌''玖'}; // 数字表示   
  1077.         long midVal = (long) (value * 100); // 转化成整形   
  1078.         String valStr = String.valueOf(midVal); // 转化成字符串   
  1079.         String head = valStr.substring(0, valStr.length() - 2); // 取整数部分   
  1080.         String rail = valStr.substring(valStr.length() - 2); // 取小数部分   
  1081.   
  1082.         String prefix = ""// 整数部分转化的结果   
  1083.         String suffix = ""// 小数部分转化的结果   
  1084.         // 处理小数点后面的数   
  1085.         if (rail.equals("00"))   
  1086.         { // 如果小数部分为0   
  1087.             suffix = "整";   
  1088.         }   
  1089.         else  
  1090.         {   
  1091.             suffix = digit[rail.charAt(0) - '0'] + "角"  
  1092.                     + digit[rail.charAt(1) - '0'] + "分"// 否则把角分转化出来   
  1093.         }   
  1094.         // 处理小数点前面的数   
  1095.         char[] chDig = head.toCharArray(); // 把整数部分转化成字符数组   
  1096.         char zero = '0'// 标志'0'表示出现过0   
  1097.         byte zeroSerNum = 0// 连续出现0的次数   
  1098.         for (int i = 0; i < chDig.length; i++)   
  1099.         { // 循环处理每个数字   
  1100.             int idx = (chDig.length - i - 1) % 4// 取段内位置   
  1101.             int vidx = (chDig.length - i - 1) / 4// 取段位置   
  1102.             if (chDig[i] == '0')   
  1103.             { // 如果当前字符是0   
  1104.                 zeroSerNum++; // 连续0次数递增   
  1105.                 if (zero == '0')   
  1106.                 { // 标志   
  1107.                     zero = digit[0];   
  1108.                 }   
  1109.                 else if (idx == 0 && vidx > 0 && zeroSerNum < 4)   
  1110.                 {   
  1111.                     prefix += vunit[vidx - 1];   
  1112.                     zero = '0';   
  1113.                 }   
  1114.                 continue;   
  1115.             }   
  1116.             zeroSerNum = 0// 连续0次数清零   
  1117.             if (zero != '0')   
  1118.             { // 如果标志不为0,则加上,例如万,亿什么的   
  1119.                 prefix += zero;   
  1120.                 zero = '0';   
  1121.             }   
  1122.             prefix += digit[chDig[i] - '0']; // 转化该数字表示   
  1123.             if (idx > 0)   
  1124.                 prefix += hunit[idx - 1];   
  1125.             if (idx == 0 && vidx > 0)   
  1126.             {   
  1127.                 prefix += vunit[vidx - 1]; // 段结束位置应该加上段名如万,亿   
  1128.             }   
  1129.         }   
  1130.   
  1131.         if (prefix.length() > 0)   
  1132.             prefix += '圆'// 如果整数部分存在,则有圆的字样   
  1133.         //System.out.println(value+"====>"+prefix+suffix);   
  1134.         return prefix + suffix; // 返回正确表示   
  1135.     }   
  1136.   
  1137.     /**  
  1138.      * 将文件名中的汉字转为UTF8编码的串,以便下载时能正确显示另存的文件名.  
  1139.      * @param s 原文件名  
  1140.      * @return 重新编码后的文件名  
  1141.      */  
  1142.     public static String toUtf8String(String s)   
  1143.     {   
  1144.         StringBuffer sb = new StringBuffer();   
  1145.         for (int i = 0; i < s.length(); i++)   
  1146.         {   
  1147.             char c = s.charAt(i);   
  1148.             if (c >= 0 && c <= 255)   
  1149.             {   
  1150.                 sb.append(c);   
  1151.             }   
  1152.             else  
  1153.             {   
  1154.                 byte[] b;   
  1155.                 try  
  1156.                 {   
  1157.                     b = Character.toString(c).getBytes("utf-8");   
  1158.                 }   
  1159.                 catch (Exception ex)   
  1160.                 {   
  1161.                     System.out.println(ex);   
  1162.                     b = new byte[0];   
  1163.                 }   
  1164.                 for (int j = 0; j < b.length; j++)   
  1165.                 {   
  1166.                     int k = b[j];   
  1167.                     if (k < 0)   
  1168.                         k += 256;   
  1169.                     sb.append("%" + Integer.toHexString(k).toUpperCase());   
  1170.                 }   
  1171.             }   
  1172.         }   
  1173.         return sb.toString();   
  1174.     }   
  1175.   
  1176.     /**  
  1177.      * 返回num个空格  
  1178.      * @param num  
  1179.      * @return  
  1180.      */  
  1181.     public static String getSpaces(int num)   
  1182.     {   
  1183.         StringBuffer sbr = new StringBuffer();   
  1184.         for (int i = 0; i < num; i++)   
  1185.         {   
  1186.             sbr.append(" ");   
  1187.         }   
  1188.         return sbr.toString();   
  1189.     }   
  1190.   
  1191. }  
原创粉丝点击