posts - 70,comments - 408,trackbacks - 0

          package net;

           

          import java.sql.Connection;

          import java.sql.Date;

          import java.sql.ResultSet;

          import java.sql.SQLException;

          import java.sql.Statement;

          import java.util.ArrayList;

          import java.util.Calendar;

          import java.util.Iterator;

          import java.util.Locale;

           

          import javax.naming.InitialContext;

          import javax.naming.NamingException;

          import javax.sql.DataSource;

          /**

           * @author 張宏亮

           * @Date 2006-3-1

           * version 1.0

           * explain 通用方法的封裝類

           * Email it5719@163.com

           */

          public class ActionUtil {

                 /**

               * 截取中文字符串的方法

               * @param str 要截取的字符串,bytes截取之后每個串的字節數

               * @return 包含截串的結果的ArrayList

               * @throws Exception 截取漢字異常

               */

                 public static ArrayList splitString(String str,int bytes) throws Exception{

                        ArrayList array = new ArrayList();

                        try {

                               String splitStr = str;

                               int splitByte = bytes;

                               int loopCount = 0;

                               loopCount = (splitStr.length()%splitByte==0)?(splitStr.length()/splitByte):(splitStr.length()/splitByte+1);

                               //System.out.println("字符串的長度是"+splitStr.length()+",要截取成"+loopCount+",因為每段需要截取"+splitByte+"字節!");

                               for(int i=1;i<=loopCount;i++) {

                                      if(i==loopCount)

                                             array.add(splitStr.substring((i-1)*splitByte,splitStr.length()));

                                      else

                                             array.add(splitStr.substring((i-1)*splitByte,(i*splitByte)));

                               }

                               Iterator it = array.iterator();

                               while(it.hasNext()){

                                      System.out.println(it.next());

                               }

                        } catch (RuntimeException e) {

                               e.printStackTrace();

                               throw new Exception("截取漢字發生異常");

                        }

                        return array;

                 }

                 /**

               * 測試池連接的方法

               * @param name 連接池的名字在TOMCAT中配置,sql 要發送的SQL語句

               * @return void

               * @throws Exception 數據庫連接異常,NamingException JNDI初始化異常

               */

                 public static void poolConnection(String name,String sql) {

                         Connection con = null;

                         Statement stmt = null;

                         try {

                                  InitialContext ctx = new InitialContext();

                                  DataSource ds = (DataSource)ctx.lookup(name);

                                  con = ds.getConnection();

                                  stmt = con.createStatement();

                                  ResultSet rs = stmt.executeQuery(sql);

                                  while(rs.next()) {

                                       System.out.print(rs.getString(1));

                                       System.out.print(rs.getString(2));

                                       System.out.print(rs.getString(3));

                                  }

                         } catch (NamingException e) {

                               System.out.println("JNDI初始化異常");

                               e.printStackTrace();

                         } catch (SQLException e) {

                               System.out.println("數據庫連接異常");

                               e.printStackTrace();

                         }

                         finally {

                               try {

                                      stmt.close();

                                      con.close();

                               } catch (SQLException e) {

                                      System.out.println("關閉連接池異常");

                                      e.printStackTrace();

                               }

                         }

                 }

              /**

               * 輸入身份證號碼返回年齡

               * @param id 身份證號碼

               * @return 年齡

               * @throws Exception 身份證號碼不正確(長度不夠)

               */

              public static int getAge(String id) throws Exception {

                  int age = -1;

                  int length = id.length();

                  String birthday = "";

                  if (length == 15) {

                      birthday = id.substring(6, 8);

                      birthday = "19" + birthday;

                  } else if (length == 18)

                      birthday = id.substring(6, 10);

                  else

                      throw new Exception("錯誤的身份證號");

                  int currentYear = Calendar.getInstance().get(1);

                  age = currentYear - (new Integer(birthday)).intValue();

                  System.out.println("您的身份證號碼是"+id+",您的年齡是"+age);

                  return age;

              }

              /**

               * 獲得當前日期字符串

               * @return 日期字符串

               */

              public static String getCurrentDate() {

                  Calendar cal = Calendar.getInstance();

                  String currentDate = null;

                  java.util.Date d = cal.getTime();

                  String currentYear = (new Integer(cal.get(1))).toString();

                  String currentMonth = null;

                  String currentDay = null;

                  if (cal.get(2) < 9)

                      currentMonth = "0" + (new Integer(cal.get(2) + 1)).toString();

                  else

                      currentMonth = (new Integer(cal.get(2) + 1)).toString();

                  if (cal.get(5) < 10)

                      currentDay = "0" + (new Integer(cal.get(5))).toString();

                  else

                      currentDay = (new Integer(cal.get(5))).toString();

                  currentDate = currentYear + currentMonth + currentDay;

                  System.out.println(currentDate);

                  return currentDate;

              }

             

                 public static void main(String[] args) {

                        try {

                               getCurrentDate();

                               getAge("220103198601211017");

                               //因為池連方法需要數據源,所以在著就不測試了

                               splitString("2005年國民經濟和社會發展統計公報》",3);

                        } catch (Exception e) {

                               e.printStackTrace();

                               System.out.println("測試發生異常");

                        }

                 }

          }

           

          posted on 2006-03-02 11:14 我心依舊 閱讀(706) 評論(1)  編輯  收藏

          FeedBack:
          # re: 工作空閑時間,隨便寫了幾個方法,寫的很爛希望不要給我丟雞蛋.
          2006-11-06 16:55 | breezedancer
          你的文章被轉至我的論壇,若有不便,請通知論壇管理員http://http://www.51forum.uni.cc  回復  更多評論
            

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


          網站導航:
           
          主站蜘蛛池模板: 昂仁县| 通榆县| 潮安县| 桦甸市| 酉阳| 永登县| 周宁县| 陇西县| 海安县| 雷山县| 青河县| 孟津县| 贺州市| 大英县| 绥江县| 平塘县| 宁明县| 孟津县| 宁都县| 平顺县| 波密县| 韶山市| 潜山县| 佳木斯市| 秦皇岛市| 叙永县| 安福县| 朝阳市| 富顺县| 松溪县| 南涧| 蕲春县| 丹凤县| 陈巴尔虎旗| 依兰县| 瑞安市| 英山县| 马山县| 会东县| 商南县| 泾川县|