??? 在用java作項(xiàng)目程序開(kāi)發(fā)過(guò)程中,,遇到財(cái)務(wù)制度要求在作社?;鹬Ц稌r(shí),,套打《專用付款款憑證》,,金額項(xiàng)除阿拉伯?dāng)?shù)字外,,還要求將人民幣(大寫)打印出來(lái),;另有許多參保人員,,居民身份證的位數(shù)由15位更換成18位,,若此類人員進(jìn)行新參保登記時(shí),,不進(jìn)行校驗(yàn)檢查,,將會(huì)造成同一參保人員,以不同的居民身份證號(hào)在業(yè)務(wù)系統(tǒng)中進(jìn)行重復(fù)登記,。故需提供將15位身份證轉(zhuǎn)換成18位,,并檢驗(yàn)此值與核心數(shù)據(jù)庫(kù)是否有相同值,有則提示此人員已參保,。另提供對(duì)18位身份證的校驗(yàn)功能(注:按已公布的國(guó)標(biāo)算法),。
模塊描述:
??? (1)人民幣在大寫功能轉(zhuǎn)換,見(jiàn)doubleToChinese模塊,,實(shí)現(xiàn)的方法為將數(shù)值及位數(shù)分別取出后,,按對(duì)應(yīng)的值將其轉(zhuǎn)換成字串即可。如100,,則大寫金額為“壹百整“,;對(duì)負(fù)金額則加負(fù)字,例-100.01,,大寫金額為“負(fù)壹百零壹分整”,。
??? (2)身份證由15位轉(zhuǎn)18位,可見(jiàn)fixPersonIDCode模塊,。附:提交判斷是否為有效的18位身份證號(hào)isIdentityId模塊,,方法為判斷年份為19開(kāi)頭的18位身份證號(hào),將其轉(zhuǎn)化為15位后,,按國(guó)標(biāo)進(jìn)行修補(bǔ),,若經(jīng)轉(zhuǎn)換的值同輸入的18位值相同,則說(shuō)明身份證編號(hào)是有效的,,否則身份證號(hào)可能有誤,;考慮到2000年以后出生的人的身份證編號(hào),可能到2016年以后才能申請(qǐng)到,,到時(shí)此系統(tǒng)能否還在使用很難說(shuō),,故暫未提供對(duì)其身份證號(hào)碼的判斷功能。但在此也提供解決的辦法,,可以先判斷18位身份證中的年份,,對(duì)以20開(kāi)頭的年份,進(jìn)行轉(zhuǎn)換時(shí)修補(bǔ)程序中改為加20既可,。
??? 目前以上功能模塊在社保核心業(yè)務(wù)系統(tǒng)應(yīng)用正常,。
??? 本例用jbuilder7編譯通過(guò)。說(shuō)明:以下代碼中所加注的說(shuō)明,,說(shuō)明功能模塊的作用。在具體應(yīng)用中借鑒或拷貝此代碼到實(shí)際程序中即可使用,。
package csi.utilitytools;
import java.text.DateFormat;
import java.text.NumberFormat;
import java.util.*;
import java.io.ByteArrayInputStream;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
public class COMUT {
public COMUT() {
? }
? /**
?? * 修補(bǔ)15位居民身份證號(hào)碼為18位
?? * @param personIDCode
?? * @return
?? */
? public static String fixPersonIDCode(String personIDCode)
? {
??? String retIDCode = "";
??? if ( personIDCode == null || personIDCode.trim().length() != 15 ) {
????? return personIDCode;
??? }
??? String id17 = personIDCode.substring(0,6) + "19" + personIDCode.substring(6,15);? //15為身份證補(bǔ)'19'
??? char[] code = {'1','0','X','9','8','7','6','5','4','3','2'};? //11個(gè)
??? int[] factor={0, 2,4,8, 5,10,9,7, 3,6,1,2, 4,8,5,10, 9,7}; //18個(gè);
??? int[] idcd = new int[18];
??? int???? i;
??? int???? j;
??? int???? sum;
??? int???? remainder;
??? for (i=1; i<18; i++)
??? {
????? j = 17 - i ;
????? idcd[i] = Integer.parseInt(id17.substring(j, j+1));
??? }
??? sum = 0;
??? for (i=1; i<18; i++)
??? {
????? sum = sum? + idcd[i] * factor[i];
??? }
??? remainder = sum%11;
??? String lastCheckBit = String.valueOf(code[ remainder ]);
??? return id17 + lastCheckBit;
??? }
??? /**
???? *人民幣大寫金額轉(zhuǎn)換
???? * @param number
???? * @param pattern
???? * @return
???? * @throws GeneralException
???? */
??? public static String format(double number,String pattern)
??? {
????? DecimalFormat df = new DecimalFormat(pattern);
????? return df.format(number);
??? }
??? public static String format(String number,String pattern)
??? {
????? double d = new Double(number).doubleValue();
????? return format(d,pattern);
??? }
??? public static String doubleToChinese(double sumofcash)
??? {
????? String[] arr = {"分","角","元","拾","百","千","萬(wàn)","拾萬(wàn)","百萬(wàn)",
??????????????????????????????? "千萬(wàn)","億","拾億","百億","千億"};
????? String sTmp = "";
????? String sMoney = "";
????? String header = "";
????? if(sumofcash<0)
??????? header = "負(fù)";
????? sumofcash = Math.abs(sumofcash);
????? double dTmp =sumofcash*100;
????? sMoney = format(dTmp,"0,000.#");
????? StringTokenizer st = new StringTokenizer(sMoney,",");
????? String tmp = "";
????? while (st.hasMoreElements()){
??????? tmp = tmp + st.nextToken();
????? }
????? sMoney = tmp;
????? int iLen = sMoney.length();
????? int count = 0;
????? for (int i=0;i
??????? String sTemp = sMoney.substring(i,i+1);
??????? if (sTemp.equals("0"))
??????? {
????????? count++;
????????? if (count==1)
????????? {
??????????? sTmp = sTmp + trans(sTemp);
????????? }
??????? }else
??????? {
????????? sTmp = sTmp + trans(sTemp);
????????? sTmp = sTmp + arr[iLen-i-1];
????????? count = 0;
??????? }
????? }
????? sTmp = sTmp.trim();
????? iLen = sTmp.length();
????? if (sTmp.substring(iLen-1,iLen).equals("零"))
????? {
??????? sTmp = sTmp.substring(0,iLen-1);
????? }
????? sTmp = sTmp + "整";
????? sTmp = header.concat(sTmp);
????? return sTmp;
??? }
??? private static String trans(String args)
??? {
????? int iTemp = Integer.parseInt(args);
????? String sRes = "";
????? switch (iTemp)
????? {
??????? case 1:sRes = "壹";
????????? break;
??????? case 2:sRes = "貳";
????????? break;
??????? case 3:sRes = "叁";
????????? break;
??????? case 4:sRes = "肆";
????????? break;
??????? case 5:sRes = "伍";
????????? break;
??????? case 6:sRes = "陸";
????????? break;
??????? case 7:sRes = "柒";
????????? break;
??????? case 8:sRes = "捌";
????????? break;
??????? case 9:sRes = "玖";
????????? break;
??????? case 0:sRes = "零";
????? }
????? return sRes;
??? }
/**
?* 判斷是否是有效的18位或15位居民身份證號(hào)碼
?* @param identityId:18位或15位居民身份證號(hào)碼
?* @return:true: 有效的18位或15位居民身份證號(hào)碼
?*/
??? public static boolean isIdentityId(String identityId) {
????? if(isEmpty(identityId)) return false;
????? try{
??????? if(identityId.length()==18){
????????? String identityId15 = identityId.substring(0,6)+identityId.substring(8,17);
????????? //System.out.println("the identityId15 is : "+identityId15);
????????? if(fixPersonIDCode(identityId15).equalsIgnoreCase(identityId)) {
??????????? return true;
????????? }else {
????????? return false;
????????? }
??????? }else if(identityId.length()==15){
????????? try{
??????????? Long.parseLong(identityId);
??????????? return true;
????????? }catch(Exception ex){
??????????? return false;
????????? }
??????? }else{
????????? return false;
??????? }
????? }catch(Exception ex){
??????????? return false;
????? }
? }
? /**
?? * 判斷是否為空串""
?? */
? public static boolean isEmpty(String sValue) {
??? if(sValue==null) return true;
??? return sValue.trim().equals("") ? true : false ;
?}
? public static void main(String[] args) {
??? try{
????? System.out.println(doubleToChinese(12003000.01)); //打印金額大寫
System.out.println(fixPersonIDCode("650103760113073"));//身份證15位轉(zhuǎn)18位
System.out.println(isIdentityId ("650103760113073")); //否是為有效的15位身份證號(hào)
??? } catch (Exception e) {
????? e.printStackTrace();
??? }
? }
}
程序調(diào)試結(jié)果如下:
D:\JBuilder7\jdk1.3.1\bin\javaw -classpath "D:\ejbwork\xjsb\classes;D:\bea\wlserver6.0\lib\weblogic.jar;C:\bea\wlserver6.1\lib\weblogic_sp.jar;C:\bea\wlserver6.1\lib\weblogic.jar;D:\JBuilder7\jdk1.3.1\demo\jfc\Java2D\Java2Demo.jar;D:\JBuilder7\jdk1.3.1\jre\lib\i18n.jar;D:\JBuilder7\jdk1.3.1\jre\lib\jaws.jar;D:\JBuilder7\jdk1.3.1\jre\lib\rt.jar;D:\JBuilder7\jdk1.3.1\jre\lib\sunrsasign.jar;D:\JBuilder7\jdk1.3.1\lib\dt.jar;D:\JBuilder7\jdk1.3.1\lib\htmlconverter.jar;D:\JBuilder7\jdk1.3.1\lib\tools.jar"? -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,address=zw2k:1992,suspend=y
?csi.utilitytools. COMUT
壹千萬(wàn)貳百萬(wàn)零叁千零壹分整650103197601130732
true