Class BasicDurationFormatter

  • All Implemented Interfaces:
    DurationFormatter

    class BasicDurationFormatter
    extends java.lang.Object
    implements DurationFormatter
    Core implementation class for DurationFormatter.
    • Field Detail

      • fallbackLimit

        private long fallbackLimit
      • localeName

        private java.lang.String localeName
      • timeZone

        private java.util.TimeZone timeZone
    • Constructor Detail

      • BasicDurationFormatter

        public BasicDurationFormatter​(PeriodFormatter formatter,
                                      PeriodBuilder builder,
                                      DateFormatter fallback,
                                      long fallbackLimit)
        Creates a basic duration formatter with the given formatter, builder, and fallback. It's up to the caller to ensure that the locales and timezones of these are in sync.
      • BasicDurationFormatter

        protected BasicDurationFormatter​(PeriodFormatter formatter,
                                         PeriodBuilder builder,
                                         DateFormatter fallback,
                                         long fallbackLimit,
                                         java.lang.String localeName,
                                         java.util.TimeZone timeZone)
    • Method Detail

      • formatDurationFromNowTo

        public java.lang.String formatDurationFromNowTo​(java.util.Date targetDate)
        Description copied from interface: DurationFormatter
        Formats the duration between now and a target date.

        This is a convenience method that calls formatDurationFrom(long, long) using now as the reference date, and the difference between now and targetDate.getTime() as the duration.

        Specified by:
        formatDurationFromNowTo in interface DurationFormatter
        Parameters:
        targetDate - the ending date
        Returns:
        the formatted time
      • formatDurationFromNow

        public java.lang.String formatDurationFromNow​(long duration)
        Description copied from interface: DurationFormatter
        Formats a duration expressed in milliseconds.

        This is a convenience method that calls formatDurationFrom using the current system time as the reference date.

        Specified by:
        formatDurationFromNow in interface DurationFormatter
        Parameters:
        duration - the duration in milliseconds
        Returns:
        the formatted time
      • formatDurationFrom

        public java.lang.String formatDurationFrom​(long duration,
                                                   long referenceDate)
        Description copied from interface: DurationFormatter
        Formats a duration expressed in milliseconds from a reference date.

        The reference date allows formatters to use actual durations of variable-length periods (like months) if they wish.

        The duration is expressed as the number of milliseconds in the past (negative values) or future (positive values) with respect to a reference date (expressed as milliseconds in epoch).

        Specified by:
        formatDurationFrom in interface DurationFormatter
        Parameters:
        duration - the duration in milliseconds
        referenceDate - the date from which to compute the duration
        Returns:
        the formatted time
      • withLocale

        public DurationFormatter withLocale​(java.lang.String locName)
        Description copied from interface: DurationFormatter
        Returns a new DurationFormatter that's the same as this one but formats for a new locale.
        Specified by:
        withLocale in interface DurationFormatter
        Parameters:
        locName - the name of the new locale
        Returns:
        a new formatter for the given locale
      • withTimeZone

        public DurationFormatter withTimeZone​(java.util.TimeZone tz)
        Description copied from interface: DurationFormatter
        Returns a new DurationFormatter that's the same as this one but uses a different time zone.
        Specified by:
        withTimeZone in interface DurationFormatter
        Parameters:
        tz - the time zone in which to compute durations.
        Returns:
        a new formatter for the given locale
      • doFallback

        protected java.lang.String doFallback​(long duration,
                                              long referenceDate)
      • doBuild

        protected Period doBuild​(long duration,
                                 long referenceDate)
      • doFormat

        protected java.lang.String doFormat​(Period period)