我得到一个整数,我需要在不同的语言环境中转换为月份名称:
地区 en-us 示例:
1 -> 一月
2 -> 二月
区域设置 es-mx 的示例:
1 -> Enero
2 -> Febrero
import java.text.DateFormatSymbols;
public String getMonth(int month) {
return new DateFormatSymbols().getMonths()[month-1];
}
Month // Enum class, predefining and naming a dozen objects, one for each month of the year.
.of( 12 ) // Retrieving one of the enum objects by number, 1-12.
.getDisplayName(
TextStyle.FULL_STANDALONE ,
Locale.CANADA_FRENCH // Locale determines the human language and cultural norms used in localizing.
)
由于 Java 1.8(或带有ThreeTen-Backport 的1.7 和 1.6 ),您可以使用它:
Month.of(integerMonth).getDisplayName(TextStyle.FULL_STANDALONE, locale);
请注意,它integerMonth
是从 1 开始的,即 1 代表一月。1 月至 12 月的范围始终为 1 到 12(即仅公历)。
您需要将 LLLL 用于独立的月份名称。这记录在SimpleDateFormat
文档中,例如:
SimpleDateFormat dateFormat = new SimpleDateFormat( "LLLL", Locale.getDefault() );
dateFormat.format( date );
我会使用 SimpleDateFormat。如果有更简单的方法来制作月历,有人纠正我,我现在用代码做这个,我不太确定。
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
public String formatMonth(int month, Locale locale) {
DateFormat formatter = new SimpleDateFormat("MMMM", locale);
GregorianCalendar calendar = new GregorianCalendar();
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.set(Calendar.MONTH, month-1);
return formatter.format(calendar.getTime());
}
这就是我将如何做到的。我将把范围检查留给int month
你。
import java.text.DateFormatSymbols;
public String formatMonth(int month, Locale locale) {
DateFormatSymbols symbols = new DateFormatSymbols(locale);
String[] monthNames = symbols.getMonths();
return monthNames[month - 1];
}
使用 SimpleDateFormat。
import java.text.SimpleDateFormat;
public String formatMonth(String month) {
SimpleDateFormat monthParse = new SimpleDateFormat("MM");
SimpleDateFormat monthDisplay = new SimpleDateFormat("MMMM");
return monthDisplay.format(monthParse.parse(month));
}
formatMonth("2");
结果:二月
显然,在 Android 2.2 中,SimpleDateFormat 存在一个错误。
为了使用月份名称,您必须自己在资源中定义它们:
<string-array name="month_names">
<item>January</item>
<item>February</item>
<item>March</item>
<item>April</item>
<item>May</item>
<item>June</item>
<item>July</item>
<item>August</item>
<item>September</item>
<item>October</item>
<item>November</item>
<item>December</item>
</string-array>
然后在您的代码中使用它们,如下所示:
/**
* Get the month name of a Date. e.g. January for the Date 2011-01-01
*
* @param date
* @return e.g. "January"
*/
public static String getMonthName(Context context, Date date) {
/*
* Android 2.2 has a bug in SimpleDateFormat. Can't use "MMMM" for
* getting the Month name for the given Locale. Thus relying on own
* values from string resources
*/
String result = "";
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int month = cal.get(Calendar.MONTH);
try {
result = context.getResources().getStringArray(R.array.month_names)[month];
} catch (ArrayIndexOutOfBoundsException e) {
result = Integer.toString(month);
}
return result;
}
当您将 DateFormatSymbols 类用于其 getMonthName 方法以按名称获取月份时,它会在某些 Android 设备中按数字显示月份。我通过这种方式解决了这个问题:
在 String_array.xml
<string-array name="year_month_name">
<item>January</item>
<item>February</item>
<item>March</item>
<item>April</item>
<item>May</item>
<item>June</item>
<item>July</item>
<item>August</item>
<item>September</item>
<item>October</item>
<item>November</item>
<item>December</item>
</string-array>
在 Java 类中,只需像这样调用这个数组:
public String[] getYearMonthName() {
return getResources().getStringArray(R.array.year_month_names);
//or like
//return cntx.getResources().getStringArray(R.array.month_names);
}
String[] months = getYearMonthName();
if (i < months.length) {
monthShow.setMonthName(months[i] + " " + year);
}
快乐编码:)
Kotlin 扩展
fun Int.toMonthName(): String {
return DateFormatSymbols().months[this]
}
用法
calendar.get(Calendar.MONTH).toMonthName()
只需插入行
DateFormatSymbols.getInstance().getMonths()[view.getMonth()]
会成功的。
public static void main(String[] args) {
SimpleDateFormat format = new SimpleDateFormat("MMMMM", new Locale("en", "US"));
System.out.println(format.format(new Date()));
}
尝试以一种非常简单的方式使用它并像您自己的函数一样调用它
public static String convertnumtocharmonths(int m){
String charname=null;
if(m==1){
charname="Jan";
}
if(m==2){
charname="Fev";
}
if(m==3){
charname="Mar";
}
if(m==4){
charname="Avr";
}
if(m==5){
charname="Mai";
}
if(m==6){
charname="Jun";
}
if(m==7){
charname="Jul";
}
if(m==8){
charname="Aou";
}
if(m==9){
charname="Sep";
}
if(m==10){
charname="Oct";
}
if(m==11){
charname="Nov";
}
if(m==12){
charname="Dec";
}
return charname;
}