服务器之家

服务器之家 > 正文

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

时间:2021-01-11 14:27     来源/作者:索隆

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
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程序设计有所帮助。

原文链接:http://blog.csdn.net/lushuaiyin/article/details/46300193

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享 2020-04-07
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部