Garlic Tseng
Garlic Tseng

Reputation: 490

How to get Sunday, Monday .... and Saturday String in the language of the android system?

I want to get Sunday, Monday, Tuesday, Wednesday, Thursday, Friday and Saturday String of the locale of the system. I have the code:

Calendar cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
String SundayString = sdf.format(cal.getTime());

This code can get Sunday. But I have to do it seven times to get all strings I need.

I consider another code below:

Calendar cal = Calendar.getInstance();
String[] weekString = new String[7]
for(int i=1;i<=7;++i){
    cal.set(Calendar.DAY_OF_WEEK, i);
    SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
    weekString[i-1] = sdf.format(cal.getTime());
}

It is work because SUNDAY~SATURDAY are 1~7 but it is not a good code I think because if Android change the definition of the constant variable this code will not execute well.

Is there any other good and elegent way to do?

Upvotes: 2

Views: 369

Answers (3)

QArea
QArea

Reputation: 4981

You can use DateFormatSymbols, see http://developer.android.com/reference/java/text/DateFormatSymbols.html#getWeekdays()

Upvotes: 0

AndroidEnthusiast
AndroidEnthusiast

Reputation: 6657

I don't know how many languages you are supporting but if its a few you could add the actual strings in the values folder.

So for Spanish e.g

in values-es you would have an array of strings

For example, in your strings.xml:

<string-array name="days_of_the_week">
    <item>Monday in Spanish</item>
    <item>Tuesday in Spanish</item>
</string-array>

String[] days = getResources().getStringArray(R.array.days_of_the_week)

Then get them from the array

Upvotes: 0

Blackbelt
Blackbelt

Reputation: 157487

you can use DateFormatSymbols

DateFormatSymbols dateFormatSymbols = new DateFormatSymbols(context.getResources().getConfiguration().locale);
Log.i("DAYS", " " + Arrays.asList(dateFormatSymbols. getWeekdays());

Upvotes: 4

Related Questions