2

Screen-shot of Vaadin InlineDateField widget

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.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • just for the records: you can NOT trick the component in such a behaviour by using `setShowISOWeekNumbers(true)`; with `Locale.US` the numbers are simply not shown, if the locale's week does not start on monday. – cfrick Jul 31 '14 at 11:20

3 Answers3

2

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.

cfrick
  • 35,203
  • 6
  • 56
  • 68
  • 1
    Can you provide more explanation? I've not heard about this state or cache. I presume most of us have not. And by `state` did you mean `getState` to access a [`UIState`](https://vaadin.com/api/7.2.5/com/vaadin/shared/ui/ui/UIState.html) object?; I do not see any `state` field. – Basil Bourque Jul 31 '14 at 18:55
  • Is there a way to do this for just one widget, such as an inlineDateField? I do not see locale state on the [TextualDateFieldState](https://vaadin.com/api/com/vaadin/shared/ui/datefield/TextualDateFieldState.html) class. – Basil Bourque Jul 31 '14 at 19:04
  • yes it is getState() from UI. the code above is groovy. basically it is calling setFirstDayOfWeek(1) to each entry in the localeData (add get.* to all items there). and no, this solution is hardcore fiddling with the guts of vaadin. due to the class hierarchy of the date fields i have not seen an easy way to restrict it to one field. you could try to manipulate the `DateTimeService dts` field on clients side (disclaimer: random guess) – cfrick Jul 31 '14 at 19:37
1

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.

Jens Jansson
  • 4,626
  • 4
  • 25
  • 29
0

Workaround: Assign Another Locale With Same Language

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.

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154