Class BrowserDateUtils

java.lang.Object
com.vaadin.swingbridge.surrogates.util.BrowserDateUtils

public final class BrowserDateUtils extends Object
Browser-zone bridge between the legacy 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 Details

    • toLocalDate

      public static LocalDate toLocalDate(Date d)
      Convert a Date to a LocalDate via the browser's time zone — the inverse of toDate(LocalDate). The browser zone, not the server JVM's ZoneId.systemDefault(), is the one that matters: a Date the user picks via a DatePicker peer is interpreted in the browser's locale, and rendering it back must reverse that mapping.
    • toDate

      public static Date toDate(LocalDate ld)
      Convert a LocalDate to a Date at start-of-day in the browser's time zone — the inverse of toLocalDate(Date). Lossy for sub-day precision: any time-of-day collapses to 00:00 across a round-trip through LocalDate (the JSpinner / JFormattedTextField + DatePicker limitation per SD_sjspinner / SD_sjformatted_family).
    • dateOf

      public static Date dateOf(int year, int month, int day)
      Convenience for toDate(LocalDate.of(year, month, day)) — midnight on the given wall-date in the browser's time zone. month is 1-indexed (January = 1), matching LocalDate.of(int, int, int) and unlike the 0-indexed Calendar convention. Use at every field-form (y, m, d) → Date seam (SpinnerDateModel bounds, hardcoded seed dates) in place of the Swing-era cal.set(year, month - 1, day); cal.getTime(); idiom.
    • formatAsISODate

      public static String formatAsISODate(Date d)
      Format a Date as an ISO-8601 "yyyy-MM-dd" string (browser-zone wall-date) — the read-only-display replacement for the Swing-era new SimpleDateFormat("yyyy-MM-dd").format(date). Matches what a DatePicker peer renders for the same Date (both go through toLocalDate(Date)).
    • formatAsDate

      public static String formatAsDate(Date d, String pattern)
      Format a Date with a DateTimeFormatter pattern (browser-zone wall-date) — for when formatAsISODate(Date) isn't enough, e.g. a locale-friendly "dd MMM yyyy". The pattern goes straight to DateTimeFormatter.ofPattern(String), whose java.time grammar differs subtly from legacy SimpleDateFormat (e.g. uuuu vs yyyy for year-of-era); throws IllegalArgumentException if malformed.