diff options
author | Tatu Lund <tatu@vaadin.com> | 2020-07-03 13:17:34 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-07-03 13:17:34 +0300 |
commit | 3640be47c3b21229c55a91b421aec3bbebd425fd (patch) | |
tree | e6128d0323f4dbb03b51c5f08853e312084dff82 /server | |
parent | 40570e17319d96adcfebaaa8c98b1e16f58902f0 (diff) | |
download | vaadin-framework-3640be47c3b21229c55a91b421aec3bbebd425fd.tar.gz vaadin-framework-3640be47c3b21229c55a91b421aec3bbebd425fd.zip |
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
Diffstat (limited to 'server')
-rw-r--r-- | server/src/main/java/com/vaadin/ui/AbstractLocalDateField.java | 28 |
1 files changed, 27 insertions, 1 deletions
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<LocalDate> 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); + } + } } |