如何使用SimpleDateFormat在句子大小写中格式化西班牙语月份? [英] How to format the Spanish month in sentence case using SimpleDateFormat?

查看:34
本文介绍了如何使用SimpleDateFormat在句子大小写中格式化西班牙语月份?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;
import java.text.SimpleDateFormat;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
    public static void main (String[] args) throws java.lang.Exception
    {
        SimpleDateFormat date = new SimpleDateFormat("dd-MMM-yyyy", new Locale("es","ar"));
        System.out.println(date.format(new Date(2014-1900,0,1)));
    }
}

上述代码返回

01-ene-2014

但是,月份应为大小写,即Ene

有人能帮我解决如何不使用子字符串获取01-Ene-2014吗?

推荐答案

这不是错误。

SimpleDateFormat根据本地规则使用月份名称和大小写。

在英语月份中,第一个字母大写的Apper在英语语法规则中是强制性的。

在西班牙语中是不一样的。必须使用月份名称作为小写。Java使用本地规则。这些西班牙语规则例如由RAE(皇家西班牙语学院)

定义

此外,无法使用您自己的规则创建自定义区域设置,但您可以使用DateFormatSymbols类将月份名称重写为您自己的名称。

DateFormatSymbols sym = DateFormatSymbols.getInstance(baseLocale);
sym.setShortMonths(new String[]{"Ene","Feb","Mar", /* and others */ });
new SimpleDateFormat(aPattern, sym);

完整示例:http://ideone.com/R7uoW0

这篇关于如何使用SimpleDateFormat在句子大小写中格式化西班牙语月份?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆