Reputation: 340350
How can I change the first day of week in the Vaadin calendar widgets, DateField & InlineDateField? For example, in the screen shot above, the week begins on a Sunday but I want Monday.
I know those widgets respect the Locale, and adjusts accordingly. For example, assigning Locale.FRANCE gives me Monday as first-day-of-week (and French language for name of month and days) while Locale.CANADA_FRENCH gives me Sunday as first-day-of-week.
But is there some way to specifically set the first-day-of-week? In my case I want the user to choose a Locale, but the calendar specifically must have Monday as first-day-of-week to present standard ISO 8601 weeks.
Upvotes: 2
Views: 1412
Reputation: 12637
In Vaadin23 there is a official way..
DatePicker startDatePicker = new DatePicker();
startDatePicker.setI18n(new DatePicker.DatePickerI18n().setFirstDayOfWeek(1));
Upvotes: 1
Reputation: 340350
As mentioned to in the answer by Jens Jansson, we can set the locale of a specific widget to override the default of using the UI’s locale.
I have used that feature as a hack, a workaround to this problem of first-day-of-week.
Say you want to use Locale.CANADA
as the locale of your app generally. But that locale uses Sunday as first day of week, while we want Monday. Perhaps we want the week-of-year feature in an InlineDateField. To that InlineDateField we can assign another English-language locale that uses Monday, such as Locale.UK
where Monday is first-day-of-week. For a French twist, if we generally use Locale.CANADA_FRENCH
, to the widget we could assign Locale.FRANCE
where Monday rather than Sunday comes first.
Not ideal, but I've been using this approach successfully.
Upvotes: 0
Reputation: 37073
you can manipulate the cache of locale data of the UI. e.g.:
class AppUI extends UI {
@Override
protected void init(VaadinRequest vaadinRequest) {
state.localeServiceState.localeData*.firstDayOfWeek = 1
}
}
be aware, that the list localeData
will change with new locales, you make vaadin aware of at runtime. you might want to make sure to fill this list beforehand with your known ones or find means to react to changes.
Upvotes: 2
Reputation: 4686
If your UI locale has to be one but you want date representation to be another within the datefields, you can override the locale specifically in the datefields with dateField.setLocale(Locale.FRANCE);. As far as I know there is no option to set the first day specifically.
Upvotes: 1