<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无码免费高潮喷水| 国产曰批免费视频播放免费s| 免费jjzz在线播放国产| 亚洲成人精品久久| 亚洲AV无码AV男人的天堂不卡| 两个人看的www免费视频| 免费黄色app网站| 午夜亚洲国产理论秋霞| 中文字幕免费视频一| 亚洲 自拍 另类小说综合图区| 亚洲av无码不卡| 黄色a三级三级三级免费看| 69精品免费视频| 亚洲va精品中文字幕| 可以免费观看的国产视频| 亚洲av成人一区二区三区在线观看 | 亚洲色无码专区一区| 97无码人妻福利免费公开在线视频 | 亚洲国产精品综合久久久 | 巨胸喷奶水视频www免费视频| 免费国产成人高清在线观看网站| 亚洲免费综合色在线视频| 亚洲欧洲自拍拍偷综合| 巨胸喷奶水视频www免费视频| 国产成人亚洲综合色影视| 九九久久国产精品免费热6| 最近2019中文字幕mv免费看| 亚洲一区二区三区首页| 精品一区二区三区免费观看| 亚洲成a人在线看天堂无码| 亚洲欧美日韩一区二区三区在线| 亚洲免费视频播放| 亚洲高清在线mv| 免费人妻无码不卡中文字幕系| 亚洲精品中文字幕乱码三区| 大片免费观看92在线视频线视频 | 特级毛片爽www免费版| 亚洲一区二区三区高清|