使用Java将日期数字转换为单词

副翼

我想将日期转换为单词。例如:12/12/2012->twelve twelve two thousand twelve并且我已经制作了数字到单词转换器。但是现在我有问题要打印出来。

这是我的代码:

   String patternString = "\\d{2}/\\d{2}/\\d{4}"; // date regex

   Pattern pattern = Pattern.compile(patternString); // pattern compiling
   Matcher matcher = pattern.matcher(nom); // matching with pattern with input text from user

   if (matcher.find()) {
       String get_data = matcher.group();
           if(get_data.contains("/")){ // check either has "/" slash or not
              String parts[] = get_data.split("[/]"); // split process
              String get_day = parts[0]; // day will store in first array
              String get_month = parts[1]; // month will store in second array
              String get_year = parts[2]; // year will store in third array

              String s = NumberConvert.convert(Integer.parseInt(get_day)) 
                        + NumberConvert.convert(Integer.parseInt(get_month)) 
                        + NumberConvert.convert(Integer.parseInt(get_year));
              String replace = matcher.replaceAll(s); // replace number to words
              System.out.println(replace);
            }
   } else {...}

输入来自用户的文本:

12/12/2012 +++ 23/11/2010

但是结果也只打印第一个模式和下一个模式,也用第一个模式的值替换。

twelve twelve two thousand twelve +++ twelve twelve two thousand twelve

请给我建议解决方案

蒂姆·比格莱森(Tim Biegeleisen)

解决问题的直接方法是使用Matcher.replaceFirst(),而不是Matcher.replaceAll(),因为您只希望将第一个日期模式替换为日期的书面版本。

String replace = matcher.replaceFirst(s);

如果您希望一次可以处理每个数字日期,则可以使用以下代码以从左至右的方式进行处理:

String patternString = "\\d{2}/\\d{2}/\\d{4}";

Pattern pattern = Pattern.compile(patternString);
Matcher matcher = pattern.matcher(nom);

String output = "";

while (matcher.find()) {
    String get_data = matcher.group();

    String parts[] = get_data.split("/");
    String get_day = parts[0];
    String get_month = parts[1];
    String get_year = parts[2];

    String s = NumberConvert.convert(Integer.parseInt(get_day)) +
               NumberConvert.convert(Integer.parseInt(get_month)) +
               NumberConvert.convert(Integer.parseInt(get_year));

    if (output.equals("")) {
        output = s;
    }
    else {
        output += " +++ " + s;
    }

    String replace = matcher.replaceFirst("");
    matcher = pattern.matcher(replace);
}

每次迭代之后,上述代码都会Matcher使用一个字符串将其重置,该字符串中先前的匹配日期已从该字符串中删除。这样一来,您就可以一次从左至右“吃”一个日期,从而在您进行操作时建立易于阅读的日期输出。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用Pandas将星期数转换为星期(日期)

来自分类Dev

使用 CALENDAR 将月份和日期数字转换为一年中的第几天

来自分类Dev

将日期数组转换为整数

来自分类Dev

使用javascript将数字转换为日期

来自分类Dev

将日期数字向量转换为单元格数组

来自分类Dev

将数字转换为单词

来自分类Dev

猫鼬-使用现有条目将日期字段转换为日期数组

来自分类Dev

如何使用“-”将日期作为数字转换为日期

来自分类Dev

将数字转换为日期

来自分类Dev

将数字转换为日期

来自分类Dev

使用 if else 将 5 位数字转换为单词

来自分类Dev

使用XSLT将日期时间转换为完整单词

来自分类Dev

如何使用JS / Jquery将UTC日期数组转换为毫秒

来自分类Dev

将时间戳转换为日期数据类型

来自分类Dev

将星期数转换为R中的日期

来自分类Dev

将varchar数组转换为日期数组-pl sql

来自分类Dev

将时间戳转换为日期数据类型

来自分类Dev

SQL将数字转换为单词

来自分类Dev

将数字(货币)转换为单词的方法

来自分类Dev

将数字转换为数组中的单词

来自分类Dev

将数字转换为Angular中的单词

来自分类Dev

将数字行转换为单词

来自分类Dev

ANDROID:将输入的数字转换为单词

来自分类Dev

将数字转换为相应的单词

来自分类Dev

将月份数字转换为单词

来自分类Dev

使用Python将数字转换为日期格式

来自分类Dev

在Java中将单词转换为数字

来自分类Dev

将字符串数字单词转换为仅字符串数字:Java

来自分类Dev

阻止地图将日期转换为数字