整数を取得し、さまざまなロケールで月の名前に変換する必要があります。
ロケールen-usの例:
1->1月
2->2月
ロケールes-mxの例:
1-> Enero
2-> Febrero
import java.text.DateFormatSymbols;
public String getMonth(int month) {
return new DateFormatSymbols().getMonths()[month-1];
}
スタンドアロンの月の名前にはLLLLを使用する必要があります。これは、次のSimpleDateFormat
ようなドキュメントに記載されています。
SimpleDateFormat dateFormat = new SimpleDateFormat( "LLLL", Locale.getDefault() );
dateFormat.format( date );
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);
これは1ベースであることに注意してくださいintegerMonth
。つまり、1は1月用です。1月から12月の範囲は常に1から12です(つまり、グレゴリオ暦のみ)。
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");
結果:2月
どうやらAndroid2.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;
}
一部のAndroidデバイスでは、getMonthNameメソッドにDateFormatSymbolsクラスを使用して、名前ごとに月を取得すると、番号ごとに月が表示されるという問題があります。私はこの方法でこの問題を解決しました:
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()
public static void main(String[] args) {
SimpleDateFormat format = new SimpleDateFormat("MMMMM", new Locale("en", "US"));
System.out.println(format.format(new Date()));
}
行を挿入するだけ
DateFormatSymbols.getInstance().getMonths()[view.getMonth()]
トリックを行います。
これを非常に簡単な方法で使用して、自分の関数のように呼んでください
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;
}