Set the first day of the week to DatePicker on Monday

I have a DatePicker where I disabled SpinnerView and now use CalendarView. But it starts on Sunday, and I would like to use Monday as the first day of the week. How can i change this? I guessed that the first day would depend on the settings of the phone, but on my Swiss phone it is still wrong ...

I did not find any given method or any XML command.

<DatePicker android:id="@+id/date_picker" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" /> 

and

 DatePicker datePicker = (DatePicker) convertView.findViewById(R.id.date_picker); datePicker.setSpinnersShown(false); 

------------ EDIT: ------------

I tried changing the locale:

 Locale.setDefault(Locale.GERMAN); 

But this did not change the air. So I read the file android.17.android.widget.DatePicker.java, where I found out:

 public DatePicker(Context context, AttributeSet attrs, int defStyle) { //... // initialization based on locale setCurrentLocale(Locale.getDefault()); //... } /** * Sets the current locale. * * @param locale The current locale. */ private void setCurrentLocale(Locale locale) { if (locale.equals(mCurrentLocale)) { return; } mCurrentLocale = locale; mTempDate = getCalendarForLocale(mTempDate, locale); mMinDate = getCalendarForLocale(mMinDate, locale); mMaxDate = getCalendarForLocale(mMaxDate, locale); mCurrentDate = getCalendarForLocale(mCurrentDate, locale); mNumberOfMonths = mTempDate.getActualMaximum(Calendar.MONTH) + 1; mShortMonths = new String[mNumberOfMonths]; for (int i = 0; i < mNumberOfMonths; i++) { mShortMonths[i] = DateUtils.getMonthString(Calendar.JANUARY + i, DateUtils.LENGTH_MEDIUM); } } /** * Gets a calendar for locale bootstrapped with the value of a given calendar. * * @param oldCalendar The old calendar. * @param locale The locale. */ private Calendar getCalendarForLocale(Calendar oldCalendar, Locale locale) { if (oldCalendar == null) { return Calendar.getInstance(locale); } else { final long currentTimeMillis = oldCalendar.getTimeInMillis(); Calendar newCalendar = Calendar.getInstance(locale); newCalendar.setTimeInMillis(currentTimeMillis); return newCalendar; } } 

So why does Locale.setDefault (Locale.GERMAN) not work ?! As you can see above, I get a DatePicker from XML .. is there an error ?!

+4
source share
3 answers

You must set the first day of the week manually, for example, in onCreate () / onCreateView () of your activity / fragment:

 date = (DatePicker) value.findViewById(R.id.date); if (Build.VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB_MR1) { date.setSpinnersShown(false); date.setCalendarViewShown(true); date.getCalendarView().setFirstDayOfWeek(Calendar.getInstance().getFirstDayOfWeek()); } 

The above example uses a standard locale (e.g. Calendar.getInstance ()), however you can specify the day manually:

 date.getCalendarView().setFirstDayOfWeek(Calendar.MONDAY); 
+13
source

Currently accepted answer using getCalendarView() deprecated using Material style.

However, you can call setFirstDayOfWeek() on an instance of DatePicker . Or, if you run DatePickerDialog :

 DatePickerDialog datePickerDlg = new DatePickerDialog(...); datePickerDlg.getDatePicker().setFirstDayOfWeek(Calendar.MONDAY); datePickerDlg.show(); 
+2
source

It depends on the location of your device.

Take a look here: How to get GWT DatePicker to use Monday as the first day of the week?

0
source

All Articles