From 3640be47c3b21229c55a91b421aec3bbebd425fd Mon Sep 17 00:00:00 2001 From: Tatu Lund Date: Fri, 3 Jul 2020 13:17:34 +0300 Subject: Add support for "ww" in date format (#12037) Calculate number of the week in the year based on Date. Note, support for "ww" is missing from GWT DateTimeFormat and java.util.Calendar is not supported in GWT, hence DIY method is needed. Fixes: #10603 --- .../java/com/vaadin/ui/AbstractLocalDateField.java | 28 +++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) (limited to 'server') diff --git a/server/src/main/java/com/vaadin/ui/AbstractLocalDateField.java b/server/src/main/java/com/vaadin/ui/AbstractLocalDateField.java index 7bd245382f..9314f5d9b9 100644 --- a/server/src/main/java/com/vaadin/ui/AbstractLocalDateField.java +++ b/server/src/main/java/com/vaadin/ui/AbstractLocalDateField.java @@ -15,9 +15,11 @@ */ package com.vaadin.ui; +import java.text.ParseException; +import java.text.SimpleDateFormat; import java.time.Instant; import java.time.LocalDate; -import java.time.LocalDateTime; +import java.time.ZoneId; import java.time.ZoneOffset; import java.time.format.DateTimeFormatter; import java.time.format.FormatStyle; @@ -26,6 +28,7 @@ import java.util.Date; import java.util.Locale; import java.util.Map; +import com.vaadin.data.Result; import com.vaadin.data.validator.DateRangeValidator; import com.vaadin.data.validator.RangeValidator; import com.vaadin.shared.ui.datefield.AbstractTextualDateFieldState; @@ -163,4 +166,27 @@ public abstract class AbstractLocalDateField return temporalAccessor == null ? null : LocalDate.from(temporalAccessor); } + + @Override + protected Result handleUnparsableDateString(String dateString) { + // Handle possible week number, which cannot be parsed client side due + // limitations in GWT + if (this.getDateFormat() != null && this.getDateFormat().contains("w")) { + Date parsedDate; + SimpleDateFormat df = new SimpleDateFormat(this.getDateFormat(),this.getLocale()); + try { + parsedDate = df.parse(dateString); + } catch (ParseException e) { + return super.handleUnparsableDateString(dateString); + } + ZoneId zi = this.getZoneId(); + if (zi == null) { + zi = ZoneId.systemDefault(); + } + LocalDate date = Instant.ofEpochMilli(parsedDate.getTime()).atZone(zi).toLocalDate(); + return Result.ok(date); + } else { + return super.handleUnparsableDateString(dateString); + } + } } -- cgit v1.2.3