翻译月份名称回复

时间:2019-06-12 08:54:04

标签: java oracle format locale navicat

我有一个名为CREATED_AT的表 我写了一个查询并收到如下响应: Score D 27 01 17 02 80 03 55 06

这是我的查询:

SELECT SUM(AMOUNT) as scores,to_char(CREATED_AT, 'MONTH') as  d  
FROM C_SCORES 
WHERE USER_ID =201 and to_char(CREATED_AT, 'YYYY') ='1398' 
GROUP BY to_char(CREATED_AT, 'MONTH');

实体代码为:

    public class ScoreCategorizeWithMonth {

        private int Scores;
        private int month;
    private String monthfa;


 public String getMonthfa() {
        switch (month) {
            case 1:
                monthfa = "فروردین";
                break;
            case 2:
                monthfa = "اردیبهشت";
                break;
            case 3:
                monthfa = "خرداد";
                break;
            case 4:
                monthfa = "تیر";
                break;
            case 5:
                monthfa = "مرداد";
                break;
            case 6:
                monthfa = "شهریور";
                break;
            case 7:
                monthfa = "مهر";
                break;
            case 8:
                monthfa = "آبان";
                break;
            case 9:
                monthfa = "آذر";
                break;
            case 10:
                monthfa = "دی";
                break;
            case 11:
                monthfa = "بهمن";
                break;
            case 12:
                monthfa = "اسفند";
            default:
                monthfa = "";
        }



        return monthfa;
    }

    }

此代码在控制器中:

 @GetMapping(value = "score-Categorize-months")
    public List<ScoreCategorizeWithMonth> scoreCategorizedWithMonths()
    {
        return   iScoresSrv.scoreCategorizeWithMonths(userInfo.getUserId());
    }

我的问题是:

2-我的回答是数据库中存在的月份。如何显示月份甚至不在数据库中

 {
    "scores": 27,
    "month": 1,
    "monthfa": "فروردین"
  },
  {
    "scores": 17,
    "month": 2,
    "monthfa": "اردیبهشت"
  },
  {
    "scores": 84,
    "month": 3,
    "monthfa": "خرداد"
  },
  {
    "scores": 55,
    "month": 6,
    "monthfa": "شهریور"
  } 

我还要显示分数== 0的4..5月份

谢谢

3 个答案:

答案 0 :(得分:1)

第2步和第3步:您可以通过以下查询来实现:

select sum(AMOUNT) as scores, month from  
(select AMOUNT ,to_char(CREATED_AT, 'MONTH') as month from C_SCORES WHERE USER_ID 
 =201 and to_char(CREATED_AT, 'YYYY') ='1398'
 union
 select 0 as AMOUNT, Month from
 (SELECT 1 AS MONTH
 UNION SELECT 2 AS MONTH
 UNION SELECT 3 AS MONTH
 UNION SELECT 4 AS MONTH
 UNION SELECT 5 AS MONTH
 UNION SELECT 6 AS MONTH
 UNION SELECT 7 AS MONTH
 UNION SELECT 8 AS MONTH
 UNION SELECT 9 AS MONTH
 UNION SELECT 10 AS MONTH
 UNION SELECT 11 AS MONTH
 UNION SELECT 12 AS MONTH) TBL1) TBL2
 group by month;

只需将1-12替换为您的月份值

答案 1 :(得分:0)

您可以使用属性文件...

此链接提供了许多示例: http://zetcode.com/java/resourcebundle/

在该教程中,'words.properties'是您的默认语言和后备语言文件,而words_de.propertieswords_sk.properties是针对自定义翻译语言的

在这一行:source.setBasenames("messages/words");中,他试图将翻译文件传递给算法。

并使用messageSource.getMessage("w1", null, Locale.GERMAN)获取所需的语言。...

请注意,sk中的words_sk.properties是语言iso代码,波斯语为fa,英语为en ...

某些系统也使用本地化,它们定义了自定义字符和格式,我不确定该算法是否支持这种命名方式……您可以传递额外的参数:fa-IR或{{ 1}}

说实话,我看上去并不深,但是自从几年前使用它以来,这就是原理。

答案 2 :(得分:0)

我只是在Java中添加sume代码来填充无效的索引。 解决起来很容易。但是我有点困惑,没有答案没有帮助我

 public List<ScoreCategorizedWithMonth> scoreCategorizeWithMonths(Long userId) {
        List<ScoreCategorizedWithMonth> monthCategory = scoreDao.scoreCategorizeWithMonths(userId);
        List<ScoreCategorizedWithMonth> result = new ArrayList<>();
        for (int i=1; i<=12;i++)
        {
            ScoreCategorizedWithMonth scoreCategorizedWithMonth = checkMonthCatIsExist(i, monthCategory);
            result.add(scoreCategorizedWithMonth);
        }
        return result;
    }

    private ScoreCategorizedWithMonth checkMonthCatIsExist(int month, List<ScoreCategorizedWithMonth> monthCategory) {
        for (ScoreCategorizedWithMonth cat:monthCategory) {
            if(cat.getMonth() == month){
                return cat;
            }
        }
        ScoreCategorizedWithMonth temp= new  ScoreCategorizedWithMonth();
        temp.setMonth(month);
        temp.setScores(0);
        return temp;
    }