Repository: primefaces/primefaces (java)

Issue:
DatePicker: firstDayWeekOffset not calculated / IndexOfOutBound while submitting
It was introduced here: #5713


i have the problem when i enable week selection (like in the showcase) and set firstDayOfWeek=2, that firstDayWeekOffset is not caulcated. SundayIndex will become 5:

```
            var sundayIndex = this.getSundayIndex();
            if (this.options.locale.firstDayWeekOffset === undefined) {
                if (sundayIndex == 0) {
                    this.options.locale.firstDayWeekOffset = 6;
                }
                else if (sundayIndex == 1) {
                    this.options.locale.firstDayWeekOffset = 12;
                }
                else if (sundayIndex == 6) {
                    this.options.locale.firstDayWeekOffset = 4;
                }
                else {
                    this.options.showWeek = false;
                }
            }
```

@fanste i dont understand everything at all. Do you have a idea?

Pull request description:
Fixes #13608 - firstDayWeekOffset not calculated / IndexOfOutBound while submitting
Fixes #13608

Patch:
diff --git a/primefaces/src/main/frontend/packages/datepicker/datepicker/src/jquery.ui.prime.datepicker.cjs b/primefaces/src/main/frontend/packages/datepicker/datepicker/src/jquery.ui.prime.datepicker.cjs
index 656c42d51b..81be36e1ce 100644
--- a/primefaces/src/main/frontend/packages/datepicker/datepicker/src/jquery.ui.prime.datepicker.cjs
+++ b/primefaces/src/main/frontend/packages/datepicker/datepicker/src/jquery.ui.prime.datepicker.cjs
@@ -670,7 +670,7 @@ $.widget("prime.datePicker", {
                             formattedValue += ' ' + this.options.rangeSeparator + ' ' + this.formatDateTime(endDate);
                         }
 
-                        if (this.options.view === 'week') {
+                        if (this.options.view === 'week' && this.options.showWeek) {
                             var startDateMeta = { day: startDate.getDate(), month: startDate.getMonth(), year: startDate.getFullYear() };
                             var week = this.options.weekCalculator(startDateMeta);
                             formattedValue += ' (' + this.options.locale.weekHeader + ' ' + week + ')';
diff --git a/primefaces/src/main/java/org/primefaces/util/CalendarUtils.java b/primefaces/src/main/java/org/primefaces/util/CalendarUtils.java
index a8bfed15c6..c40a73faac 100644
--- a/primefaces/src/main/java/org/primefaces/util/CalendarUtils.java
+++ b/primefaces/src/main/java/org/primefaces/util/CalendarUtils.java
@@ -648,17 +648,22 @@ public class CalendarUtils {
         }
 
         // fast return
-        if (!input.contains(token)) {
+        if (input == null || !input.contains(token)) {
             return Collections.emptyList();
         }
 
         int tokenIndex = input.indexOf(token);
         String from = input.substring(0, tokenIndex);
         String to = input.substring(tokenIndex + token.length());
+
         if (week) {
             // hardcoded pattern, see JS:
             // formattedValue += ' (' + this.options.locale.weekHeader + ' ' + week + ')';
-            to = to.substring(0, to.lastIndexOf(" ("));
+            int weekNumberIndex = to.lastIndexOf(" (");
+            // there are cases when weeknumber might not get displayed on client and therefore not submitted
+            if (weekNumberIndex != -1) {
+                to = to.substring(0, weekNumberIndex);
+            }
         }
 
         return Arrays.asList(from, to);


Test patch:
diff --git a/primefaces/src/test/java/org/primefaces/util/CalendarUtilsTest.java b/primefaces/src/test/java/org/primefaces/util/CalendarUtilsTest.java
index 401933b6c8..b528df4bba 100644
--- a/primefaces/src/test/java/org/primefaces/util/CalendarUtilsTest.java
+++ b/primefaces/src/test/java/org/primefaces/util/CalendarUtilsTest.java
@@ -221,8 +221,10 @@ class CalendarUtilsTest {
         assertEquals(2, splitRange.size());
         assertEquals("2021-03-01", splitRange.get(0));
         assertEquals("2021-03-31", splitRange.get(1));
+
         splitRange = CalendarUtils.splitRange("2021-03-01", "yyyy-MM-dd", "-", false);
         assertTrue(splitRange.isEmpty());
+
         splitRange = CalendarUtils.splitRange("", "yyyy-MM-dd", "-", false);
         assertTrue(splitRange.isEmpty());
 
@@ -237,13 +239,26 @@ class CalendarUtilsTest {
         assertEquals(2, splitRange.size());
         assertEquals("2021-03-01", splitRange.get(0));
         assertEquals("2021-03-31", splitRange.get(1));
+
         splitRange = CalendarUtils.splitRange("2021-03-01 (Wk 21)", "yyyy-MM-dd", "-", true);
         assertTrue(splitRange.isEmpty());
+
         splitRange = CalendarUtils.splitRange("", "yyyy-MM-dd (Wk 21)", "-", true);
         assertTrue(splitRange.isEmpty());
 
+        splitRange = CalendarUtils.splitRange("2021-03-01 - 2021-03-31 (Wk NaN)", "yyyy-MM-dd", "-", true);
+        assertEquals(2, splitRange.size());
+        assertEquals("2021-03-01", splitRange.get(0));
+        assertEquals("2021-03-31", splitRange.get(1));
+
+        splitRange = CalendarUtils.splitRange("2021-03-01 - 2021-03-31", "yyyy-MM-dd", "-", true);
+        assertEquals(2, splitRange.size());
+        assertEquals("2021-03-01", splitRange.get(0));
+        assertEquals("2021-03-31", splitRange.get(1));
+
         assertThrows(FacesException.class, () -> {
             CalendarUtils.splitRange("2021 - 03 - 01 - 2021 - 03 - 31 (Wk 21)", "yyyy - MM - dd", "-", true);
         });
     }
+
 }