Class BrowserDateUtils
Date world and
LocalDate. Every conversion here interprets a Date
against the browser's time zone (cached in BrowserTimeZone),
not the server JVM's ZoneId.systemDefault().
These are a compatibility bridge, not the preferred path. The
migration arc past the vaadinx.text.SimpleDateFormat /
vaadinx.util.Calendar emulators is for code to move toward
modern java.time (LocalDate / DateTimeFormatter)
directly; reach for these methods only where the app genuinely can't be
upgraded (spaghetti, old libraries, no in-house knowledge). Where you do use
them, use them at every Date↔wall-date seam so the whole
codebase shares one zone interpretation — the surrogate DatePicker peers use
these exact calls internally.
Shared contract of every method: returns null on null input, and
throws IllegalStateException if BrowserTimeZone.fetch() was not
called during UI init (R_match_swing_errors programming-error contract, SD_browser_timezone).
The arithmetic is a default GregorianCalendar's in
TimeZone.getTimeZone(BrowserTimeZone.get()) — what
SimpleDateFormat and Calendar run — so a picked date prints as
the date picked. date.toInstant().atZone(zoneId) is not
equivalent before 1900: ZoneId applies local mean time (Los Angeles
1867: -07:52:58), TimeZone today's offset (-08:00), and
midnight in one is the previous day in the other. Before 1582-10-15 the
LocalDate carries the Julian label SimpleDateFormat prints.
-
Method Summary
Modifier and TypeMethodDescriptionstatic DatedateOf(int year, int month, int day) Convenience fortoDate(LocalDate.of(year, month, day))— midnight on the given wall-date in the browser's time zone.static StringformatAsDate(Date d, String pattern) Format aDatewith aDateTimeFormatterpattern (browser-zone wall-date) — for whenformatAsISODate(Date)isn't enough, e.g. a locale-friendly"dd MMM yyyy".static StringFormat aDateas an ISO-8601"yyyy-MM-dd"string (browser-zone wall-date) — the read-only-display replacement for the Swing-eranew SimpleDateFormat("yyyy-MM-dd").format(date).static DateConvert aLocalDateto aDateat start-of-day in the browser's time zone — the inverse oftoLocalDate(Date).static LocalDatetoLocalDate(Date d)
-
Method Details
-
toLocalDate
Convert aDateto aLocalDatevia the browser's time zone — the inverse oftoDate(LocalDate). The browser zone, not the server JVM'sZoneId.systemDefault(), is the one that matters: aDatethe user picks via a DatePicker peer is interpreted in the browser's locale, and rendering it back must reverse that mapping. -
toDate
Convert aLocalDateto aDateat start-of-day in the browser's time zone — the inverse oftoLocalDate(Date). Lossy for sub-day precision: any time-of-day collapses to 00:00 across a round-trip throughLocalDate(the JSpinner / JFormattedTextField + DatePicker limitation per SD_sjspinner / SD_sjformatted_family). -
dateOf
Convenience fortoDate(LocalDate.of(year, month, day))— midnight on the given wall-date in the browser's time zone.monthis 1-indexed (January = 1), matchingLocalDate.of(int, int, int)and unlike the 0-indexedCalendarconvention. Use at every field-form(y, m, d) → Dateseam (SpinnerDateModel bounds, hardcoded seed dates) in place of the Swing-eracal.set(year, month - 1, day); cal.getTime();idiom. -
formatAsISODate
Format aDateas an ISO-8601"yyyy-MM-dd"string (browser-zone wall-date) — the read-only-display replacement for the Swing-eranew SimpleDateFormat("yyyy-MM-dd").format(date). Matches what a DatePicker peer renders for the sameDate(both go throughtoLocalDate(Date)). -
formatAsDate
Format aDatewith aDateTimeFormatterpattern (browser-zone wall-date) — for whenformatAsISODate(Date)isn't enough, e.g. a locale-friendly"dd MMM yyyy". The pattern goes straight toDateTimeFormatter.ofPattern(String), whosejava.timegrammar differs subtly from legacySimpleDateFormat(e.g.uuuuvsyyyyfor year-of-era); throwsIllegalArgumentExceptionif malformed.
-