隨筆-37  評論-58  文章-4  trackbacks-0
          常用的java工具類
             1public class StringUtil {  
             
          2.       
             
          3.     /** 
             4.      * 判斷字符串值是否為空 
             5.      * 
          @param value 
             6.      * 
          @return 
             7.      
          */
            
             
          8.     public static boolean isEmpty(String value){  
             
          9.         if(value == null || "".equals(value)){  
            
          10.             return true;  
            
          11.         }
            
            
          12.         return false;  
            
          13.     }
            
            
          14.       
            
          15.     public static boolean isDate(String value,String format){  
            
          16.           
            
          17.         SimpleDateFormat sdf = null;  
            
          18.         ParsePosition pos = new ParsePosition(0);//指定從所傳字符串的首位開始解析  
            19.           
            
          20.         if(value == null || isEmpty(format)){  
            
          21.             return false;  
            
          22.         }
            
            
          23.         try {  
            
          24.             sdf = new SimpleDateFormat(format);  
            
          25.             sdf.setLenient(false);  
            
          26.             Date date = sdf.parse(value,pos);  
            
          27.             if(date == null){  
            
          28.                 return false;  
            
          29.             }
          else{  
            
          30.                 System.out.println("-------->pos : " + pos.getIndex());  
            
          31.                 System.out.println("-------->date : " + sdf.format(date));  
            
          32.                 //更為嚴謹的日期,如2011-03-024認為是不合法的  
            33.                 if(pos.getIndex() > sdf.format(date).length()){  
            
          34.                     return false;  
            
          35.                 }
            
            
          36.                 return true;  
            
          37.             }
            
            
          38.         }
           catch (Exception e) {  
            
          39.             e.printStackTrace();  
            
          40.             return false;  
            
          41.         }
            
            
          42.     }
            
            
          43.       
            
          44.     public static void main(String[] args) {  
            
          45.         System.out.println(isDate("21011-02-18","yyyy-MM-dd"));  
            
          46.     }
            
            
          47. }
            


          posted on 2011-11-15 15:46 楓中玎玲 閱讀(2032) 評論(0)  編輯  收藏 所屬分類: Java常用

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


          網站導航:
           
          主站蜘蛛池模板: 江西省| 噶尔县| 平远县| 珲春市| 无棣县| 临猗县| 雅江县| 康保县| 长治市| 五莲县| 资源县| 仪陇县| 柞水县| 邻水| 洪雅县| 九江县| 海城市| 绥中县| 迁西县| 合江县| 平江县| 南京市| 汉阴县| 南川市| 建平县| 深水埗区| 长沙县| 东丽区| 吴堡县| 右玉县| 镇平县| 肃北| 柘荣县| 成都市| 临城县| 海城市| 舞阳县| 垫江县| 赤水市| 平江县| 卢氏县|