2009-12-31 91 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