将数字替换为月份名称

时间:2015-10-23 03:26:24

标签: jasper-reports

我有一些包含数字的数据。

1,2,3,4,5,6,7,8,9,10,11,12,代表几个月。

我想更改它,以便在我预览显示的报告时

January
February
March
April 
May
June
July
August
November
December 

我尝试过使用replace()

.replace("1", "January")

当我运行它时,它不起作用

有什么建议吗?

2 个答案:

答案 0 :(得分:1)

您可以使用文字字段表达式,将$V{Month}替换为您的字段名称

<textfieldexpression class="java.lang.String">
    (
        $V{Month}.equals("1") ? "January" : 
        $V{Month}.equals("2") ? "February" :
        $V{Month}.equals("3") ? "March" :
        $V{Month}.equals("4") ? "April" :
        $V{Month}.equals("5") ? "May" :
        $V{Month}.equals("6") ? "June" :
        $V{Month}.equals("7") ? "July" :
        $V{Month}.equals("8") ? "August" :
        $V{Month}.equals("9") ? "September" :
        $V{Month}.equals("10") ? "October" : 
        $V{Month}.equals("11") ? "November" : "December"
    )
</textfieldexpression>

答案 1 :(得分:0)

这应该可以解决您的问题。我在这里做的是将源字符串拆分为ArrayList String,然后使用switch case将相应月份的名称连接到显示为输出的新字符串。 / p>

import java.util.*;

class abc
{
    public static void main (String[] args)
    {
        String str = "1,2,3,4,5,6,7,8,9,10,11,12,";
        String res = "";
        List<String> list = new ArrayList<String>(Arrays.asList(str.split(",")));
        for (String s : list)
        {
            switch (s)
            {
                case "1":
                    res += "January\n";
                    break;
                case "2":
                    res += "February\n";
                    break;
                case "3":
                    res += "March\n";
                    break;
                case "4":
                    res += "April\n";
                    break;
                case "5":
                    res += "May\n";
                    break;
                case "6":
                    res += "June\n";
                    break;
                case "7":
                    res += "July\n";
                    break;
                case "8":
                    res += "August\n";
                    break;
                case "9":
                    res += "September\n";
                    break;
                case "10":
                    res += "October\n";
                    break;
                case "11":
                    res += "November\n";
                    break;
                case "12":
                    res += "December\n";
                    break;
                default:
                    System.err.println("Invalid output");
            }
        }
        System.out.println(res);
    }
}