I am working on an application and I need to return Arabic/French month name and day name, depending on the local I am using. For example, if the local is "Ar" i need the month to be فبراير for F开发者_Python百科ebruary. If local is "Fr" month name should be Février. I am currently working around it and reading the month and day names from different .properties files. Now, my question is: is there any way to return month and day names in different languages depending on the given local?
Your help is much appreciated :)
you do not have to work around it. Use SimpleDateFormat(format, locale)
. Here is the code example:
SimpleDateFormat fen = new SimpleDateFormat("dd/MMMM/yyyy", new Locale("en"));
SimpleDateFormat ffr = new SimpleDateFormat("dd/MMMM/yyyy", new Locale("fr"));
Date date = new Date();
System.out.println(fen.format(date));
System.out.println(ffr.format(date));
Formatter is the most flexible variant. There are convenience methods, like String.format() and printf() that is using Formatter too.
Date d = new Date();
Locale saudi = new Locale("ar","SA");
Formatter formatter = new Formatter(System.out,Locale.FRENCH);
formatter.format("month: %tB\n", d);
System.out.printf(saudi, "month: %tB\n", d);
month: décembre
month: ديسمبر
Use the getMonths
and getWeekdays
methods of DateFormatSymbols
class
String getMonthName(int month, Locale locale) {
return DateFormatSymbols.getInstance(locale).getMonths()[month];
}
Try this one method:pass the value of format in which format you will pass month name parameter like "MMM" for JAN.
public static String GetMonthInArabic(Context ctx, String month, String format)
{
String Mon = month;
try {
Date convertedDate = new Date();
convertedDate = new SimpleDateFormat(format, new Locale("eng")).parse(Mon);
Mon = new SimpleDateFormat(format, new Locale("ar")).format(convertedDate);
} catch (Exception e) {
e.printStackTrace();
}
return Mon;
}
精彩评论