欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java实现将数字日期翻译成英文单词的工具类实例

程序员文章站 2024-03-03 15:05:22
本文实例讲述了java实现将数字日期翻译成英文单词的工具类。分享给大家供大家参考,具体如下: package com.sunyard.etp.ag.util;...

本文实例讲述了java实现将数字日期翻译成英文单词的工具类。分享给大家供大家参考,具体如下:

package com.sunyard.etp.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
     */
  }
  /**
   * 格式化日期
   *
   * @return
   *
   */
  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||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;
  }
  /**
   * 年转化成英文单词
   * @param month
   * @return
   */
  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;
  }
  /**
   * 月份转化成英文单词
   * @param month
   * @return
   */
  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;
  }
  /**
   * 几号转化成英文单词
   * @param date
   * @return
   */
  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;
  }
  /////////////////
  /**
   * 翻译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;
    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;
  }
  /**
   * 翻译十几,二十几的读法
   * @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 = "";
    int zheng=num/10;//十位
    int yu=num%10;//个位
    if(num<20){
      res=translatenum19(num);
    }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("")&&!(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);
  }
  ////////////////////////////////////////下面是英文数字的直白读法,应用场景:卡号/////////////////////////////
  /**
   * 翻译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||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;
  }
  ////////////////////////////////////////下面是英文数字的读法,应用场景:钱/////////////////////////////
  /**
   * 参数是数字,可以带最多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||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;
  }
  /**
   * //这个是翻译数字的整数部分,这才是关键
   *
   * 参数是数字,可以带最多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 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);
  }
  /**
   *
例如
12345678
要得到
12 345 678
=============
算法如下:
先反序
87654321
从前往后3位截取
876 543 21
每个再反序
678 345 12
这个数组要反序,这就是要的结果
12 345 678
   * @param number
   * @return
   */
  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;
  }
}

运行结果:

Java实现将数字日期翻译成英文单词的工具类实例

更多关于java相关内容感兴趣的读者可查看本站专题:《java日期与时间操作技巧汇总》、《java数据结构与算法教程》、《java操作dom节点技巧总结》和《java缓存操作技巧汇总

希望本文所述对大家java程序设计有所帮助。