<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    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 我心依舊 閱讀(702) 評論(1)  編輯  收藏

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

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


    網站導航:
     
    主站蜘蛛池模板: 国产免费av片在线无码免费看| 免费在线看污视频| 亚洲国产老鸭窝一区二区三区| 国产亚洲美女精品久久| 日本一区二区三区在线视频观看免费 | a免费毛片在线播放| 一本色道久久88亚洲综合| 亚洲精品夜夜夜妓女网| 亚洲夂夂婷婷色拍WW47| 成年女人色毛片免费看| 亚洲AV成人无码网天堂| 免费人妻av无码专区| 一级一片免费视频播放| 奇米影视亚洲春色| 久久免费美女视频| 亚洲性一级理论片在线观看| 一级特黄特色的免费大片视频| 亚洲国产精品自在拍在线播放| a毛片成人免费全部播放| 久久亚洲一区二区| 亚洲一区二区三区免费视频| 亚洲Aⅴ无码专区在线观看q| 84pao强力永久免费高清| 亚洲最大的黄色网| 免费v片在线观看无遮挡| 黄床大片免费30分钟国产精品| 成人爽A毛片免费看| 怡红院亚洲红怡院在线观看| 亚洲免费观看视频| 一日本道a高清免费播放 | 国产成人免费网站在线观看| 欧亚一级毛片免费看| 亚洲av永久无码精品网站| 91免费在线播放| 国产精品亚洲综合网站| 亚洲春色在线视频| 国产成人免费网站| www免费黄色网| 天堂亚洲国产中文在线| 久久久久亚洲精品天堂久久久久久| 最近2019免费中文字幕6|