From b4f011230fd5c9d56a0dd7ad7c00c584e25ee990 Mon Sep 17 00:00:00 2001 From: Anna Koskinen Date: Fri, 22 Jan 2021 13:08:02 +0200 Subject: DateField value should actively adjust to the set resolution. (#12183) --- .../main/java/com/vaadin/ui/AbstractDateField.java | 62 ++++++++++++++-------- .../java/com/vaadin/ui/AbstractLocalDateField.java | 20 ++++--- .../com/vaadin/ui/AbstractLocalDateTimeField.java | 7 +-- 3 files changed, 57 insertions(+), 32 deletions(-) (limited to 'server/src/main') diff --git a/server/src/main/java/com/vaadin/ui/AbstractDateField.java b/server/src/main/java/com/vaadin/ui/AbstractDateField.java index 84a26436c0..dbbc19b673 100644 --- a/server/src/main/java/com/vaadin/ui/AbstractDateField.java +++ b/server/src/main/java/com/vaadin/ui/AbstractDateField.java @@ -38,9 +38,9 @@ import java.util.logging.Logger; import java.util.stream.Collectors; import java.util.stream.Stream; -import com.googlecode.gentyref.GenericTypeReflector; import org.jsoup.nodes.Element; +import com.googlecode.gentyref.GenericTypeReflector; import com.vaadin.data.Result; import com.vaadin.data.ValidationResult; import com.vaadin.data.Validator; @@ -179,7 +179,7 @@ public abstract class AbstractDateField * Note: Negative, i.e. BC dates are not supported. *

- * Note: It's usually recommended to use only one of the following at the same - * time: Range validator with Binder or DateField's setRangeStart check. + * Note: It's usually recommended to use only one of the following at the + * same time: Range validator with Binder or DateField's setRangeStart + * check. * * @param startDate * - the allowed range's start date @@ -377,8 +378,11 @@ public abstract class AbstractDateField - * Note: It's usually recommended to use only one of the following at the same - * time: Range validator with Binder or DateField's setRangeEnd check. + * Note: It's usually recommended to use only one of the following at the + * same time: Range validator with Binder or DateField's setRangeEnd check. * * @param endDate * the allowed range's end date (inclusive, based on the current @@ -545,8 +549,8 @@ public abstract class AbstractDateField validator = getRangeValidator(); - ValidationResult result = validator.apply(value, + ValidationResult result = validator.apply(adjusted, new ValueContext(this, this)); if (result.isError()) { @@ -718,25 +723,40 @@ public abstract class AbstractDateField getRangeValidator() { return new DateRangeValidator(getDateOutOfRangeMessage(), - getDate(getRangeStart(), getResolution()), - getDate(getRangeEnd(), getResolution())); + adjustToResolution(getRangeStart(), getResolution()), + adjustToResolution(getRangeEnd(), getResolution())); } @Override @@ -134,7 +134,9 @@ public abstract class AbstractLocalDateField return Date.from(date.atStartOfDay(ZoneOffset.UTC).toInstant()); } - private LocalDate getDate(LocalDate date, DateResolution forResolution) { + @Override + protected LocalDate adjustToResolution(LocalDate date, + DateResolution forResolution) { if (date == null) { return null; } @@ -171,19 +173,21 @@ public abstract class AbstractLocalDateField 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")) { + if (getDateFormat() != null && getDateFormat().contains("w")) { Date parsedDate; - SimpleDateFormat df = new SimpleDateFormat(this.getDateFormat(),this.getLocale()); + SimpleDateFormat df = new SimpleDateFormat(getDateFormat(), + getLocale()); try { parsedDate = df.parse(dateString); } catch (ParseException e) { return super.handleUnparsableDateString(dateString); } - ZoneId zi = this.getZoneId(); - if (zi == null) { + ZoneId zi = getZoneId(); + if (zi == null) { zi = ZoneId.systemDefault(); } - LocalDate date = Instant.ofEpochMilli(parsedDate.getTime()).atZone(zi).toLocalDate(); + LocalDate date = Instant.ofEpochMilli(parsedDate.getTime()) + .atZone(zi).toLocalDate(); return Result.ok(date); } else { return super.handleUnparsableDateString(dateString); diff --git a/server/src/main/java/com/vaadin/ui/AbstractLocalDateTimeField.java b/server/src/main/java/com/vaadin/ui/AbstractLocalDateTimeField.java index 6f7037d732..39f16735a9 100644 --- a/server/src/main/java/com/vaadin/ui/AbstractLocalDateTimeField.java +++ b/server/src/main/java/com/vaadin/ui/AbstractLocalDateTimeField.java @@ -111,8 +111,8 @@ public abstract class AbstractLocalDateTimeField @Override protected RangeValidator getRangeValidator() { return new DateTimeRangeValidator(getDateOutOfRangeMessage(), - getDate(getRangeStart(), getResolution()), - getDate(getRangeEnd(), getResolution())); + adjustToResolution(getRangeStart(), getResolution()), + adjustToResolution(getRangeEnd(), getResolution())); } @Override @@ -143,7 +143,8 @@ public abstract class AbstractLocalDateTimeField return Date.from(date.toInstant(ZoneOffset.UTC)); } - private LocalDateTime getDate(LocalDateTime date, + @Override + protected LocalDateTime adjustToResolution(LocalDateTime date, DateTimeResolution forResolution) { if (date == null) { return null; -- cgit v1.2.3