2009-12-31 95 views
2

我想以编程方式将1-12范围内的整数转换为相应的月份名称。 (例如1 - > 1月,2 - > 2月)等在一个语句中使用Java Calendar类。获取Java中的月份名称

注意:我只想使用Java Calendar类来完成它。不要建议任何开关盒或字符串阵列解决方案。

谢谢。

回答

7

Calendar类不是在一个语句中获取本地化月份名称时使用的最佳类。

下面是(其中,一月是1),仅使用Calendar类获得由一个int值指定的期望的月份的月份名称的一个示例:

// Month as a number. 
int month = 1; 

// Sets the Calendar instance to the desired month. 
// The "-1" takes into account that Calendar counts months 
// beginning from 0. 
Calendar c = Calendar.getInstance(); 
c.set(Calendar.MONTH, month - 1); 

// This is to avoid the problem of having a day that is greater than the maximum of the 
// month you set. c.getInstance() copies the whole current dateTime from system 
// including day, if you execute this on the 30th of any month and set the Month to 1 
// (February) getDisplayName will get you March as it automatically jumps to the next    
// Month 
c.set(Calendar.DAY_OF_MONTH, 1);  

// Returns a String of the month name in the current locale. 
c.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.getDefault()); 

上面的代码将返回该月系统区域设置中的名称。

如果需要其他语言环境,则可以指定另一个Locale,方法是将Locale.getDefault()替换为特定语言环境,例如Locale.US

+0

虽然不是在一个声明中,这对我来说是一个足够好的答案!谢谢! – 2009-12-31 06:08:17

+0

我实际上正在查看getDisplayNames()方法。任何想法为什么它返回一个Map ?我认为Map 会更合乎逻辑,因为您可以使用适当的Integer值访问任何月份。我看不到有任何要求访问具有月份名称的地图来获取其整数值的要求。我想这就是你说它不是用来获得这个月的最佳班级时的意思。 – camickr 2009-12-31 06:21:32

+0

@camickr:'getDisplayNames'方法看起来好像已经对'Calendar'类进行了改进,以支持由另一个类的添加引起的一些新的用例。我会怀疑'DateFormatSymbols',因为它看起来像该类的许多部分,上述方法已添加到JDK 1.6中。 – coobird 2009-12-31 06:41:19

3

使用DateFormatSymbols

自豪地复制和粘贴从bluebones.net

import java.text.*; 

String getMonthForInt(int m) { 
    String month = "invalid"; 
    DateFormatSymbols dfs = new DateFormatSymbols(); 
    String[] months = dfs.getMonths(); 
    if (m >= 0 && m <= 11) { 
     month = months[m]; 
    } 
    return month; 
} 
+0

使用日历,问题说。 – 2009-12-31 06:05:19

+0

然后使用getDisplayName作为camickr建议 – Anurag 2009-12-31 06:09:07

+0

我从coolbird得到了答案。你的解决方案也很好!我修正了它,如果修改了一下,就满足一个语句的要求!:) – 2009-12-31 06:11:08

2

你看了API? getDisplayName(...)方法看起来是一个开始的好地方。在一个声明中这样做是一个可怕的要求。

0

TL;博士

Month.of(12).getDisplayName(TextStyle.FULL , Locale.US) 

......或者......

Month.DECEMBER.getDisplayName(TextStyle.FULL , Locale.US) 

使用java.time

现代的方式来获得的本地化名称一个月nth与java.time.Month枚举。这个类是java.time包的一部分,比现在取代了麻烦的旧的传统日期时间类,如DateCalendar

要本地化,指定:

  • TextStyle确定字符串应该多长或缩写是。
  • Locale确定(a)用于翻译日期名称,月份名称等的人类语言,以及(b)用于确定缩写,大写,标点符号,分隔符等问题的文化规范。

示例代码。

Month month = Month.of(7); 
String outputConstantName = month.toString(); 
String outputMonthNameEnglish = month.getDisplayName(TextStyle.FULL , Locale.US); 
String outputMonthQuébec = month.getDisplayName(TextStyle.FULL , Locale.CANADA_FRENCH); 

month。的toString():七月

outputMonthNameEnglish:七月

outputMonthQuébec:JUILLET

的名字,而不是一个月号码使用Month枚举对象可以是得心应手,更易于阅读,和不易出错。

String output = Month.JULY.getDisplayName(TextStyle.FULL , Locale.US) ; 

关于java.time

java.time框架是建立在Java 8和更高版本。这些类取代了日期时间类legacy,如java.util.Date,Calendar,& SimpleDateFormat

Joda-Time项目现在位于maintenance mode,建议迁移到java.time类。请参阅Oracle Tutorial。并搜索堆栈溢出了很多例子和解释。规格是JSR 310

从何处获取java.time类?

ThreeTen-Extra项目与其他类扩展java.time。这个项目是未来可能增加java.time的一个试验场。您可以在这里找到一些有用的类,如Interval,YearWeek,YearQuartermore