Class RelativeTime
- All Implemented Interfaces:
AttachNotifier,DetachNotifier,HasElement,HasStyle,Serializable
Thin wrapper around the @github/relative-time-element
custom element. The Java setters write the corresponding attributes on the element and the
browser does all the formatting and updating work.
Client-side rendering. The rendered text reflects the viewer's clock and locale, not the server's. There is no server-side API to read the displayed string; the string lives only in the DOM.
Attributes, not properties. Setters write HTML attributes
(setAttribute), not DOM properties, because the upstream element is
attribute-driven and its kebab-case attribute names match the upstream docs
one-to-one. setProperty would force diverging camelCase names.
LocalDateTime and LocalDate inputs are interpreted in the server's
default zone before being serialised as an instant.
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionCreates an empty component.RelativeTime(Instant datetime) Creates a component bound to the given instant.RelativeTime(LocalDate date) Creates a component bound to midnight of the given date, in the server'sdefault zone.RelativeTime(LocalDateTime datetime) Creates a component bound to the given local date-time, interpreted in the server'sdefault zone.RelativeTime(OffsetDateTime datetime) Creates a component bound to the given offset date-time.RelativeTime(ZonedDateTime datetime) Creates a component bound to the given zoned date-time. -
Method Summary
Modifier and TypeMethodDescriptionclear()Clears the target datetime: removes thedatetimeattribute and the component renders as empty.Returns the last instant applied via any of thesetDateTimeoverloads, normalised to UTC.setDateTime(Instant datetime) Sets the target instant.setDateTime(LocalDate date) Sets the target to midnight of the given date, in the server'sdefault zone.setDateTime(LocalDateTime datetime) Sets the target as aLocalDateTime, interpreted in the server'sdefault zone.setDateTime(OffsetDateTime datetime) Sets the target as anOffsetDateTime.setDateTime(ZonedDateTime datetime) Sets the target as aZonedDateTime.setDay(DateTimePartStyle style) Sets thedayattribute.setFormatStyle(FormatStyle style) Sets theformat-styleattribute.setHour(DateTimePartStyle style) Sets thehourattribute.Sets thelangattribute from aLocale(rendered as a BCP 47 language tag).setMinute(DateTimePartStyle style) Sets theminuteattribute.setMonth(DateTimePartStyle style) Sets themonthattribute.setNoTitle(boolean noTitle) Whentrue, adds theno-titleattribute, suppressing the automatic absolute-date tooltip.setPrecision(Precision precision) Sets theprecisionattribute (defaultsecond).Sets theprefixattribute (default"on"), the word prepended to absolute dates when the relative-format threshold is crossed.setSecond(DateTimePartStyle style) Sets thesecondattribute.Sets thetenseattribute (defaultauto).setThreshold(Duration threshold) Sets thethresholdattribute as an ISO-8601 duration (P30D,PT24H).setTimeZone(ZoneId timeZone) Sets thetime-zoneattribute from aZoneId(e.g.setTimeZoneName(TimeZoneName value) Sets thetime-zone-nameattribute, controlling whether and how the time-zone name is appended in absolute-date output.setWeekday(DateTimePartStyle style) Sets theweekdayattribute.setYear(DateTimePartStyle style) Sets theyearattribute.Methods inherited from class com.vaadin.flow.component.Component
addListener, findAncestor, fireEvent, from, get, getChildren, getElement, getEventBus, getId, getListeners, getLocale, getParent, getTranslation, getTranslation, getTranslation, getTranslation, getTranslation, getTranslation, getUI, hasListener, isAttached, isTemplateMapped, isVisible, onAttach, onDetach, onEnabledStateChanged, removeFromParent, scrollIntoView, scrollIntoView, set, setElement, setId, setVisibleMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.vaadin.flow.component.AttachNotifier
addAttachListenerMethods inherited from interface com.vaadin.flow.component.DetachNotifier
addDetachListenerMethods inherited from interface com.vaadin.flow.component.HasStyle
addClassName, addClassNames, getClassName, getClassNames, getStyle, hasClassName, removeClassName, removeClassNames, setClassName, setClassName
-
Constructor Details
-
RelativeTime
public RelativeTime()Creates an empty component.setDateTime(java.time.Instant)can be called later. -
RelativeTime
Creates a component bound to the given instant. -
RelativeTime
Creates a component bound to the given offset date-time. -
RelativeTime
Creates a component bound to the given zoned date-time. -
RelativeTime
Creates a component bound to the given local date-time, interpreted in the server'sdefault zone. -
RelativeTime
Creates a component bound to midnight of the given date, in the server'sdefault zone.
-
-
Method Details
-
setDateTime
Sets the target instant.nullclears thedatetimeattribute and the component renders as empty. -
setDateTime
Sets the target as anOffsetDateTime. The offset is applied viatoInstant(); the wire string is the resulting UTC instant (the offset itself is not retained). -
setDateTime
Sets the target as aZonedDateTime. The zone is applied viatoInstant(); the wire string is the resulting UTC instant (the zone itself is not retained). -
setDateTime
Sets the target as aLocalDateTime, interpreted in the server'sdefault zone. -
setDateTime
Sets the target to midnight of the given date, in the server'sdefault zone. -
clear
Clears the target datetime: removes thedatetimeattribute and the component renders as empty. Equivalent tosetDateTime((Instant) null)but without the cast that the overloadedsetDateTime(null)would otherwise require. -
getDateTime
Returns the last instant applied via any of thesetDateTimeoverloads, normalised to UTC. Returnsnullif no datetime has been set or it was cleared. -
setTense
Sets thetenseattribute (defaultauto). Ignored whenformatisFormat.DATETIME.nullremoves the attribute. -
setFormat
Sets theformatattribute (defaultauto, which is an alias forFormat.RELATIVE).nullremoves the attribute. -
setPrecision
Sets theprecisionattribute (defaultsecond). Ignored whenformatisFormat.DATETIME.nullremoves the attribute. -
setFormatStyle
Sets theformat-styleattribute. The default depends on theformatin effect:narrowforFormat.ELAPSED/Format.MICRO,shortforFormat.DATETIME,longforFormat.RELATIVE/Format.AUTO/Format.DURATION.nullremoves the attribute. -
setThreshold
Sets thethresholdattribute as an ISO-8601 duration (P30D,PT24H). Default isP30D. Once the gap to the target exceeds this duration the element switches from a relative phrase to an absolute date.nullremoves the attribute.Only consulted when
formatisFormat.AUTO/Format.RELATIVEANDtenseisTense.AUTO. Pairing this withsetTense(PAST)orsetTense(FUTURE)silently makes the threshold inert, because the element then commits to relative phrasing regardless of distance.- Throws:
IllegalArgumentException- ifthresholdis negative. Java serialises a negative duration asPT-nS, which the upstream element's duration parser rejects, silently reverting to the defaultP30D. Zero is allowed (it means "always absolute").
-
setPrefix
Sets theprefixattribute (default"on"), the word prepended to absolute dates when the relative-format threshold is crossed. Pass an empty string to drop the prefix entirely;nullremoves the attribute and restores the default. -
setNoTitle
Whentrue, adds theno-titleattribute, suppressing the automatic absolute-date tooltip.falseremoves it (the element's default behaviour shows the tooltip). -
setLocale
Sets thelangattribute from aLocale(rendered as a BCP 47 language tag). The browser usesIntl.RelativeTimeFormat/Intl.DateTimeFormatto localise the phrasing.nullremoves the attribute; when unset, the upstream walks DOM ancestors viaclosest('[lang]')and falls back to the document'slang, so an app-wide locale on<html lang="...">is picked up automatically.Note:
Locale.ROOT(andnew Locale("")) serialises tolang="und", which the browser treats as "undetermined" and silently resolves to the document/default locale, so passing the root locale looks like it was ignored. Passnullto clear instead. -
setTimeZone
Sets thetime-zoneattribute from aZoneId(e.g.America/New_York). When set, the element formats the date in this zone instead of the viewer's.nullremoves the attribute; when unset, the upstream walks DOM ancestors viaclosest('[time-zone]')and falls back to the document'stime-zoneattribute (and, absent that, the browser default).- Throws:
IllegalArgumentException- iftimeZoneis not an IANA region zone. Offset-based zones (aZoneOffsetsuch as+02:00orZ, or aGMT+02:00form) are written verbatim into the attribute and rejected by the browser'sIntl.DateTimeFormatwith aRangeError, which breaks rendering. The accepted set isZoneId.getAvailableZoneIds()(which includesUTC).
-
setTimeZoneName
Sets thetime-zone-nameattribute, controlling whether and how the time-zone name is appended in absolute-date output. Only relevant whenformatisFormat.DATETIME.nullremoves the attribute. -
setYear
Sets theyearattribute. AcceptsDateTimePartStyle.NUMERICorDateTimePartStyle.TWO_DIGIT. Only relevant whenformatisFormat.DATETIME.nullremoves the attribute. -
setMonth
Sets themonthattribute. AcceptsDateTimePartStyle.NUMERIC,DateTimePartStyle.TWO_DIGIT,DateTimePartStyle.NARROW,DateTimePartStyle.SHORT, orDateTimePartStyle.LONG. Only relevant whenformatisFormat.DATETIME.nullremoves the attribute. -
setDay
Sets thedayattribute. AcceptsDateTimePartStyle.NUMERICorDateTimePartStyle.TWO_DIGIT. Only relevant whenformatisFormat.DATETIME.nullremoves the attribute. -
setWeekday
Sets theweekdayattribute. AcceptsDateTimePartStyle.NARROW,DateTimePartStyle.SHORT, orDateTimePartStyle.LONG. Only relevant whenformatisFormat.DATETIME.nullremoves the attribute. -
setHour
Sets thehourattribute. AcceptsDateTimePartStyle.NUMERICorDateTimePartStyle.TWO_DIGIT. Only relevant whenformatisFormat.DATETIME.nullremoves the attribute. -
setMinute
Sets theminuteattribute. AcceptsDateTimePartStyle.NUMERICorDateTimePartStyle.TWO_DIGIT. Only relevant whenformatisFormat.DATETIME.nullremoves the attribute. -
setSecond
Sets thesecondattribute. AcceptsDateTimePartStyle.NUMERICorDateTimePartStyle.TWO_DIGIT. Only relevant whenformatisFormat.DATETIME.nullremoves the attribute.
-