diff options
author | elmot <elmotelmot.vaadin.com> | 2018-02-22 13:38:16 +0200 |
---|---|---|
committer | elmot <elmotelmot.vaadin.com> | 2018-02-22 13:38:16 +0200 |
commit | c35e16d78a22bc393a4bc4792080b66e697e3d6d (patch) | |
tree | f5ccc18f03f0e2960f898481fe6b40c5b76a2a8c | |
parent | 6d84351fee4bdb6e877d9100c8645e6c6044864e (diff) | |
download | vaadin-framework-wip-date-time-datatype.tar.gz vaadin-framework-wip-date-time-datatype.zip |
[WIP] getting rid of Date in date inputswip-date-time-datatype
21 files changed, 354 insertions, 272 deletions
diff --git a/client/src/main/java/com/vaadin/client/DateTimeService.java b/client/src/main/java/com/vaadin/client/DateTimeService.java index 4883d34caf..eb1a25f5e4 100644 --- a/client/src/main/java/com/vaadin/client/DateTimeService.java +++ b/client/src/main/java/com/vaadin/client/DateTimeService.java @@ -23,6 +23,7 @@ import java.util.logging.Logger; import com.google.gwt.i18n.client.LocaleInfo; import com.google.gwt.i18n.client.TimeZone; import com.google.gwt.i18n.shared.DateTimeFormat; +import com.vaadin.shared.data.date.VaadinDateTime; import com.vaadin.shared.ui.datefield.DateResolution; /** @@ -202,6 +203,12 @@ public class DateTimeService { return (int) (date.getTime() - date.getTime() / 1000 * 1000); } + public static int getNumberOfDaysInMonth(VaadinDateTime date) { + if (date.getMonth() == 1 && isLeapYear(date.getYear())) { + return 29; + } + return maxDaysInMonth[date.getMonth()]; + } public static int getNumberOfDaysInMonth(Date date) { final int month = date.getMonth(); if (month == 1 && isLeapYear(date)) { @@ -210,6 +217,11 @@ public class DateTimeService { return maxDaysInMonth[month]; } + public static boolean isLeapYear(int year) { + if((year % 400) == 0) return true; + if((year % 100) == 0) return false; + return (year % 4) == 0; + } public static boolean isLeapYear(Date date) { // Instantiate the date for 1st March of that year final Date firstMarch = new Date(date.getYear(), 2, 1); @@ -229,6 +241,10 @@ public class DateTimeService { return (getDayInt(d1) == getDayInt(d2)); } + public static boolean isSameDay(VaadinDateTime d1, VaadinDateTime d2) { + return d1.year==d2.year && d1.month==d2.month && d1.day==d2.day; + } + public static boolean isInRange(Date date, Date rangeStart, Date rangeEnd, DateResolution resolution) { Date s; diff --git a/client/src/main/java/com/vaadin/client/ui/CalendarEntry.java b/client/src/main/java/com/vaadin/client/ui/CalendarEntry.java index d7bea84758..c91c9bd6b5 100644 --- a/client/src/main/java/com/vaadin/client/ui/CalendarEntry.java +++ b/client/src/main/java/com/vaadin/client/ui/CalendarEntry.java @@ -19,42 +19,26 @@ package com.vaadin.client.ui; import java.util.Date; import com.vaadin.client.DateTimeService; +import com.vaadin.shared.data.date.VaadinDateTime; public class CalendarEntry { private final String styleName; - private Date start; - private Date end; + private VaadinDateTime start; + private VaadinDateTime end; private String title; private String description; private boolean notime; @SuppressWarnings("deprecation") - public CalendarEntry(String styleName, Date start, Date end, String title, + public CalendarEntry(String styleName, VaadinDateTime start, VaadinDateTime end, String title, String description, boolean notime) { this.styleName = styleName; - if (notime) { - Date d = new Date(start.getTime()); - d.setSeconds(0); - d.setMinutes(0); - this.start = d; - if (end != null) { - d = new Date(end.getTime()); - d.setSeconds(0); - d.setMinutes(0); - this.end = d; - } else { - end = start; - } - } else { - this.start = start; - this.end = end; - } this.title = title; this.description = description; this.notime = notime; } - public CalendarEntry(String styleName, Date start, Date end, String title, + public CalendarEntry(String styleName, VaadinDateTime start, VaadinDateTime end, String title, String description) { this(styleName, start, end, title, description, false); } @@ -63,19 +47,19 @@ public class CalendarEntry { return styleName; } - public Date getStart() { + public VaadinDateTime getStart() { return start; } - public void setStart(Date start) { + public void setStart(VaadinDateTime start) { this.start = start; } - public Date getEnd() { + public VaadinDateTime getEnd() { return end; } - public void setEnd(Date end) { + public void setEnd(VaadinDateTime end) { this.end = end; } @@ -104,29 +88,29 @@ public class CalendarEntry { } @SuppressWarnings("deprecation") - public String getStringForDate(Date d) { + public String getStringForDate(VaadinDateTime d) { // TODO format from DateTimeService String s = ""; if (!notime) { if (!DateTimeService.isSameDay(d, start)) { s += (start.getYear() + 1900) + "." + (start.getMonth() + 1) - + "." + start.getDate() + " "; + + "." + start.getDay() + " "; } - int i = start.getHours(); + int i = start.getHour(); s += asTwoDigits(i); s += ":"; - i = start.getMinutes(); + i = start.getMinute(); s += asTwoDigits(i); if (!start.equals(end)) { s += " - "; if (!DateTimeService.isSameDay(start, end)) { s += (end.getYear() + 1900) + "." + (end.getMonth() + 1) - + "." + end.getDate() + " "; + + "." + end.getDay() + " "; } - i = end.getHours(); + i = end.getHour(); s += asTwoDigits(i); s += ":"; - i = end.getMinutes(); + i = end.getMinute(); s += asTwoDigits(i); } s += " "; diff --git a/client/src/main/java/com/vaadin/client/ui/VAbstractCalendarPanel.java b/client/src/main/java/com/vaadin/client/ui/VAbstractCalendarPanel.java index 8810b26e30..6b5587da39 100644 --- a/client/src/main/java/com/vaadin/client/ui/VAbstractCalendarPanel.java +++ b/client/src/main/java/com/vaadin/client/ui/VAbstractCalendarPanel.java @@ -58,6 +58,7 @@ import com.vaadin.client.BrowserInfo; import com.vaadin.client.DateTimeService; import com.vaadin.client.WidgetUtil; import com.vaadin.client.ui.aria.AriaHelper; +import com.vaadin.shared.data.date.VaadinDateTime; import com.vaadin.shared.util.SharedUtil; /** @@ -105,7 +106,7 @@ public abstract class VAbstractCalendarPanel<R extends Enum<R>> * value. */ public interface FocusChangeListener { - void focusChanged(Date focusedDate); + void focusChanged(VaadinDateTime focusedDate); } /** @@ -143,7 +144,7 @@ public abstract class VAbstractCalendarPanel<R extends Enum<R>> private Timer mouseTimer; - private Date value; + private VaadinDateTime value; private DateTimeService dateTimeService; @@ -186,7 +187,7 @@ public abstract class VAbstractCalendarPanel<R extends Enum<R>> return; } - Date newDate = ((Day) event.getSource()).getDate(); + VaadinDateTime newDate = ((Day) event.getSource()).getDate(); if (!isDateInsideRange(newDate, getResolution(VAbstractCalendarPanel.this::isDay))) { return; @@ -238,7 +239,7 @@ public abstract class VAbstractCalendarPanel<R extends Enum<R>> * A Date representing the day of month to be focused. Must be * one of the days currently visible. */ - private void focusDay(Date date) { + private void focusDay(VaadinDateTime date) { // Only used when calendar body is present if (acceptDayFocus()) { if (focusedDay != null) { @@ -246,7 +247,7 @@ public abstract class VAbstractCalendarPanel<R extends Enum<R>> } if (date != null && focusedDate != null) { - focusedDate.setTime(date.getTime()); + focusedDate= date; int rowCount = days.getRowCount(); for (int i = 0; i < rowCount; i++) { int cellCount = days.getCellCount(i); @@ -693,7 +694,7 @@ public abstract class VAbstractCalendarPanel<R extends Enum<R>> * @param date * @return */ - private boolean isDateInsideRange(Date date, R minResolution) { + private boolean isDateInsideRange(VaadinDateTime date, R minResolution) { assert (date != null); return isAcceptedByRangeEnd(date, minResolution) @@ -711,7 +712,7 @@ public abstract class VAbstractCalendarPanel<R extends Enum<R>> * @param minResolution * @return */ - private boolean isAcceptedByRangeStart(Date date, R minResolution) { + private boolean isAcceptedByRangeStart(VaadinDateTime date, R minResolution) { assert (date != null); // rangeStart == null means that we accept all values below rangeEnd @@ -719,8 +720,8 @@ public abstract class VAbstractCalendarPanel<R extends Enum<R>> return true; } - Date valueDuplicate = (Date) date.clone(); - Date rangeStartDuplicate = (Date) rangeStart.clone(); + VaadinDateTime valueDuplicate = date; + VaadinDateTime rangeStartDuplicate = (Date) rangeStart.clone(); if (isYear(minResolution)) { return valueDuplicate.getYear() >= rangeStartDuplicate.getYear(); @@ -747,7 +748,7 @@ public abstract class VAbstractCalendarPanel<R extends Enum<R>> * @param minResolution * @return */ - private boolean isAcceptedByRangeEnd(Date date, R minResolution) { + private boolean isAcceptedByRangeEnd(VaadinDateTime date, R minResolution) { assert (date != null); // rangeEnd == null means that we accept all values above rangeStart @@ -849,16 +850,14 @@ public abstract class VAbstractCalendarPanel<R extends Enum<R>> // Zero out hours, minutes, seconds, and milliseconds to compare dates // without time part - final Date tmp = new Date(); - final Date today = new Date(tmp.getYear(), tmp.getMonth(), - tmp.getDate()); + final VaadinDateTime today = VaadinDateTime.today(); - final Date selectedDate = value == null ? null - : new Date(value.getYear(), value.getMonth(), value.getDate()); + final VaadinDateTime selectedDate = value == null ? null + : new VaadinDateTime(value.getYear(), value.getMonth(), value.getDay()); final int startWeekDay = getDateTimeService() .getStartWeekDay(displayedMonth); - final Date curr = (Date) displayedMonth.clone(); + final VaadinDateTime curr = displayedMonth; // Start from the first day of the week that at least partially belongs // to the current month curr.setDate(1 - startWeekDay); @@ -1705,7 +1704,7 @@ public abstract class VAbstractCalendarPanel<R extends Enum<R>> * @param currentDate * The date to set */ - public void setDate(Date currentDate) { + public void setDate(VaadinDateTime currentDate) { doSetDate(currentDate, false, () -> { }); } @@ -1725,7 +1724,7 @@ public abstract class VAbstractCalendarPanel<R extends Enum<R>> * an additional action which will be executed in case * rerendering is not required */ - protected void doSetDate(Date currentDate, boolean needRerender, + protected void doSetDate(VaadinDateTime currentDate, boolean needRerender, Runnable focusAction) { // Check that we are not re-rendering an already active date if (currentDate == value && currentDate != null) { @@ -1739,7 +1738,7 @@ public abstract class VAbstractCalendarPanel<R extends Enum<R>> currentDateWasAdjusted = true; } - Date oldDisplayedMonth = displayedMonth; + VaadinDateTime oldDisplayedMonth = displayedMonth; value = currentDate; // If current date was adjusted, we will not select any date, @@ -1769,7 +1768,7 @@ public abstract class VAbstractCalendarPanel<R extends Enum<R>> } } else { focusedDate = new FocusedDate(value.getYear(), value.getMonth(), - value.getDate()); + value.getDay()); displayedMonth = new FocusedDate(value.getYear(), value.getMonth(), 1); } @@ -1794,20 +1793,20 @@ public abstract class VAbstractCalendarPanel<R extends Enum<R>> * A widget representing a single day in the calendar panel. */ private class Day extends InlineHTML { - private final Date date; + private final VaadinDateTime date; - Day(Date date) { - super("" + date.getDate()); + Day(VaadinDateTime date) { + super("" + date.getDay()); this.date = date; addClickHandler(dayClickHandler); } - public Date getDate() { + public VaadinDateTime getDate() { return date; } } - public Date getDate() { + public VaadinDateTime getDate() { return value; } @@ -1914,9 +1913,9 @@ public abstract class VAbstractCalendarPanel<R extends Enum<R>> private static final String SUBPART_DAY = "day"; private static final String SUBPART_MONTH_YEAR_HEADER = "header"; - private Date rangeStart; + private VaadinDateTime rangeStart; - private Date rangeEnd; + private VaadinDateTime rangeEnd; @Override public String getSubPartName( @@ -1933,8 +1932,8 @@ public abstract class VAbstractCalendarPanel<R extends Enum<R>> // Day, find out which dayOfMonth and use that as the identifier Day day = WidgetUtil.findWidget(subElement, Day.class); if (day != null) { - Date date = day.getDate(); - int id = date.getDate(); + VaadinDateTime date = day.getDate(); + int id = date.getDay(); // Zero or negative ids map to days of the preceding month, // past-the-end-of-month ids to days of the following month if (date.getMonth() < displayedMonth.getMonth()) { @@ -2021,37 +2020,10 @@ public abstract class VAbstractCalendarPanel<R extends Enum<R>> * is defined as a live area. That way the screen reader recognizes the * change and reads it to the user. */ - public class FocusedDate extends Date { - - public FocusedDate(int year, int month, int date) { - super(year, month, date); - } - - @Override - public void setTime(long time) { - super.setTime(time); - setLabel(); - } - - @Override - @Deprecated - public void setDate(int date) { - super.setDate(date); - setLabel(); - } - - @Override - @Deprecated - public void setMonth(int month) { - super.setMonth(month); - setLabel(); - } + public class FocusedDate extends VaadinDateTime { - @Override - @Deprecated - public void setYear(int year) { - super.setYear(year); - setLabel(); + public FocusedDate(int year, int month, int day) { + super(year, month, day); } private void setLabel() { @@ -2070,7 +2042,7 @@ public abstract class VAbstractCalendarPanel<R extends Enum<R>> * @param newRangeStart * - the allowed range's start date */ - public void setRangeStart(Date newRangeStart) { + public void setRangeStart(VaadinDateTime newRangeStart) { if (!SharedUtil.equals(rangeStart, newRangeStart)) { rangeStart = newRangeStart; if (initialRenderDone) { @@ -2088,7 +2060,7 @@ public abstract class VAbstractCalendarPanel<R extends Enum<R>> * @param newRangeEnd * - the allowed range's end date */ - public void setRangeEnd(Date newRangeEnd) { + public void setRangeEnd(VaadinDateTime newRangeEnd) { if (!SharedUtil.equals(rangeEnd, newRangeEnd)) { rangeEnd = newRangeEnd; if (initialRenderDone) { diff --git a/client/src/main/java/com/vaadin/client/ui/VAbstractPopupCalendar.java b/client/src/main/java/com/vaadin/client/ui/VAbstractPopupCalendar.java index 4df4d0e325..e0e63ff7dc 100644 --- a/client/src/main/java/com/vaadin/client/ui/VAbstractPopupCalendar.java +++ b/client/src/main/java/com/vaadin/client/ui/VAbstractPopupCalendar.java @@ -17,6 +17,7 @@ package com.vaadin.client.ui; import java.util.Date; +import java.util.Objects; import java.util.logging.Logger; import com.google.gwt.aria.client.Id; @@ -46,6 +47,7 @@ import com.vaadin.client.BrowserInfo; import com.vaadin.client.ComputedStyle; import com.vaadin.client.ui.VAbstractCalendarPanel.SubmitListener; import com.vaadin.client.ui.aria.AriaHelper; +import com.vaadin.shared.data.date.VaadinDateTime; import com.vaadin.shared.ui.datefield.TextualDateFieldState; /** @@ -213,11 +215,11 @@ public abstract class VAbstractPopupCalendar<PANEL extends VAbstractCalendarPane * the new {@code Date} to update */ @SuppressWarnings("deprecation") - public void updateValue(Date newDate) { - Date currentDate = getCurrentDate(); + public void updateValue(VaadinDateTime newDate) { + VaadinDateTime currentDate = getCurrentDate(); R resolution = getCurrentResolution(); - if (currentDate == null || newDate.getTime() != currentDate.getTime()) { - setCurrentDate((Date) newDate.clone()); + if (!Objects.equals(currentDate,newDate)) { + setCurrentDate(newDate); bufferedResolutions.put(calendar.getResolution(calendar::isYear), newDate.getYear() + 1900); if (!calendar.isYear(resolution)) { @@ -227,7 +229,7 @@ public abstract class VAbstractPopupCalendar<PANEL extends VAbstractCalendarPane if (!calendar.isMonth(resolution)) { bufferedResolutions.put( calendar.getResolution(calendar::isDay), - newDate.getDate()); + newDate.getDay()); } } } @@ -381,11 +383,11 @@ public abstract class VAbstractPopupCalendar<PANEL extends VAbstractCalendarPane open = true; if (getCurrentDate() != null) { - calendar.setDate((Date) getCurrentDate().clone()); + calendar.setDate(getCurrentDate()); } else if (getDefaultDate() != null) { calendar.setDate(getDefaultDate()); } else { - calendar.setDate(new Date()); + calendar.setDate(VaadinDateTime.today()); } // clear previous values @@ -475,8 +477,9 @@ public abstract class VAbstractPopupCalendar<PANEL extends VAbstractCalendarPane * @param selectedDate * Date that is currently selected */ - public void setFocusedDate(Date selectedDate) { - this.selectedDate.setText(DateTimeFormat.getFormat("dd, MMMM, yyyy") + public void setFocusedDate(VaadinDateTime selectedDate) { + this.selectedDate.setText( + DateTimeFormat.getFormat("dd, MMMM, yyyy") .format(selectedDate)); } @@ -599,7 +602,7 @@ public abstract class VAbstractPopupCalendar<PANEL extends VAbstractCalendarPane * @param rangeStart * - the allowed range's start date */ - public void setRangeStart(Date rangeStart) { + public void setRangeStart(VaadinDateTime rangeStart) { calendar.setRangeStart(rangeStart); } @@ -610,7 +613,7 @@ public abstract class VAbstractPopupCalendar<PANEL extends VAbstractCalendarPane * @param rangeEnd * - the allowed range's end date */ - public void setRangeEnd(Date rangeEnd) { + public void setRangeEnd(VaadinDateTime rangeEnd) { calendar.setRangeEnd(rangeEnd); } diff --git a/client/src/main/java/com/vaadin/client/ui/VAbstractTextualDate.java b/client/src/main/java/com/vaadin/client/ui/VAbstractTextualDate.java index 409dc0ef7f..a346878707 100644 --- a/client/src/main/java/com/vaadin/client/ui/VAbstractTextualDate.java +++ b/client/src/main/java/com/vaadin/client/ui/VAbstractTextualDate.java @@ -41,6 +41,7 @@ import com.vaadin.client.ui.aria.HandlesAriaCaption; import com.vaadin.client.ui.aria.HandlesAriaInvalid; import com.vaadin.client.ui.aria.HandlesAriaRequired; import com.vaadin.shared.EventId; +import com.vaadin.shared.data.date.VaadinDateTime; /** * Abstract textual date field base implementation. Provides a text box as an @@ -184,7 +185,7 @@ public abstract class VAbstractTextualDate<R extends Enum<R>> removeStyleName(getStylePrimaryName() + PARSE_ERROR_CLASSNAME); // Create the initial text for the textfield String dateText; - Date currentDate = getDate(); + VaadinDateTime currentDate = getDate(); // Always call this to ensure the format ends up in the element String formatString = getFormatString(); if (currentDate != null) { @@ -301,11 +302,11 @@ public abstract class VAbstractTextualDate<R extends Enum<R>> * @since 8.2 */ protected void updateBufferedResolutions() { - Date currentDate = getDate(); + VaadinDateTime currentDate = getDate(); if (currentDate != null) { bufferedResolutions.put( getResolutions().filter(this::isYear).findFirst().get(), - currentDate.getYear() + 1900); + currentDate.getYear()); } } diff --git a/client/src/main/java/com/vaadin/client/ui/VDateField.java b/client/src/main/java/com/vaadin/client/ui/VDateField.java index 21ad59b4ba..39996ec521 100644 --- a/client/src/main/java/com/vaadin/client/ui/VDateField.java +++ b/client/src/main/java/com/vaadin/client/ui/VDateField.java @@ -28,6 +28,7 @@ import com.google.gwt.user.client.ui.HasEnabled; import com.vaadin.client.ApplicationConnection; import com.vaadin.client.DateTimeService; import com.vaadin.client.ui.datefield.AbstractDateFieldConnector; +import com.vaadin.shared.data.date.VaadinDateTime; import com.vaadin.shared.ui.datefield.AbstractDateFieldServerRpc; /** @@ -88,13 +89,13 @@ public abstract class VDateField<R extends Enum<R>> extends FlowPanel * The date that is displayed the date field before a value is selected. If * null, display the current date. */ - private Date defaultDate; + private VaadinDateTime defaultDate; /** * The date that is selected in the date field. Null if an invalid date is * specified. */ - private Date date; + private VaadinDateTime date; /** For internal use only. May be removed or replaced in the future. */ public DateTimeService dts; @@ -123,11 +124,11 @@ public abstract class VDateField<R extends Enum<R>> extends FlowPanel this.currentLocale = currentLocale; } - public Date getCurrentDate() { + public VaadinDateTime getCurrentDate() { return date; } - public void setCurrentDate(Date date) { + public void setCurrentDate(VaadinDateTime date) { this.date = date; } @@ -139,7 +140,7 @@ public abstract class VDateField<R extends Enum<R>> extends FlowPanel * opening a popup with no value selected * @since 8.1.2 */ - public void setDefaultDate(Date date) { + public void setDefaultDate(VaadinDateTime date) { this.defaultDate = date; } @@ -148,7 +149,7 @@ public abstract class VDateField<R extends Enum<R>> extends FlowPanel * <p> * The map contains integer representation of values per resolution. The * method should construct a date based on the map and set it via - * {@link #setCurrentDate(Date)} + * {@link #setCurrentDate(VaadinDateTime)} * * @param dateValues * a map with date values to convert into a date @@ -174,7 +175,7 @@ public abstract class VDateField<R extends Enum<R>> extends FlowPanel * @return the default date * @since 8.1.2 */ - public Date getDefaultDate() { + public VaadinDateTime getDefaultDate() { return defaultDate; } @@ -225,19 +226,19 @@ public abstract class VDateField<R extends Enum<R>> extends FlowPanel /** * Returns a copy of the current date. Modifying the returned date will not - * modify the value of this VDateField. Use {@link #setDate(Date)} to change + * modify the value of this VDateField. Use {@link #setDate(VaadinDateTime)} to change * the current date. * <p> * For internal use only. May be removed or replaced in the future. * * @return A copy of the current date */ - public Date getDate() { - Date current = getCurrentDate(); + public VaadinDateTime getDate() { + VaadinDateTime current = getCurrentDate(); if (current == null) { return null; } else { - return (Date) getCurrentDate().clone(); + return getCurrentDate(); } } @@ -247,7 +248,7 @@ public abstract class VDateField<R extends Enum<R>> extends FlowPanel * @param date * The new date to use */ - protected void setDate(Date date) { + protected void setDate(VaadinDateTime date) { this.date = date; } @@ -341,7 +342,7 @@ public abstract class VDateField<R extends Enum<R>> extends FlowPanel * a map with date values to convert into a date * @return the date based on the dateValues map */ - protected abstract Date getDate(Map<R, Integer> dateValues); + protected abstract VaadinDateTime getDate(Map<R, Integer> dateValues); /** * Returns all available resolutions as an array. diff --git a/client/src/main/java/com/vaadin/client/ui/VDateFieldCalendar.java b/client/src/main/java/com/vaadin/client/ui/VDateFieldCalendar.java index 42e8f9dc97..3e12e97216 100644 --- a/client/src/main/java/com/vaadin/client/ui/VDateFieldCalendar.java +++ b/client/src/main/java/com/vaadin/client/ui/VDateFieldCalendar.java @@ -21,8 +21,10 @@ import static com.vaadin.shared.ui.datefield.DateResolution.YEAR; import java.util.Date; import java.util.Map; +import java.util.Objects; import com.google.gwt.core.shared.GWT; +import com.vaadin.shared.data.date.VaadinDateTime; import com.vaadin.shared.ui.datefield.DateResolution; /** @@ -46,11 +48,11 @@ public class VDateFieldCalendar return; } - Date date2 = calendarPanel.getDate(); - Date currentDate = getCurrentDate(); + VaadinDateTime date2 = calendarPanel.getDate(); + VaadinDateTime currentDate = getCurrentDate(); DateResolution resolution = getCurrentResolution(); - if (currentDate == null || date2.getTime() != currentDate.getTime()) { - setCurrentDate((Date) date2.clone()); + if (!Objects.equals(currentDate ,date2)) { + setCurrentDate(date2); bufferedResolutions.put(YEAR, // Java Date uses the year aligned to 1900 (no to zero). // So we should add 1900 to get a correct year aligned to 0. @@ -58,7 +60,7 @@ public class VDateFieldCalendar if (resolution.compareTo(YEAR) < 0) { bufferedResolutions.put(MONTH, date2.getMonth() + 1); if (resolution.compareTo(MONTH) < 0) { - bufferedResolutions.put(DAY, date2.getDate()); + bufferedResolutions.put(DAY, date2.getDay()); } } } @@ -98,7 +100,7 @@ public class VDateFieldCalendar } @Override - protected Date getDate(Map<DateResolution, Integer> dateVaules) { + protected VaadinDateTime getDate(Map<DateResolution, Integer> dateVaules) { return VPopupCalendar.makeDate(dateVaules); } diff --git a/client/src/main/java/com/vaadin/client/ui/VDateTimeCalendarPanel.java b/client/src/main/java/com/vaadin/client/ui/VDateTimeCalendarPanel.java index 5e9732e091..31e5c78c0b 100644 --- a/client/src/main/java/com/vaadin/client/ui/VDateTimeCalendarPanel.java +++ b/client/src/main/java/com/vaadin/client/ui/VDateTimeCalendarPanel.java @@ -25,6 +25,7 @@ import com.google.gwt.user.client.ui.FlowPanel; import com.google.gwt.user.client.ui.ListBox; import com.google.gwt.user.client.ui.Widget; import com.vaadin.client.DateTimeService; +import com.vaadin.shared.data.date.VaadinDateTime; import com.vaadin.shared.ui.datefield.DateTimeResolution; /** @@ -79,8 +80,6 @@ public class VDateTimeCalendarPanel /** * Constructs the ListBoxes and updates their value * - * @param redraw - * Should new instances of the listboxes be created */ private void buildTime() { clear(); @@ -125,7 +124,7 @@ public class VDateTimeCalendarPanel if (isReadonly()) { int h = 0; if (getDate() != null) { - h = getDate().getHours(); + h = getDate().getHour(); } if (getDateTimeService().isTwelveHourClock()) { h -= h < 12 ? 0 : 12; @@ -161,7 +160,7 @@ public class VDateTimeCalendarPanel if (isReadonly()) { int i = 0; if (getDate() != null) { - i = (getDate().getHours() < 12) ? 0 : 1; + i = (getDate().getHour() < 12) ? 0 : 1; } add(new VLabel(ampm.getItemText(i))); } else { @@ -205,24 +204,24 @@ public class VDateTimeCalendarPanel */ public void updateTimes() { if (getDate() == null) { - setDate(new Date()); + setDate(VaadinDateTime.now()); } if (getDateTimeService().isTwelveHourClock()) { - int h = getDate().getHours(); + int h = getDate().getHour(); ampm.setSelectedIndex(h < 12 ? 0 : 1); h -= ampm.getSelectedIndex() * 12; hours.setSelectedIndex(h); } else { - hours.setSelectedIndex(getDate().getHours()); + hours.setSelectedIndex(getDate().getHour()); } if (getResolution().compareTo(DateTimeResolution.MINUTE) <= 0) { - mins.setSelectedIndex(getDate().getMinutes()); + mins.setSelectedIndex(getDate().getMinute()); } if (getResolution().compareTo(DateTimeResolution.SECOND) <= 0) { - sec.setSelectedIndex(getDate().getSeconds()); + sec.setSelectedIndex(getDate().getSec()); } if (getDateTimeService().isTwelveHourClock()) { - ampm.setSelectedIndex(getDate().getHours() < 12 ? 0 : 1); + ampm.setSelectedIndex(getDate().getHour() < 12 ? 0 : 1); } hours.setEnabled(isEnabled()); @@ -265,11 +264,10 @@ public class VDateTimeCalendarPanel if (getDateTimeService().isTwelveHourClock()) { h = h + ampm.getSelectedIndex() * 12; } - getDate().setHours(h); + getDate().setHour(h); if (timeChangeListener != null) { - timeChangeListener.changed(h, getDate().getMinutes(), - getDate().getSeconds(), - DateTimeService.getMilliseconds(getDate())); + timeChangeListener.changed(h, getDate().getMinute(), + getDate().getSec(),0); } event.preventDefault(); event.stopPropagation(); @@ -277,9 +275,8 @@ public class VDateTimeCalendarPanel final int m = mins.getSelectedIndex(); getDate().setMinutes(m); if (timeChangeListener != null) { - timeChangeListener.changed(getDate().getHours(), m, - getDate().getSeconds(), - DateTimeService.getMilliseconds(getDate())); + timeChangeListener.changed(getDate().getHour(), m, + getDate().getSec(),0); } event.preventDefault(); event.stopPropagation(); @@ -287,9 +284,8 @@ public class VDateTimeCalendarPanel final int s = sec.getSelectedIndex(); getDate().setSeconds(s); if (timeChangeListener != null) { - timeChangeListener.changed(getDate().getHours(), - getDate().getMinutes(), s, - DateTimeService.getMilliseconds(getDate())); + timeChangeListener.changed(getDate().getHour(), + getDate().getMinute(), s,0); } event.preventDefault(); event.stopPropagation(); @@ -298,9 +294,8 @@ public class VDateTimeCalendarPanel + (ampm.getSelectedIndex() * 12); getDate().setHours(h); if (timeChangeListener != null) { - timeChangeListener.changed(h, getDate().getMinutes(), - getDate().getSeconds(), - DateTimeService.getMilliseconds(getDate())); + timeChangeListener.changed(h, getDate().getMinute(), + getDate().getSec(),0); } event.preventDefault(); event.stopPropagation(); @@ -330,7 +325,7 @@ public class VDateTimeCalendarPanel } @Override - public void setDate(Date currentDate) { + public void setDate(VaadinDateTime currentDate) { doSetDate(currentDate, isTimeSelectorNeeded() && time == null, () -> { if (isTimeSelectorNeeded()) { time.updateTimes(); diff --git a/client/src/main/java/com/vaadin/client/ui/VDateTimeFieldCalendar.java b/client/src/main/java/com/vaadin/client/ui/VDateTimeFieldCalendar.java index a6c6cc75be..5433cdc212 100644 --- a/client/src/main/java/com/vaadin/client/ui/VDateTimeFieldCalendar.java +++ b/client/src/main/java/com/vaadin/client/ui/VDateTimeFieldCalendar.java @@ -24,8 +24,10 @@ import static com.vaadin.shared.ui.datefield.DateTimeResolution.YEAR; import java.util.Date; import java.util.Map; +import java.util.Objects; import com.google.gwt.core.client.GWT; +import com.vaadin.shared.data.date.VaadinDateTime; import com.vaadin.shared.ui.datefield.DateTimeResolution; /** @@ -50,23 +52,24 @@ public class VDateTimeFieldCalendar extends return; } - Date date2 = calendarPanel.getDate(); - Date currentDate = getCurrentDate(); + VaadinDateTime date2 = calendarPanel.getDate(); + VaadinDateTime currentDate = getCurrentDate(); DateTimeResolution resolution = getCurrentResolution(); - if (currentDate == null || date2.getTime() != currentDate.getTime()) { - setCurrentDate((Date) date2.clone()); - bufferedResolutions.put(YEAR, date2.getYear() + 1900); + + if (!Objects.equals(currentDate,date2)) { + setCurrentDate(date2); + bufferedResolutions.put(YEAR, date2.getYear()); if (resolution.compareTo(YEAR) < 0) { bufferedResolutions.put(MONTH, date2.getMonth() + 1); if (resolution.compareTo(MONTH) < 0) { - bufferedResolutions.put(DAY, date2.getDate()); + bufferedResolutions.put(DAY, date2.getDay()); if (resolution.compareTo(DAY) < 0) { - bufferedResolutions.put(HOUR, date2.getHours()); + bufferedResolutions.put(HOUR, date2.getHour()); if (resolution.compareTo(HOUR) < 0) { - bufferedResolutions.put(MINUTE, date2.getMinutes()); + bufferedResolutions.put(MINUTE, date2.getMinute()); if (resolution.compareTo(MINUTE) < 0) { bufferedResolutions.put(SECOND, - date2.getSeconds()); + date2.getSec()); } } } @@ -97,7 +100,7 @@ public class VDateTimeFieldCalendar extends } @Override - protected Date getDate(Map<DateTimeResolution, Integer> dateValues) { + protected VaadinDateTime getDate(Map<DateTimeResolution, Integer> dateValues) { return VPopupTimeCalendar.makeDate(dateValues); } diff --git a/client/src/main/java/com/vaadin/client/ui/VPopupCalendar.java b/client/src/main/java/com/vaadin/client/ui/VPopupCalendar.java index 970ccfb71a..8aa8e81a0a 100644 --- a/client/src/main/java/com/vaadin/client/ui/VPopupCalendar.java +++ b/client/src/main/java/com/vaadin/client/ui/VPopupCalendar.java @@ -19,10 +19,10 @@ import static com.vaadin.shared.ui.datefield.DateResolution.DAY; import static com.vaadin.shared.ui.datefield.DateResolution.MONTH; import static com.vaadin.shared.ui.datefield.DateResolution.YEAR; -import java.util.Date; import java.util.Map; import com.google.gwt.core.client.GWT; +import com.vaadin.shared.data.date.VaadinDateTime; import com.vaadin.shared.ui.datefield.DateResolution; /** @@ -54,24 +54,14 @@ public class VPopupCalendar super.setCurrentResolution(resolution == null ? YEAR : resolution); } - public static Date makeDate(Map<DateResolution, Integer> dateValues) { + public static VaadinDateTime makeDate(Map<DateResolution, Integer> dateValues) { if (dateValues.get(YEAR) == null) { return null; } - Date date = new Date(2000 - 1900, 0, 1); - Integer year = dateValues.get(YEAR); - if (year != null) { - date.setYear(year - 1900); - } - Integer month = dateValues.get(MONTH); - if (month != null) { - date.setMonth(month - 1); - } - Integer day = dateValues.get(DAY); - if (day != null) { - date.setDate(day); - } - return date; + int year = dateValues.getOrDefault(YEAR,2000); + int month = dateValues.getOrDefault(MONTH,0); + int day = dateValues.getOrDefault(DAY,1); + return new VaadinDateTime(year, month, day); } @Override @@ -80,21 +70,21 @@ public class VPopupCalendar } @Override - protected Date getDate(Map<DateResolution, Integer> dateValues) { + protected VaadinDateTime getDate(Map<DateResolution, Integer> dateValues) { return makeDate(dateValues); } @Override protected void updateBufferedResolutions() { super.updateBufferedResolutions(); - Date currentDate = getDate(); + VaadinDateTime currentDate = getDate(); if (currentDate != null) { DateResolution resolution = getCurrentResolution(); if (resolution.compareTo(MONTH) <= 0) { bufferedResolutions.put(MONTH, currentDate.getMonth() + 1); } if (resolution.compareTo(DAY) <= 0) { - bufferedResolutions.put(DAY, currentDate.getDate()); + bufferedResolutions.put(DAY, currentDate.getDay()); } } } diff --git a/client/src/main/java/com/vaadin/client/ui/VPopupTimeCalendar.java b/client/src/main/java/com/vaadin/client/ui/VPopupTimeCalendar.java index 46889da0e2..fa71041d4e 100644 --- a/client/src/main/java/com/vaadin/client/ui/VPopupTimeCalendar.java +++ b/client/src/main/java/com/vaadin/client/ui/VPopupTimeCalendar.java @@ -22,14 +22,15 @@ import static com.vaadin.shared.ui.datefield.DateTimeResolution.MONTH; import static com.vaadin.shared.ui.datefield.DateTimeResolution.SECOND; import static com.vaadin.shared.ui.datefield.DateTimeResolution.YEAR; -import java.util.Date; import java.util.Map; +import java.util.Objects; import java.util.logging.Level; import java.util.logging.Logger; import com.google.gwt.core.client.GWT; import com.vaadin.client.LocaleNotLoadedException; import com.vaadin.client.LocaleService; +import com.vaadin.shared.data.date.VaadinDateTime; import com.vaadin.shared.ui.datefield.DateTimeResolution; /** @@ -65,36 +66,17 @@ public class VPopupTimeCalendar extends super.setCurrentResolution(resolution == null ? MINUTE : resolution); } - public static Date makeDate(Map<DateTimeResolution, Integer> dateValues) { + public static VaadinDateTime makeDate(Map<DateTimeResolution, Integer> dateValues) { if (dateValues.get(YEAR) == null) { return null; } - Date date = new Date(2000 - 1900, 0, 1); - Integer year = dateValues.get(YEAR); - if (year != null) { - date.setYear(year - 1900); - } - Integer month = dateValues.get(MONTH); - if (month != null) { - date.setMonth(month - 1); - } - Integer day = dateValues.get(DAY); - if (day != null) { - date.setDate(day); - } - Integer hour = dateValues.get(HOUR); - if (hour != null) { - date.setHours(hour); - } - Integer minute = dateValues.get(MINUTE); - if (minute != null) { - date.setMinutes(minute); - } - Integer second = dateValues.get(SECOND); - if (second != null) { - date.setSeconds(second); - } - return date; + int year = dateValues.getOrDefault(DateTimeResolution.YEAR,2000); + int month = dateValues.getOrDefault(DateTimeResolution.MONTH,0); + int day = dateValues.getOrDefault(DateTimeResolution.DAY,1); + int hour = dateValues.getOrDefault(DateTimeResolution.HOUR,0); + int minute = dateValues.getOrDefault(DateTimeResolution.MINUTE,0); + int second = dateValues.getOrDefault(DateTimeResolution.SECOND,0); + return new VaadinDateTime(year, month, day,hour,minute,second); } @Override @@ -103,47 +85,47 @@ public class VPopupTimeCalendar extends } @Override - protected Date getDate(Map<DateTimeResolution, Integer> dateValues) { + protected VaadinDateTime getDate(Map<DateTimeResolution, Integer> dateValues) { return makeDate(dateValues); } @Override protected void updateBufferedResolutions() { super.updateBufferedResolutions(); - Date currentDate = getDate(); + VaadinDateTime currentDate = getDate(); if (currentDate != null) { DateTimeResolution resolution = getCurrentResolution(); if (resolution.compareTo(MONTH) <= 0) { bufferedResolutions.put(MONTH, currentDate.getMonth() + 1); } if (resolution.compareTo(DAY) <= 0) { - bufferedResolutions.put(DAY, currentDate.getDate()); + bufferedResolutions.put(DAY, currentDate.getDay()); } if (resolution.compareTo(HOUR) <= 0) { - bufferedResolutions.put(HOUR, currentDate.getHours()); + bufferedResolutions.put(HOUR, currentDate.getHour()); } if (resolution.compareTo(MINUTE) <= 0) { - bufferedResolutions.put(MINUTE, currentDate.getMinutes()); + bufferedResolutions.put(MINUTE, currentDate.getMinute()); } if (resolution.compareTo(SECOND) <= 0) { - bufferedResolutions.put(SECOND, currentDate.getSeconds()); + bufferedResolutions.put(SECOND, currentDate.getSec()); } } } @Override @SuppressWarnings("deprecation") - public void updateValue(Date newDate) { - Date currentDate = getCurrentDate(); + public void updateValue(VaadinDateTime newDate) { + VaadinDateTime currentDate = getCurrentDate(); super.updateValue(newDate); DateTimeResolution resolution = getCurrentResolution(); - if (currentDate == null || newDate.getTime() != currentDate.getTime()) { + if(!Objects.equals(currentDate,newDate)) { if (resolution.compareTo(DAY) < 0) { - bufferedResolutions.put(HOUR, newDate.getHours()); + bufferedResolutions.put(HOUR, newDate.getHour()); if (resolution.compareTo(HOUR) < 0) { - bufferedResolutions.put(MINUTE, newDate.getMinutes()); + bufferedResolutions.put(MINUTE, newDate.getMinute()); if (resolution.compareTo(MINUTE) < 0) { - bufferedResolutions.put(SECOND, newDate.getSeconds()); + bufferedResolutions.put(SECOND, newDate.getSec()); } } } diff --git a/client/src/main/java/com/vaadin/client/ui/datefield/AbstractInlineDateFieldConnector.java b/client/src/main/java/com/vaadin/client/ui/datefield/AbstractInlineDateFieldConnector.java index 51852de708..bd7e73613c 100644 --- a/client/src/main/java/com/vaadin/client/ui/datefield/AbstractInlineDateFieldConnector.java +++ b/client/src/main/java/com/vaadin/client/ui/datefield/AbstractInlineDateFieldConnector.java @@ -23,6 +23,7 @@ import com.vaadin.client.annotations.OnStateChange; import com.vaadin.client.communication.StateChangeEvent; import com.vaadin.client.ui.VAbstractCalendarPanel; import com.vaadin.client.ui.VAbstractDateFieldCalendar; +import com.vaadin.shared.data.date.VaadinDateTime; import com.vaadin.shared.ui.datefield.InlineDateFieldState; /** @@ -53,16 +54,16 @@ public abstract class AbstractInlineDateFieldConnector<PANEL extends VAbstractCa if (isResolutionMonthOrHigher()) { widget.calendarPanel .setFocusChangeListener(date -> { - Date date2 = new Date(); + VaadinDateTime date2; if (widget.calendarPanel.getDate() != null) { - date2.setTime(widget.calendarPanel.getDate() - .getTime()); + date2 = widget.calendarPanel.getDate(); + } else { + date2 = VaadinDateTime.today(); } /* * Update the value of calendarPanel */ - date2.setYear(date.getYear()); - date2.setMonth(date.getMonth()); + date2 = new VaadinDateTime(date.getYear(),date.getYear(),date2.getDay()); widget.calendarPanel.setDate(date2); /* * Then update the value from panel to server @@ -86,9 +87,9 @@ public abstract class AbstractInlineDateFieldConnector<PANEL extends VAbstractCa .setShowISOWeekNumbers(widget.isShowISOWeekNumbers()); widget.calendarPanel.setDateTimeService(widget.getDateTimeService()); widget.calendarPanel.setResolution(widget.getCurrentResolution()); - Date currentDate = widget.getCurrentDate(); + VaadinDateTime currentDate = widget.getCurrentDate(); if (currentDate != null) { - widget.calendarPanel.setDate(new Date(currentDate.getTime())); + widget.calendarPanel.setDate(currentDate); } else { widget.calendarPanel.setDate(null); } diff --git a/client/src/main/java/com/vaadin/client/ui/datefield/DateTimeFieldConnector.java b/client/src/main/java/com/vaadin/client/ui/datefield/DateTimeFieldConnector.java index 9c3023a8da..cc87e62f9c 100644 --- a/client/src/main/java/com/vaadin/client/ui/datefield/DateTimeFieldConnector.java +++ b/client/src/main/java/com/vaadin/client/ui/datefield/DateTimeFieldConnector.java @@ -20,6 +20,7 @@ import java.util.Date; import com.vaadin.client.DateTimeService; import com.vaadin.client.ui.VDateTimeCalendarPanel; import com.vaadin.client.ui.VPopupTimeCalendar; +import com.vaadin.shared.data.date.VaadinDateTime; import com.vaadin.shared.ui.Connect; import com.vaadin.shared.ui.datefield.DateTimeResolution; import com.vaadin.shared.ui.datefield.LocalDateTimeFieldState; @@ -59,7 +60,7 @@ public class DateTimeFieldConnector extends .compareTo(DateTimeResolution.DAY) < 0) { widget.calendar .setTimeChangeListener((hour, min, sec, msec) -> { - Date d = widget.getDate(); + VaadinDateTime d = widget.getDate(); if (d == null) { // date currently null, use the value from // calendarPanel diff --git a/client/src/main/java/com/vaadin/client/ui/datefield/InlineDateTimeFieldConnector.java b/client/src/main/java/com/vaadin/client/ui/datefield/InlineDateTimeFieldConnector.java index 086363f344..6fd0bf1da4 100644 --- a/client/src/main/java/com/vaadin/client/ui/datefield/InlineDateTimeFieldConnector.java +++ b/client/src/main/java/com/vaadin/client/ui/datefield/InlineDateTimeFieldConnector.java @@ -20,6 +20,7 @@ import java.util.Date; import com.vaadin.client.DateTimeService; import com.vaadin.client.ui.VDateTimeCalendarPanel; import com.vaadin.client.ui.VDateTimeFieldCalendar; +import com.vaadin.shared.data.date.VaadinDateTime; import com.vaadin.shared.ui.Connect; import com.vaadin.shared.ui.datefield.DateTimeResolution; import com.vaadin.ui.InlineDateTimeField; @@ -52,21 +53,17 @@ public class InlineDateTimeFieldConnector extends .compareTo(DateTimeResolution.DAY) < 0) { getWidget().calendarPanel .setTimeChangeListener((hour, min, sec, msec) -> { - Date d = getWidget().getDate(); + VaadinDateTime d = getWidget().getDate(); if (d == null) { // date currently null, use the value from // calendarPanel // (~ client time at the init of the widget) - d = (Date) getWidget().calendarPanel.getDate() - .clone(); + d = getWidget().calendarPanel.getDate(); } - d.setHours(hour); - d.setMinutes(min); - d.setSeconds(sec); - DateTimeService.setMilliseconds(d, msec); - + VaadinDateTime newDateTime = new + VaadinDateTime(d,hour,min,sec); // Always update time changes to the server - getWidget().calendarPanel.setDate(d); + getWidget().calendarPanel.setDate(newDateTime); getWidget().updateValueFromPanel(); }); } diff --git a/client/src/main/java/com/vaadin/client/ui/datefield/TextualDateConnector.java b/client/src/main/java/com/vaadin/client/ui/datefield/TextualDateConnector.java index 0702211ccb..171dc9fbdb 100644 --- a/client/src/main/java/com/vaadin/client/ui/datefield/TextualDateConnector.java +++ b/client/src/main/java/com/vaadin/client/ui/datefield/TextualDateConnector.java @@ -28,6 +28,7 @@ import com.vaadin.client.communication.StateChangeEvent; import com.vaadin.client.ui.VAbstractCalendarPanel; import com.vaadin.client.ui.VAbstractCalendarPanel.FocusChangeListener; import com.vaadin.client.ui.VAbstractPopupCalendar; +import com.vaadin.shared.data.date.VaadinDateTime; import com.vaadin.shared.ui.datefield.TextualDateFieldState; /** @@ -85,9 +86,16 @@ public abstract class TextualDateConnector<PANEL extends VAbstractCalendarPanel< if (isResolutionMonthOrHigher()) { getWidget().updateValue(date); getWidget().buildDate(); - Date date2 = getWidget().calendar.getDate(); - date2.setYear(date.getYear()); - date2.setMonth(date.getMonth()); + VaadinDateTime calendarDate = getWidget().calendar.getDate(); + VaadinDateTime newDate = new VaadinDateTime(date.getYear(), date.getMonth() + , calendarDate.getDay() + , calendarDate.getHour() + , calendarDate.getMinute() + , calendarDate.getSec(), + calendarDate.resolution + ); + getWidget().calendar.setDate(newDate); + } }; } else { @@ -123,8 +131,8 @@ public abstract class TextualDateConnector<PANEL extends VAbstractCalendarPanel< super.onStateChanged(stateChangeEvent); getWidget().setTextFieldEnabled(getState().textFieldEnabled); - getWidget().setRangeStart(nullSafeDateClone(getState().rangeStart)); - getWidget().setRangeEnd(nullSafeDateClone(getState().rangeEnd)); + getWidget().setRangeStart(getState().rangeStart); + getWidget().setRangeEnd(getState().rangeEnd); getWidget().calendar.setDateStyles(getState().dateStyles); getWidget().calendar @@ -140,7 +148,7 @@ public abstract class TextualDateConnector<PANEL extends VAbstractCalendarPanel< && getWidget().getCurrentDate() != null) { hasSelectedDate = true; getWidget().calendar - .setDate((Date) getWidget().getCurrentDate().clone()); + .setDate(getWidget().getCurrentDate()); } // force re-render when changing resolution only getWidget().calendar.renderCalendar(hasSelectedDate); @@ -167,13 +175,6 @@ public abstract class TextualDateConnector<PANEL extends VAbstractCalendarPanel< getWidget().setTextFieldTabIndex(); } - private Date nullSafeDateClone(Date date) { - if (date != null) { - return (Date) date.clone(); - } - return null; - } - @Override protected void setWidgetStyleName(String styleName, boolean add) { super.setWidgetStyleName(styleName, add); diff --git a/server/src/main/java/com/vaadin/ui/AbstractDateField.java b/server/src/main/java/com/vaadin/ui/AbstractDateField.java index 3998afbae6..a8df21e324 100644 --- a/server/src/main/java/com/vaadin/ui/AbstractDateField.java +++ b/server/src/main/java/com/vaadin/ui/AbstractDateField.java @@ -53,6 +53,7 @@ import com.vaadin.event.FieldEvents.FocusNotifier; import com.vaadin.server.ErrorMessage; import com.vaadin.server.UserError; import com.vaadin.shared.Registration; +import com.vaadin.shared.data.date.VaadinDateTime; import com.vaadin.shared.ui.datefield.AbstractDateFieldServerRpc; import com.vaadin.shared.ui.datefield.AbstractDateFieldState; import com.vaadin.shared.ui.datefield.AbstractDateFieldState.AccessibleElement; @@ -269,7 +270,7 @@ public abstract class AbstractDateField<T extends Temporal & TemporalAdjuster & * - the allowed range's start date */ public void setRangeStart(T startDate) { - Date date = convertToDate(startDate); + VaadinDateTime date = convertToDate(startDate); if (date != null && getState().rangeEnd != null && date.after(getState().rangeEnd)) { throw new IllegalStateException( @@ -333,7 +334,7 @@ public abstract class AbstractDateField<T extends Temporal & TemporalAdjuster & * resolution) */ public void setRangeEnd(T endDate) { - Date date = convertToDate(endDate); + VaadinDateTime date = convertToDate(endDate); if (date != null && getState().rangeStart != null && getState().rangeStart.after(date)) { throw new IllegalStateException( @@ -760,18 +761,18 @@ public abstract class AbstractDateField<T extends Temporal & TemporalAdjuster & * a date to convert * @return object of type {@code T} representing the {@code date} */ - protected abstract T convertFromDate(Date date); + protected abstract T convertFromDate(VaadinDateTime date); /** - * Converts the object of type {@code T} to {@link Date}. + * Converts the object of type {@code T} to {@link VaadinDateTime}. * <p> - * This is the opposite to {@link #convertFromDate(Date)}. + * This is the opposite to {@link #convertFromDate(VaadinDateTime)}. * * @param date * the date of type {@code T} * @return converted date of type {@code Date} */ - protected abstract Date convertToDate(T date); + protected abstract VaadinDateTime convertToDate(T date); @SuppressWarnings("unchecked") private Stream<R> getResolutions() { diff --git a/server/src/main/java/com/vaadin/ui/AbstractLocalDateField.java b/server/src/main/java/com/vaadin/ui/AbstractLocalDateField.java index 84072288e5..c5936f100e 100644 --- a/server/src/main/java/com/vaadin/ui/AbstractLocalDateField.java +++ b/server/src/main/java/com/vaadin/ui/AbstractLocalDateField.java @@ -26,6 +26,7 @@ import java.util.Map; import com.vaadin.data.validator.DateRangeValidator; import com.vaadin.data.validator.RangeValidator; +import com.vaadin.shared.data.date.VaadinDateTime; import com.vaadin.shared.ui.datefield.AbstractTextualDateFieldState; import com.vaadin.shared.ui.datefield.DateResolution; @@ -113,20 +114,19 @@ public abstract class AbstractLocalDateField } @Override - protected LocalDate convertFromDate(Date date) { + protected LocalDate convertFromDate(VaadinDateTime date) { if (date == null) { return null; } - return Instant.ofEpochMilli(date.getTime()).atZone(ZoneOffset.UTC) - .toLocalDate(); + return LocalDate.of(date.getYear(),date.getMonth()+1,date.getDay()); } @Override - protected Date convertToDate(LocalDate date) { + protected VaadinDateTime convertToDate(LocalDate date) { if (date == null) { return null; } - return Date.from(date.atStartOfDay(ZoneOffset.UTC).toInstant()); + return new VaadinDateTime(date.getYear(),date.getMonthValue() - 1,date.getDayOfMonth()); } private LocalDate getDate(LocalDate date, DateResolution forResolution) { diff --git a/server/src/main/java/com/vaadin/ui/AbstractLocalDateTimeField.java b/server/src/main/java/com/vaadin/ui/AbstractLocalDateTimeField.java index adf6e0bc53..62bd5f5787 100644 --- a/server/src/main/java/com/vaadin/ui/AbstractLocalDateTimeField.java +++ b/server/src/main/java/com/vaadin/ui/AbstractLocalDateTimeField.java @@ -16,6 +16,7 @@ package com.vaadin.ui; import java.time.Instant; +import java.time.LocalDate; import java.time.LocalDateTime; import java.time.ZoneOffset; import java.time.format.DateTimeFormatter; @@ -27,6 +28,7 @@ import java.util.Map; import com.vaadin.data.validator.DateTimeRangeValidator; import com.vaadin.data.validator.RangeValidator; +import com.vaadin.shared.data.date.VaadinDateTime; import com.vaadin.shared.ui.datefield.AbstractTextualDateFieldState; import com.vaadin.shared.ui.datefield.DateTimeResolution; @@ -126,20 +128,19 @@ public abstract class AbstractLocalDateTimeField } @Override - protected LocalDateTime convertFromDate(Date date) { - if (date == null) { + protected LocalDateTime convertFromDate(VaadinDateTime dateTime) { + if (dateTime == null) { return null; } - return Instant.ofEpochMilli(date.getTime()).atZone(ZoneOffset.UTC) - .toLocalDateTime(); + return LocalDateTime.of(dateTime.getYear(),dateTime.getMonth() + 1 ,dateTime.getDay(),dateTime.getHour(),dateTime.getMinute(),dateTime.getSec()); } @Override - protected Date convertToDate(LocalDateTime date) { - if (date == null) { + protected VaadinDateTime convertToDate(LocalDateTime dateTime) { + if (dateTime == null) { return null; } - return Date.from(date.toInstant(ZoneOffset.UTC)); + return new VaadinDateTime(dateTime.getYear(),dateTime.getMonthValue() - 1 ,dateTime.getDayOfMonth(),dateTime.getHour(),dateTime.getMinute(),dateTime.getSecond()); } private LocalDateTime getDate(LocalDateTime date, diff --git a/server/src/test/java/com/vaadin/tests/server/component/datefield/DateFieldListenersTest.java b/server/src/test/java/com/vaadin/tests/server/component/datefield/DateFieldListenersTest.java index eea1e33137..8e5b914e86 100644 --- a/server/src/test/java/com/vaadin/tests/server/component/datefield/DateFieldListenersTest.java +++ b/server/src/test/java/com/vaadin/tests/server/component/datefield/DateFieldListenersTest.java @@ -11,6 +11,7 @@ import com.vaadin.event.FieldEvents.BlurEvent; import com.vaadin.event.FieldEvents.BlurListener; import com.vaadin.event.FieldEvents.FocusEvent; import com.vaadin.event.FieldEvents.FocusListener; +import com.vaadin.shared.data.date.VaadinDateTime; import com.vaadin.shared.ui.datefield.DateTimeResolution; import com.vaadin.tests.server.component.AbstractListenerMethodsTestBase; import com.vaadin.ui.AbstractDateField; @@ -42,12 +43,12 @@ public class DateFieldListenersTest extends AbstractListenerMethodsTestBase { } @Override - protected LocalDateTime convertFromDate(Date date) { + protected LocalDateTime convertFromDate(VaadinDateTime date) { return null; } @Override - protected Date convertToDate(LocalDateTime date) { + protected VaadinDateTime convertToDate(LocalDateTime date) { return null; } diff --git a/shared/src/main/java/com/vaadin/shared/data/date/VaadinDateTime.java b/shared/src/main/java/com/vaadin/shared/data/date/VaadinDateTime.java new file mode 100644 index 0000000000..8f3bdfa484 --- /dev/null +++ b/shared/src/main/java/com/vaadin/shared/data/date/VaadinDateTime.java @@ -0,0 +1,130 @@ +package com.vaadin.shared.data.date; + +import java.util.Calendar; +import java.util.GregorianCalendar; +import java.util.Objects; + +import com.vaadin.shared.ui.datefield.DateTimeResolution; + +public class VaadinDateTime { + public final int year; + public final int month; + public final int day; + public final int hour; + public final int minute; + public final int sec; + public final DateTimeResolution resolution; + + public VaadinDateTime(VaadinDateTime date, int hour, int minute, int second) { + this(date.getYear(), date.getMonth(),date.getDay(),hour, minute, second); + } + + public VaadinDateTime(int year, int month, int day, int hour, int minute, int sec) { + this(year, month, day, hour,minute,sec,DateTimeResolution.SECOND); + } + + public VaadinDateTime(int year, int month, int day) { + this(year, month, day, 0,0,0,DateTimeResolution.DAY); + } + + public VaadinDateTime(int year, int month, int day, int hour, int minute, int sec, + DateTimeResolution resolution) { + this.year = year; + this.month = month; + this.day = day; + this.hour = hour; + this.minute = minute; + this.sec = sec; + this.resolution = resolution; + + } + + public int getYear() { + return year; + } + + public int getMonth() { + return month; + } + + public int getDay() { + return day; + } + + public int getHour() { + return hour; + } + + public int getMinute() { + return minute; + } + + public int getSec() { + return sec; + } + + public DateTimeResolution getResolution() { + return resolution; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + VaadinDateTime that = (VaadinDateTime) o; + return year == that.year && + month == that.month && + day == that.day && + hour == that.hour && + minute == that.minute && + sec == that.sec && + resolution == that.resolution; + } + + @Override + public int hashCode() { + + return Objects.hash(year, month, day, hour, minute, sec, resolution); + } + + public static VaadinDateTime now() { + GregorianCalendar calendar = new GregorianCalendar(); + return new VaadinDateTime( + calendar.get(Calendar.YEAR), + calendar.get(Calendar.MONTH), + calendar.get(Calendar.DATE), + calendar.get(Calendar.HOUR_OF_DAY), + calendar.get(Calendar.MINUTE), + calendar.get(Calendar.SECOND) + ); + } + + public static VaadinDateTime today() { + GregorianCalendar calendar = new GregorianCalendar(); + return new VaadinDateTime( + calendar.get(Calendar.YEAR), + calendar.get(Calendar.MONTH), + calendar.get(Calendar.DATE) + ); + } + + public boolean after(VaadinDateTime rangeEnd) { + for(DateTimeResolution r = DateTimeResolution.YEAR; r.ordinal()<=resolution.ordinal();r = DateTimeResolution.values()[r.ordinal()-1]) + { + if(getField(r) > rangeEnd.getField(r)) return true; + } + return false; + } + + public int getField(DateTimeResolution r) { + switch (r) { + case YEAR: return getYear(); + case MONTH: return getMonth(); + case DAY: return getDay(); + case HOUR: return getHour(); + case MINUTE: return getMinute(); + case SECOND: return getSec(); + } + return 0; + } +} diff --git a/shared/src/main/java/com/vaadin/shared/ui/datefield/AbstractDateFieldState.java b/shared/src/main/java/com/vaadin/shared/ui/datefield/AbstractDateFieldState.java index 553e82a57e..bf77aad1e8 100644 --- a/shared/src/main/java/com/vaadin/shared/ui/datefield/AbstractDateFieldState.java +++ b/shared/src/main/java/com/vaadin/shared/ui/datefield/AbstractDateFieldState.java @@ -15,12 +15,12 @@ */ package com.vaadin.shared.ui.datefield; -import java.util.Date; import java.util.HashMap; import java.util.Map; import com.vaadin.shared.AbstractFieldState; import com.vaadin.shared.annotations.NoLayout; +import com.vaadin.shared.data.date.VaadinDateTime; /** * Shared state for the AbstractDateField component. @@ -52,14 +52,14 @@ public class AbstractDateFieldState extends AbstractFieldState { * date field. */ @NoLayout - public Date rangeStart; + public VaadinDateTime rangeStart; /** * End range that has been cleared, depending on the resolution of the date * field. */ @NoLayout - public Date rangeEnd; + public VaadinDateTime rangeEnd; /** * The JSON used to construct a TimeZone on the client side, can be |