利用Java怎么将数字日期翻译成英文单词
短信预约 -IT技能 免费直播动态提醒
利用Java怎么将数字日期翻译成英文单词?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
具体如下:
package com.sunyard.etp.ag.util;import java.math.BigDecimal;import java.util.Arrays;public class DateEngUtil { public static void main(String[] args) { // TODO Auto-generated method stub String dd="19870401"; String res=formatDateEng(dd); System.out.println(dd+",英文==="+res); String dd2="19000823"; String res2=formatDateEng(dd2); System.out.println(dd2+",英文==="+res2); String dd3="20001231"; String res3=formatDateEng(dd3); System.out.println(dd3+",英文==="+res3); String dd4="20121116"; String res4=formatDateEng(dd4); System.out.println(dd4+",英文==="+res4); String dd5="20150901"; String res5=formatDateEng(dd5); System.out.println(dd5+",英文==="+res5); //支持最多4位数字的翻译 String nn1="12"; String r1=translateNum9999NoZero(nn1); System.out.println(nn1+",英文==="+r1); String nn2="561"; String r2=translateNum9999NoZero(nn2); System.out.println(nn2+",英文==="+r2); String nn3="5589"; String r3=translateNum9999NoZero(nn3); System.out.println(nn3+",英文==="+r3); String cardNum="6210910002000718183"; String cardn=translateCardNumber(cardNum); System.out.println(cardNum+",英文==="+cardn); String fff="3394.56"; System.out.println("钱"+fff+"==="+translateMoney(fff)); String fff2="123456789123456.45"; System.out.println("钱"+fff2+"==="+translateMoney(fff2)); String fff3="78.00"; System.out.println("钱"+fff3+"==="+translateMoney(fff3)); String fff4="12000089103456.45"; System.out.println("钱"+fff4+"==="+translateMoney(fff4)); String fff5="123456789.12"; System.out.println("钱"+fff5+"==="+translateMoney(fff5)); String p1="0%"; System.out.println("百分比"+p1+"==="+translatePercent(p1)); String p2="0.0%"; System.out.println("百分比"+p2+"==="+translatePercent(p2)); String p3="0.00%"; System.out.println("百分比"+p3+"==="+translatePercent(p3)); String p4="0.23%"; System.out.println("百分比"+p4+"==="+translatePercent(p4)); String p5="0.05%"; System.out.println("百分比"+p5+"==="+translatePercent(p5)); String p6="56.00%"; System.out.println("百分比"+p6+"==="+translatePercent(p6)); String p7="56.67%"; System.out.println("百分比"+p7+"==="+translatePercent(p7)); String p8="237.90%"; System.out.println("百分比"+p8+"==="+translatePercent(p8)); } public static String formatDate(String Date){ try{ return Date.substring(0,4)+"年"+Date.substring(4,6)+"月"+Date.substring(6,8)+"日"; }catch (Exception e) { return ""; } } public static String formatDateEng(String Date){ if(Date==null||Date.trim().equals("")){ return ""; } if(Date.trim().length()<8){ return ""; } String DateStr=Date.trim(); String year=DateStr.substring(0,4); String month=DateStr.substring(4,6); String date2=DateStr.substring(6,8); String res=""; res=translateYear(year)+", "+translateMonth(month)+", "+translateDate(date2); return res; } public static String translateYear(String year) { if (year == null || year.trim().equals("")) { return ""; } String res = ""; int yearNum = Integer.valueOf(year); if(year.endsWith("00")){//xx00 switch (yearNum) { case 2000: res = "two thousand";// break; case 1900: res = "nineteen hundred";// break; case 1800: res = "eighteen hundred";// break; case 1700: res = "seventeenth hundred";// break; case 1600: res = "sixteenth hundred";// break; case 1500: res = "fifteenth hundred";// break; default: res = ""; } }else{//19xx,20xx,21xx String qian2=year.substring(0, 2); String hou2=year.substring(2, 4); int qian2Num = Integer.valueOf(qian2); int hou2Num = Integer.valueOf(hou2); if(year.startsWith("20")){//20xx res = "two thousand and "+translateNum20wei(hou2Num); }else{//19xx,21xx res = translateNum20wei(qian2Num)+" "+translateNum20wei(hou2Num); } } return res; } public static String translateMonth(String month) { if (month == null || month.trim().equals("")) { return ""; } String res = ""; int mon = Integer.valueOf(month); switch (mon) { case 1: res = "January";// Jan break; case 2: res = "February";// Feb break; case 3: res = "March";// Mar break; case 4: res = "April";// Apr break; case 5: res = "May";// May break; case 6: res = "June";// Jun break; case 7: res = "July";// Jul break; case 8: res = "August";// Aug break; case 9: res = "September";// Sep break; case 10: res = "October";// Oct break; case 11: res = "November";// Nov break; case 12: res = "December";// Dec break; default: res = ""; } return res; } public static String translateDate(String date) { if (date == null || date.trim().equals("")) { return ""; } String res = ""; int dateNum = Integer.valueOf(date); switch (dateNum) { case 1: res = "first";// break; case 2: res = "second";// break; case 3: res = "third";// break; case 4: res = "fourth";// break; case 5: res = "fifth";// break; case 6: res = "sixth";// break; case 7: res = "seventh";// break; case 8: res = "eigth";// break; case 9: res = "ninth";// break; case 10: res = "tenth";// break; case 11: res = "eleventh";// break; case 12: res = "twelfth";// break; case 13: res = "thirteenth";// break; case 14: res = "fourteenth";// break; case 15: res = "fifteenth";// break; case 16: res = "sixteenth";// break; case 17: res = "seventeenth";// break; case 18: res = "eighteenth";// break; case 19: res = "nineteenth";// break; case 20: res = "twentieth";// break; case 21: res = "twenty first";// break; case 22: res = "twenty second";// break; case 23: res = "twenty third";// break; case 24: res = "twenty fourth";// break; case 25: res = "twenty fifth";// break; case 26: res = "twenty sixth";// break; case 27: res = "twenty seventh";// break; case 28: res = "twenty eigth";// break; case 29: res = "twenty ninth";// break; case 30: res = "thirtieth";// break; case 31: res = "thirty first";// break; default: res = ""; } return res; } ///////////////// public static String translateNum19(int num){ String res = ""; switch (num) { case 0:res = " ";break; case 1:res = "one";break; case 2:res = "two";break; case 3:res = "three";break; case 4:res = "four";break; case 5:res = "five";break; case 6:res = "six";break; case 7:res = "seven";break; case 8:res = "eight";break; case 9:res = "nine";break; case 10:res = "ten";break; case 11:res = "eleven";break; case 12:res = "twelve";break; case 13:res = "thirteen";break; case 14:res = "fourteen";break; case 15:res = "fifteen";break; case 16:res = "sixteen";break; case 17:res = "seventeen";break; case 18:res = "eighteen";break; case 19:res = "nineteen";break; default: res = ""; } return res; } public static String translateNum2090(int num){ String res = ""; switch (num) { case 20:res = "twenty";break; case 30:res = "thirty";break; case 40:res = "forty";break; case 50:res = "fifty";break; case 60:res = "sixty";break; case 70:res = "seventy";break; case 80:res = "eighty";break; case 90:res = "ninety";break; default: res = ""; } return res; } public static String translateNum20wei(int num){ String res = ""; int zheng=num/10;//十位 int yu=num%10;//个位 if(num<20){ if(num<10){ res="zero"+translateNum19(num); }else{ res=translateNum19(num); } }else{ res=translateNum2090(zheng*10)+" "+translateNum19(yu); } return res; } public static String translateNum20weiNoZero(int num){ String res = ""; int zheng=num/10;//十位 int yu=num%10;//个位 if(num<20){ res=translateNum19(num); }else{ res=translateNum2090(zheng*10)+" "+translateNum19(yu); } return res; } public static String translateNum9999NoZero(int num){ String res = ""; int qian=num/1000;//千位 int last3=num%1000; int bai=last3/100;//百位 int last2=num%100; int shi=last2/10;//十位 int ge=num%10;//个位 if(qian!=0){ res+=translateNum19(qian)+" thousand "; } if(bai!=0){ res+=translateNum19(bai)+" hundred "; } String last2eng=translateNum20weiNoZero(last2); if(!res.equals("")&&!(last2eng.trim().equals(""))){ res+=" and "; } res+=last2eng+" "; return res; } public static String translateNum9999NoZero(String num){ if(num==null||num.trim().equals("")){ return ""; } int nn=Integer.valueOf(num); return translateNum9999NoZero(nn); } ////////////////////////////////////////下面是英文数字的直白读法,应用场景:卡号///////////////////////////// public static String translateNum09(int num){ String res = ""; switch (num) { case 0:res = "zero";break; case 1:res = "one";break; case 2:res = "two";break; case 3:res = "three";break; case 4:res = "four";break; case 5:res = "five";break; case 6:res = "six";break; case 7:res = "seven";break; case 8:res = "eight";break; case 9:res = "nine";break; default: res = ""; } return res; } public static String translateCardNumber(String number){ if(number==null||number.trim().equals("")){ return ""; } String res = ""; String numstr=number.trim(); char[] numArr=numstr.toCharArray(); for(int i=0;i<numArr.length;i++){ char ai=numArr[i]; String ain=Character.toString(ai); int bi=Integer.valueOf(ain);// System.out.println(translateNum09(bi)); res+=" "+translateNum09(bi); } return res; } ////////////////////////////////////////下面是英文数字的读法,应用场景:钱///////////////////////////// public static String translateMoney(String money){ if(money==null||money.trim().equals("")){ return ""; } String res = ""; String moneyStr=money.trim(); String moneyStr1=""; String moneyStr2=""; if(moneyStr.indexOf(".")!=-1){//有小数点 moneyStr1=moneyStr.substring(0, (moneyStr.indexOf("."))); moneyStr2=moneyStr.substring(moneyStr.indexOf(".")+1); }else{ moneyStr1=moneyStr; moneyStr2=""; } String trans1=translateMoney1(moneyStr1); String trans2=translateMoney2(moneyStr2); if(trans1.trim().equals("")){ res="zero point "+trans2; }else{ res=trans1+" point "+trans2; } return res; } public static String translateMoney1(String moneyStr1){ if(moneyStr1==null||moneyStr1.trim().equals("")||moneyStr1.length()>15){ return ""; } String res = ""; String moneyStr=moneyStr1.trim(); String[] moneyArr=getThreeArr(moneyStr); if(moneyArr[0]!=null&&!moneyArr[0].trim().equals("")){ res+=translateNum9999NoZero(moneyArr[0].trim())+" trillion, "; } if(moneyArr[1]!=null&&!moneyArr[1].trim().equals("")){ res+=translateNum9999NoZero(moneyArr[1].trim())+" billion, "; } if(moneyArr[2]!=null&&!moneyArr[2].trim().equals("")){ res+=translateNum9999NoZero(moneyArr[2].trim())+" million, "; } if(moneyArr[3]!=null&&!moneyArr[3].trim().equals("")){ res+=translateNum9999NoZero(moneyArr[3].trim())+" thousand, "; } if(moneyArr[4]!=null&&!moneyArr[4].trim().equals("")){ res+=translateNum9999NoZero(moneyArr[4].trim())+" "; } return res; } //翻译小数点后面的数字,其实就是按位直接翻译单个数字即可。 public static String translateMoney2(String moneyStr2){ return translateCardNumber(moneyStr2); } public static String[] getThreeArr(String number){ if(number==null||number.trim().equals("")){ return null; } String[] res= new String [5]; BigDecimal dd=new BigDecimal("0"); BigDecimal bd1000=new BigDecimal("1000");// dd.divideAndRemainder(divisor) try{ dd=new BigDecimal(number.trim());//123,456,789,222,555 BigDecimal dd30=dd.divideAndRemainder(bd1000)[1];//3wei,555(0是整数,1是余数) BigDecimal dd2=dd.divideAndRemainder(bd1000)[0];//123,456,789,222 BigDecimal dd64=dd2.divideAndRemainder(bd1000)[1];//thousand,222 BigDecimal dd3=dd2.divideAndRemainder(bd1000)[0];//123,456,789(0是整数,1是余数) BigDecimal dd97=dd3.divideAndRemainder(bd1000)[1];//million,789 BigDecimal dd4=dd3.divideAndRemainder(bd1000)[0];////123,456 BigDecimal dd1210=dd4.divideAndRemainder(bd1000)[1];//billion,456 BigDecimal dd5=dd4.divideAndRemainder(bd1000)[0];////123 BigDecimal dd1513=dd5.divideAndRemainder(bd1000)[1];//trillion,123 res[0]=dd1513.toString(); res[1]=dd1210.toString(); res[2]=dd97.toString(); res[3]=dd64.toString(); res[4]=dd30.toString(); for(int i=0;i<res.length;i++){ String nn=res[i]; if(nn.trim().equals("0")){ res[i]=""; } } }catch(Exception e){ return null; }// System.out.println("getThreeArr=="+Arrays.toString(res)); return res; } //讲一个字符串反序 public static String reverseStr(String str){ if(str==null||str.trim().equals("")){ return ""; } String strre=""; String str2=str.trim(); for(int i=str2.length()-1;i>=0;i--){ strre+=String.valueOf(str2.charAt(i));//反序后 } return strre; } /////////////////////////////翻译百分比,例如12.12%/////////////////////////////////////// public static String translatePercent(String percentNum){ if(percentNum==null||percentNum.trim().equals("")||!(percentNum.trim().endsWith("%"))){ return ""; } String res = ""; String percent=percentNum.trim().substring(0, percentNum.indexOf("%")); String percent1=""; String percent2=""; if(percent.indexOf(".")!=-1){//有小数点 percent1=percent.trim().substring(0, percent.indexOf(".")); percent2=percent.trim().substring(percent.indexOf(".")+1); }else{ percent1=percent; percent2=""; } double nu=Double.valueOf(percent); if(nu==0){ res="zero point zero zero percent"; }else{ double nu2=Double.valueOf(percent1); if(nu2==0){ res+=" zero "; } res+=translateNum9999NoZero(percent1)+" point "+translateCardNumber(percent2)+" percent "; } return res; }}
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
利用Java怎么将数字日期翻译成英文单词
下载Word文档到电脑,方便收藏和打印~
下载Word文档
猜你喜欢
利用Java怎么将数字日期翻译成英文单词
利用Java怎么将数字日期翻译成英文单词?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。具体如下:package com.sunyard.etp.ag.util;
2023-05-31
使用java怎么实现一个英文文本单词翻译器
今天就跟大家聊聊有关使用java怎么实现一个英文文本单词翻译器,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。直接上代码:package fanyi;import java.io.Bu
2023-05-31
怎么用Python将所有的英文单词首字母变成大写
本篇内容主要讲解“怎么用Python将所有的英文单词首字母变成大写”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“怎么用Python将所有的英文单词首字母变成大写”吧!将英文单词首字母变成大写是非
2023-06-15