commonfuns

Posted on 2005-11-22 13:38  飞鼠  阅读(284)  评论(0)    收藏  举报

 

package com.bshine.Util ;

import java.io.*;
import java.util.*;
import java.text.SimpleDateFormat;

public class ClsCommFuns {

    public ClsCommFuns() {
    }


//清除變量值為空白
    public static String doNull(String str){
        if(str==null){
          return str = "";
      }
        return str;
    }

//得到系統當前日期
    public  static String getDate()
    {
        Date date=new Date();
        Date newdat=null;
        SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy/MM/dd");

        bartDateFormat.format(date);
        return bartDateFormat.format(date);
   }

   //轉碼
   public static String urlEncodeChange(String s,String cEncode)
   {
       /*
       String ss;
       try { //
           ss= java.net.URLEncoder.encode(s, cEncode);
           return ss;
       } catch(Exception ex) {}

       try { //將ISO8859_1碼的字串s轉成Unicode碼並傳回
          ss= new String(Tnul(s).getBytes("iso-8859-1"), cEncode);
           return ss;
       } catch(Exception ex) {}

      try { //將ISO8859_1碼的字串s轉成Unicode碼並傳回
           ss= new String(Tnul(s).getBytes("iso-8859-1"), cEncode);
            return ss;
       } catch(Exception ex) {}

      try { //將ISO8859_1碼的字串s轉成Unicode碼並傳回
           ss= new String(Tnul(s).getBytes("UTF-8"), cEncode);
            return ss;
       } catch(Exception ex) {}

       try { //將Big5碼的字串s轉成Unicode碼並傳回
           ss= new String(Tnul(s).getBytes("Big5"), cEncode);
            return ss;
       } catch(Exception ex) {}

*/
       return s;

   }


// 將字串s以十六進位顯示
    public static String toHexString(String s) {
        String str = "";
        for (int i = 0; i < s.length(); i++) {
            int ch = (int) s.charAt(i);
            String s4 = "0000" + Integer.toHexString(ch);
            str = str + s4.substring(s4.length() - 4) + " ";
        }
        return str;
    }

// 轉換大寫
    public static String Upper(String s) {
        String str = s.toUpperCase();
        return str;
    }

// 轉換小寫
    public static String Lower(String s) {
        String str = s.toLowerCase();
        return str;
    }

// 今天日期 - 西元年
    public static int GetMonth() {
        Calendar calendar = null;
        calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        return 1 + calendar.get(Calendar.MONTH);
    }

//
    public static int GetYearCha() {
        Calendar calendar = null;
        calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        return calendar.get(Calendar.YEAR) - 1911;
    }

//
    public static String GetDate_Eng() {
        Calendar calendar = null;
        calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        String yyyy = "0000", mm = "00", dd = "00", mm_1 = "00";
        yyyy = yyyy + calendar.get(Calendar.YEAR);
        mm = mm + (int) GetMonth();
        dd = dd + calendar.get(Calendar.DAY_OF_MONTH);
        yyyy = yyyy.substring(yyyy.length() - 4);
        mm = mm.substring(mm.length() - 2, mm.length());
        dd = dd.substring(dd.length() - 2);
        return yyyy + "/" + mm + "/" + dd;
    }

// 取得日期 - 民國年
    public static String GetDate_Cha() {
        Calendar calendar = null;
        calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        String yyyy = "0000", yy = "00", mm = "00", dd = "00", mm_1 = "00";
        yy = yy + (int) GetYearCha();
        mm = mm + (int) GetMonth();
        dd = dd + calendar.get(Calendar.DAY_OF_MONTH);
        yy = yy.substring(yyyy.length() - 2);
        mm = mm.substring(mm.length() - 2, mm.length());
        dd = dd.substring(dd.length() - 2);
        return yy + "/" + mm + "/" + dd;
    }

//
    public static String GetDate_Cha_1() {
        Calendar calendar = null;
        calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        String yyyy = "0000", yy = "00", mm = "00", dd = "00", mm_1 = "00";
        yy = yy + (int) GetYearCha();
        mm = mm + new Integer(1 + calendar.get(Calendar.MONTH));
        dd = dd + calendar.get(Calendar.DAY_OF_MONTH);
        yy = yy.substring(yyyy.length() - 2);
        mm = mm.substring(mm.length() - 2, mm.length());
        dd = dd.substring(dd.length() - 2);
        return yy + "/" + mm + "/" + dd;
    }

//
    public static String getToDay() {
        Calendar calendar = null;
        calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        String yyyy = "0000", yy = "00", mm = "00", dd = "00", mm_1 = "00";
        yy = yy + (int) GetYearCha();
        mm = mm + (int) GetMonth();
        dd = dd + calendar.get(Calendar.DAY_OF_MONTH);
        yy = yy.substring(yyyy.length() - 2);
        mm = mm.substring(mm.length() - 2, mm.length());
        dd = dd.substring(dd.length() - 2);
        return yy + mm + dd;


    }

 

// Set Null Data To Spaec(1)
    public static String Tnul(String In_Str, int In_Len) {
        String s = In_Str;
        if (s == null) {
            s = "";
            for (int i = 0; i < In_Len; i++) {
                s = s + " ";
            }
        }
        ;
        return s;
    }

// Set Null Data To Empty
    public static String Tnul(String In_Str) {
        String s = In_Str;
        if (In_Str == null)return "";
        else return In_Str;
    }

// Set Null or Empty To &nbsp;
    public static String Tnul0(String In_Str) {
        if (In_Str == null || In_Str.trim().equals(""))return "&nbsp";
        else return In_Str;
    }

 


    public static String replace(String line, String ch, String rep) {
        int i = line.indexOf(ch);
        StringBuffer sb = new StringBuffer();
        if (i == -1)
            return line;
        sb.append(line.substring(0, i) + rep);
        if (i + ch.length() < line.length())
            sb.append(replace(line.substring(i + ch.length(), line.length()),
                              ch, rep));
        return sb.toString();
    }

     public static String getSysDate(String strFormat) {
         Date d = new Date();
         SimpleDateFormat f = new SimpleDateFormat(strFormat);//yyyy-MM-dd  yyyy/MM/dd  HH:mm:ss
         String s = f.format(d);
         System.out.println(s);
         return s;
     }


     /* **************************************************************
 ※ 以下程序段功能: 使用指定字串(p)將字符串(s)從左(left)邊填補到指定長度(Len)
     參數:        1. String s               :字符串(s)
                  2. String p               :指定字串(p)
                  3. int Len                :補到指定長度(Len)
  ************************************************************** */
     public static String PadL(String s, String p,int Len)
     {
         int len1=0;
         for(int i=0;i<s.length();i++)
         {
             if (s.charAt(i) >= 128)
                 len1 += 2;
             else
                 len1++;
         }
         int k=Len-len1;

         for(int i=0;i<k;i++)
         {
         s=p+s;
         }

     return s;
     }


 /* **************************************************************
※ 以下程序段功能: 使用指定字串(p)將字符串(s)從左(right)邊填補到指定長度(Len)
   參數:        1. String s               :字符串(s)
                2. String p               :指定字串(p)
                3. int Len                :補到指定長度(Len)
************************************************************** */
   public static String PadR(String s, String p,int Len)
   {
       int len1=0;
       for(int i=0;i<s.length();i++)
       {
           if (s.charAt(i) >= 128)
               len1 += 2;
           else
               len1++;
       }
       int k=Len-len1;

       for(int i=0;i<k;i++)
       {
       s=s+p;
       }

   return s;
   }


   /* **************************************************************
※ 以下程序段功能: 使用指定字串(p)將字符串(s)從左(center)邊填補到指定長度(Len)
   參數:        1. String s               :字符串(s)
                2. String p               :指定字串(p)
                3. int Index_begin        :開始填補位置
                4. int Len                :填補長度(Len)
************************************************************** */
   public static String PadC(String s, String p,int Index_begin,int Len)
   {
       /*
          if(s.length()<Index_begin)
              return s;

           if (s.charAt(Index_begin) >= 128)
               Index_begin += 1;

           if(s.length()<Index_begin)
              return s;

           String s1=s.substring(0,Index_begin);
           String s2=s.substring(Index_begin);
           String s3="";

           for (int i = 0; i < Len; i++) {
               s3 = s3 + p;
           }
  */
            return "";
   }

 

 

 

 


}