package com.zky.pub; import java.text.FieldPosition; import java.text.ParsePosition; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Calendar; import java.util.StringTokenizer; import javax.servlet.http.HttpServletRequest; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; /** 模块名称: Common.java 文件描述: 该文件为系统公共文件 */ public class Common { //pub类的构造方法 public Common() { } public static String toGb(String errorInfo) throws UnsupportedEncodingException{ return URLEncoder.encode(errorInfo, "UTF-8").toString(); } /********把一个字符串转换成一个实数********/ public static double string_to_double(String value) { if (value == null || value.length() == 0) return 0; else return (Double.valueOf(value)).doubleValue(); } /********把一个字符串转换成一个整数********/ public static int string_to_int(String value) { if (value == null || value.length() == 0) return 0; else return (Integer.valueOf(value)).intValue(); } /********把一个整数转换成一个字符串********/ public static String int_to_String(int value) { return Integer.toString(value); } /********转换null为0********/ public static String convertNullToZero(String strname) { if (strname == null || strname.equals("")) return "0"; else return strname; } //把NULL字符串转换成表格空格并在屏幕上显示出来 public static String null_show_table(String var) { //String str = var.trim(); String str = var; if (str == null || str.trim().length() == 0) str = " "; return str; } //把ISO8859_1转换成GB2312 public static String IsoConvertGb(String insert) { String outstr; try { if (insert == null || insert.trim().length() == 0) { return insert; } outstr = new String(insert.getBytes("iso8859-1"), "GBK"); } catch (Exception e) { outstr = null; } return outstr; } //把GB2312转换成ISO8859_1 public static String GbConvertIso(String insert) { String outstr; try { if (insert == null || insert.trim().length() == 0) { return insert; } outstr = new String(insert.getBytes("GBK"), "iso8859-1"); } catch (Exception e) { outstr = null; } return outstr; } //把utf-8转换成GB2312 public static String UtfConvertGb(String insert) { String outstr; try { if (insert == null || insert.trim().length() == 0) { return insert; } outstr = new String(insert.getBytes("utf-8"), "GBK"); } catch (Exception e) { outstr = null; } return outstr; } //把GB2312转换成ISO8859_1 public static String GbConvertUtf(String insert) { String outstr; try { if (insert == null || insert.trim().length() == 0) { return insert; } outstr = new String(insert.getBytes("GBK"), "utf-8"); } catch (Exception e) { outstr = null; } return outstr; } //把ISO8859_1转换成utf-8 public static String IsoConvertutf(String insert) { String outstr; try { if (insert == null || insert.trim().length() == 0) { return insert; } outstr = new String(insert.getBytes("iso8859-1"), "utf-8"); } catch (Exception e) { outstr = null; } return outstr; } //把srcStr字符串的oldStr字符替换为newStr字符 public static String replace(String srcStr, String oldStr, String newStr) { int i = 0; if ((srcStr == null) || (srcStr.trim().length() == 0)) return srcStr; i = srcStr.indexOf(oldStr); StringBuffer sb = new StringBuffer(); if (i == -1) return srcStr; sb.append(srcStr.substring(0, i) + newStr); if (i + oldStr.length() < srcStr.length()) sb.append( replace( srcStr.substring(i + oldStr.length(), srcStr.length()), oldStr, newStr)); return sb.toString(); } /** * 把yyyy-mm-dd的格式转化成14位字符串 * @param as_time * @return */ public static String datetostring(String as_time) { if (as_time == null || as_time.equals("")) return ""; String ls_temp = ""; ls_temp = as_time.substring(0, 4) + as_time.substring(5, 7) + as_time.substring(8, 10) + "000000"; return ls_temp; } /** * 把14位字符串转化成年月日时分秒 * as_flag="1"表示要转化后只有年月日,"0"表示转化后年月日时分秒都有 * @param as_time * @return */ public static String stringtotime(String as_time, String as_flag) { if (as_time == null || as_time.trim().equals("")) { return ""; } String ls_temp = ""; if (as_flag.equals("1")) { ls_temp = new StringBuffer(as_time.substring(0, 4)) .append("-") .append(as_time.substring(4, 6)) .append("-") .append(as_time.substring(6, 8)) .toString(); } else { ls_temp = new StringBuffer(as_time.substring(0, 4)) .append("-") .append(as_time.substring(4, 6)) .append("-") .append(as_time.substring(6, 8)) .append(" ") .append(as_time.substring(8, 10)) .append(":") .append(as_time.substring(10, 12)) .append(":") .append(as_time.substring(12, 14)) .toString(); } return ls_temp; } public static String getSysDate() { //获取系统时间 Connection con = null; ResultSet rs = null; PreparedStatement pst = null; String sql = ""; String date = ""; try { con = DbConn.getConn(); //查询该表的有多少列 sql = "select to_char(sysdate,'yyyymmddhh24miss') from dual"; pst = con.prepareStatement(sql); rs = pst.executeQuery(); int cnt = 0; if (rs.next()) { date = rs.getString(1); } rs.close(); pst.close(); con.close(); } catch (SQLException e) { } catch (Exception e) { } finally { try { if(rs != null) rs.close(); if(pst != null) pst.close(); if(con != null) con.close(); } catch (SQLException e1) { } } return date; } public static String getSysDate(HttpServletRequest req) { //获取系统时间 Connection con = null; ResultSet rs = null; PreparedStatement pst = null; String sql = ""; String date = ""; try { con = DbConn.getConn(); //查询该表的有多少列 sql = "select now() from dual"; pst = con.prepareStatement(sql); rs = pst.executeQuery(); int cnt = 0; if (rs.next()) { date = rs.getString(1); } rs.close(); pst.close(); con.close(); } catch (SQLException e) { } catch (Exception e) { } finally { try { if(rs != null) rs.close(); if(pst != null) pst.close(); if(con != null) con.close(); } catch (SQLException e1) { } } return date; } //获得当前帐期 --- dudj public static String getBillingCycl(HttpServletRequest req) { //获取系统时间 Connection con = null; ResultSet rs = null; PreparedStatement pst = null; String sql = ""; String date = ""; try { con = DbConn.getConn(); //查询该表的有多少列 sql = "select billingcyclid from ucis.tab_billingcycl where chrgbegin <= sysdate and chrgend >= sysdate"; pst = con.prepareStatement(sql); rs = pst.executeQuery(); int cnt = 0; if (rs.next()) { date = rs.getString(1); } rs.close(); pst.close(); con.close(); } catch (Exception e) { } finally { try { if(rs != null) rs.close(); if(pst != null) pst.close(); if(con != null) con.close(); } catch (SQLException e1) { e1.printStackTrace(); } } return date; } public static String[] Split(String str, String split) { String[] strTokens; StringTokenizer token = new StringTokenizer(str, split); strTokens = new String[token.countTokens()]; int i = 0; while (token.hasMoreTokens()) { try { strTokens[i] = token.nextToken(); } catch (Exception ex) { } i++; } return strTokens; } public final static String HanDigiStr[] = new String[] { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" }; public final static String HanDiviStr[] = new String[] { "", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟", "万", "拾", "佰", "仟" }; public static String PositiveIntegerToHanStr(String NumStr) { // 输入字符串必须正整数,只允许前导空格(必须右对齐),不宜有前导零 String RMBStr = ""; boolean lastzero = false; boolean hasvalue = false; // 亿、万进位前有数值标记 int len, n; len = NumStr.length(); if (len > 15) return "数值过大!"; for (int i = len - 1; i >= 0; i--) { if (NumStr.charAt(len - i - 1) == ' ') continue; n = NumStr.charAt(len - i - 1) - '0'; if (n < 0 || n > 9) return "输入含非数字字符!"; if (n != 0) { if (lastzero) RMBStr += HanDigiStr[0]; // 若干零后若跟非零值,只显示一个零 // 除了亿万前的零不带到后面 //if( !( n==1 && (i%4)==1 && (lastzero || i==len-1) ) ) // 如十进位前有零也不发壹音用此行 if (!(n == 1 && (i % 4) == 1 && i == len - 1)) // 十进位处于第一位不发壹音 RMBStr += HanDigiStr[n]; RMBStr += HanDiviStr[i]; // 非零值后加进位,个位为空 hasvalue = true; // 置万进位前有值标记 } else { if ((i % 8) == 0 || ((i % 8) == 4 && hasvalue)) // 亿万之间必须有非零值方显示万 RMBStr += HanDiviStr[i]; // “亿”或“万” } if (i % 8 == 0) hasvalue = false; // 万进位前有值标记逢亿复位 lastzero = (n == 0) && (i % 4 != 0); } if (RMBStr.length() == 0) return HanDigiStr[0]; // 输入空字符或"0",返回"零" return RMBStr; } public static String NumToRMBStr(double val) { String SignStr = ""; String TailStr = ""; long fraction, integer; int jiao, fen; if (val < 0) { val = -val; SignStr = "负"; } if (val > 99999999999999.999 || val < -99999999999999.999) return "数值位数过大!"; // 四舍五入到分 long temp = Math.round(val * 100); integer = temp / 100; fraction = temp % 100; jiao = (int) fraction / 10; fen = (int) fraction % 10; if (jiao == 0 && fen == 0) { TailStr = "整"; } else { TailStr = HanDigiStr[jiao]; if (jiao != 0) TailStr += "角"; if (integer == 0 && jiao == 0) // 零元后不写零几分 TailStr = ""; if (fen != 0) TailStr += HanDigiStr[fen] + "分"; } // 下一行可用于非正规金融场合,0.03只显示“叁分”而不是“零元叁分” // if( !integer ) return SignStr+TailStr; return "¥" + SignStr + PositiveIntegerToHanStr(String.valueOf(integer)) + "元" + TailStr; } public final static String getId() { ResultSet rs = null; Connection conn = null; PreparedStatement prep = null; String ls_tradeid = ""; try{ conn = DbConn.getConn(); String ls_sql = "SELECT to_char(sysdate,'yyyymmdd')||lpad(to_char(seq_id.NEXTVAL),8,'0') FROM DUAL"; prep = conn.prepareStatement(ls_sql); //执行sql语句 rs = prep.executeQuery(); while (rs.next()) { ls_tradeid = rs.getString(1); } rs.close(); prep.close(); conn.close(); } catch (Exception e) { ls_tradeid = ""; }finally { try { if(rs != null) rs.close(); if(prep != null) prep.close(); if(conn != null) conn.close(); } catch (SQLException e1) { } } return ls_tradeid; } public final static String getId(Connection conn) { ResultSet rs = null; PreparedStatement prep = null; String ls_tradeid = ""; try{ String ls_sql = "SELECT to_char(sysdate,'yyyymmdd')||lpad(to_char(seq_id.NEXTVAL),8,'0') FROM DUAL"; prep = conn.prepareStatement(ls_sql); //执行sql语句 rs = prep.executeQuery(); while (rs.next()) { ls_tradeid = rs.getString(1); } rs.close(); prep.close(); } catch (Exception e) { ls_tradeid = ""; }finally { try { if(rs != null) rs.close(); if(prep != null) prep.close(); } catch (SQLException e1) { } } return ls_tradeid; } /* public final static String getTradeID15() { ResultSet rs = null; Connection conn = null; PreparedStatement prep = null; String ls_tradeid = ""; try{ conn = DbConn.getConn(); String ls_sql = "SELECT to_char(sysdate,'yyyymmdd')||lpad(to_char(seq_tradeid.NEXTVAL),7,'0') FROM DUAL"; prep = conn.prepareStatement(ls_sql); //执行sql语句 rs = prep.executeQuery(); while (rs.next()) { ls_tradeid = rs.getString(1); } rs.close(); prep.close(); conn.close(); } catch (Exception e) { ls_tradeid = ""; }finally { try { if(rs != null) rs.close(); if(prep != null) prep.close(); if(conn != null) conn.close(); } catch (SQLException e1) { } } return ls_tradeid; } public final static String getRepairID() { ResultSet rs = null; Connection conn = null; PreparedStatement prep = null; String ls_tradeid = ""; try{ conn = DbConn.getConn(); String ls_sql = "SELECT to_char(Seq_mophonerepairid.NEXTVAL) FROM DUAL"; prep = conn.prepareStatement(ls_sql); //执行sql语句 rs = prep.executeQuery(); while (rs.next()) { ls_tradeid = rs.getString(1); } rs.close(); prep.close(); conn.close(); } catch (Exception e) { ls_tradeid = ""; }finally { try { if(rs != null) rs.close(); if(prep != null) prep.close(); if(conn != null) conn.close(); } catch (SQLException e1) { } } return ls_tradeid; } */ public final static String getMaxMophoneTradeID(String mophoneno) { ResultSet rs = null; Connection conn = null; PreparedStatement prep = null; String ls_tradeid = ""; try{ conn = DbConn.getConn(); String ls_sql = "SELECT max(mophonetradeid) from tf_r_mophonetrade where mophoneno='"; ls_sql += mophoneno; ls_sql += "'"; prep = conn.prepareStatement(ls_sql); //执行sql语句 rs = prep.executeQuery(); while (rs.next()) { ls_tradeid = rs.getString(1); } rs.close(); prep.close(); conn.close(); } catch (Exception e) { ls_tradeid = ""; } finally { try { if(rs != null) rs.close(); if(prep != null) prep.close(); if(conn != null) conn.close(); } catch (SQLException e1) { } } return ls_tradeid; } public static void updateParaTab(String tableName) { Connection conn = null; PreparedStatement prep = null; try{ conn = DbConn.getConn(); String ls_sql = "UPDATE td_s_tableinfo SET updatetime = now() WHERE upper(table_name) = ?"; prep = conn.prepareStatement(ls_sql); //执行sql语句 prep.setString(1,tableName.toUpperCase()); prep.executeUpdate(); conn.commit(); } catch (Exception e) { try{ conn.rollback(); }catch(Exception ex){ } e.printStackTrace(); } finally { try { if(prep != null) prep.close(); if(conn != null) conn.close(); } catch (SQLException e1) { } } } /** * 把 null 转换成 空字符串"" * @param string * @return */ public static String convertNull(String string) { if (string == null) { string = ""; } return string; } public static void main(String args[]) { //DateAdd("20060220",10); } /** * @param siteid_get * @return */ public static boolean isNull(String string) { if (string == null || string.equals("")) { return true; } return false; } public static String getYearMonthOptions( ){ Date cur = new Date(); StringBuffer result = new StringBuffer(); final int max = 12; String temp; for(int i=1 ; i <=max ; i ++ ){ temp = DateTime.Date2Str(DateTime.addMonths( cur,-i),"yyyyMM"); result.append("\n"); } return result.toString(); } public static String toUtf8String(String s) { StringBuffer sb = new StringBuffer(); for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); if (c >= 0 && c <= 255) { sb.append(c); } else { byte[] b; try { b = Character.toString(c).getBytes("utf-8"); } catch (Exception ex) { System.out.println(ex); b = new byte[0]; } for (int j = 0; j < b.length; j++) { int k = b[j]; if (k < 0) k += 256; sb.append("%" + Integer.toHexString(k).toUpperCase()); } } } return sb.toString(); } }