Java实现将数字⽇期翻译成英⽂单词的⼯具类实例
本⽂实例讲述了Java实现将数字⽇期翻译成英⽂单词的⼯具类。分享给⼤家供⼤家参考,具体如下:
package p.ag.util;
import java.math.BigDecimal;
import java.util.Arrays;
public class DateEngUtil {
/**
* @param args
*/
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);
/*
19870401,英⽂===nineteen eighty seven, April, first
19000823,英⽂===nineteen hundred, August, twenty third
20001231,英⽂===two thousand, December, thirty first
20121116,英⽂===two thousand and twelve, November, sixteenth
20150901,英⽂===two thousand and fifteen, September, first
*/
/
/⽀持最多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);
/*
12,英⽂===twelve
561,英⽂===five hundred and sixty one
5589,英⽂===five thousand five hundred and eighty nine
*/
String cardNum="6210910002000718183";
String cardn=translateCardNumber(cardNum);
System.out.println(cardNum+",英⽂==="+cardn);
/*
6210910002000718183,英⽂=== six two one zero nine one zero zero zero two zero zero zero seven one eight one eight three
*/
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));
/*
钱3394.56===three thousand, three hundred and ninety four point five six
钱123456789123456.45===one hundred and twenty three trillion, four hundred and fifty six billion, seven hundred and eighty nine million, one hundred and twenty three thousand, four hundred and fifty six point four five 钱78.00===seventy eight point zero zero
钱12000089103456.45===twelve trillion, eighty nine million, one hundred and three thousand, four hundred and fifty six point four five
钱123456789.12===one hundred and twenty three million, four hundred and fifty six thousand, seven hundred and eighty nine point one two
*/
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));
/
*
百分⽐0%===zero point zero zero percent
百分⽐0.0%===zero point zero zero percent
百分⽐0.00%===zero point zero zero percent
百分⽐0.23%=== zero point two three percent
百分⽐0.05%=== zero point zero five percent
百分⽐56.00%===fifty six point zero zero percent
百分⽐56.67%===fifty six point six seven percent
百分⽐237.90%===two hundred and thirty seven point nine zero percent
*/
}
/
**
* 格式化⽇期
*
public static String formatDate(String Date){
try{
return Date.substring(0,4)+"年"+Date.substring(4,6)+"⽉"+Date.substring(6,8)+"⽇"; }catch (Exception e) {
return "";
}
}
/**
* 格式化⽇期转化成能播报的英⽂
*
* @return
*
*/
public static String formatDateEng(String Date){
if(Date==null||im().equals("")){
return "";
}
im().length()<8){
return "";
}
String im();
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;
}
/**
* 年转化成英⽂单词
* @param month
* @return
*/
public static String translateYear(String year) {
if (year == null || im().equals("")) {
return "";
}
String res = "";
int yearNum = Integer.valueOf(year);
dsWith("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;
}
/
**
* ⽉份转化成英⽂单词
* @param month
* @return
*/
public static String translateMonth(String month) {
if (month == null || im().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 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;
}
/**
* ⼏号转化成英⽂单词
* @param date
* @return
*/
public static String translateDate(String date) { if (date == null || im().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;
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;
}
/////////////////
/**
* 翻译1-19
* @param num
* @return
*/
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;
java switch case stringcase 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;
}
/**
* 翻译⼗⼏,⼆⼗⼏的读法
* @param num
* @return
*/
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;
}
/**
* 翻译2位数字的读法
* @param num
* @return
*/
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;
}
/**
* 翻译2位数字的读法
* @param num
* @return
*/
public static String translateNum20weiNoZero(int num){
String res = "";
}else{
res=translateNum2090(zheng*10)+" "+translateNum19(yu);
}
return res;
}
/**
* 翻译数字的读法,最多⽀持到9999,即4位
* @param num
* @return
*/
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("")&&!(im().equals(""))){
res+=" and ";
}
res+=last2eng+" ";
return res;
}
public static String translateNum9999NoZero(String num){
if(num==null||im().equals("")){
return "";
}
int nn=Integer.valueOf(num);
return translateNum9999NoZero(nn);
}
////////////////////////////////////////下⾯是英⽂数字的直⽩读法,应⽤场景:卡号///////////////////////////// /**
* 翻译0123456789
* @param num
* @return
*/
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;
}
/
**
* 把卡号翻译成英⽂单词。
* 参数是已传数字,不能有任何其他字符
* @param number
* @return
*/
public static String translateCardNumber(String number){
if(number==null||im().equals("")){
return "";
}
String res = "";
String im();
char[] CharArray();
for(int i=0;i<numArr.length;i++){
char ai=numArr[i];
String String(ai);
int bi=Integer.valueOf(ain);
// System.out.println(translateNum09(bi));
res+=" "+translateNum09(bi);
}
return res;
}
/
///////////////////////////////////////下⾯是英⽂数字的读法,应⽤场景:钱/////////////////////////////
/**
* 参数是数字,可以带最多2位⼩数,最⼤⽀持到亿亿,即15位数字(⼩数点前⾯15位)。 * 例如:123456789123456.78
*
* 英⽂数字按3位读,单位都是3位⼀个单位,所以按3位先分好组,再翻译成单词。
* 例如:123,456,789,123,456.78
* 读作:
* one hundred and twenty three trillion,
* four hundred and fifty six billion,
* seven hundred and sixty nine million,
* one hundred and twenty three thousand,
* four hundred and fifty six
* point seven eight
*
*/
public static String translateMoney(String money){
if(money==null||im().equals("")){
return "";
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论