B4A Library PrettyTime

This is a wrap for this github project.

PrettyTime
Author:
DonManfred (wrapper)
Version: 1
  • PrettyTime
    Methods:
    • Initialize (EventName As String)
    • IsInitialized As Boolean
    • approximateDuration (then As Date) As Duration
    • calculatePreciseDuration (then As Date) As List
      Calculate to the precision of the smallest provided {@link TimeUnit}, the exact {@link Duration} represented by
      the difference between the reference {@link Date}, and the given {@link Date}. If the given {@link Date} is
      <code>null</code>, the current value of {@link System#currentTimeMillis()} will be used instead.
      <p>
      <b>Note</b>: Precision may be lost if no supplied {@link TimeUnit} is granular enough to represent the remainder
      of time (in milliseconds).
      then: The {@link Date} to be compared against the reference timestamp, or <i>now</i> if no reference
      timestamp was provided
      Return type: @return:A sorted {@link List} of {@link Duration} objects, from largest to smallest. Each element in the list
      represents the approximate duration (number of times) that {@link TimeUnit} to fit into the previous
      element's delta. The first element is the largest {@link TimeUnit} to fit within the total difference
      between compared dates.
    • clearUnits As List
      Remove all registered {@link TimeUnit} instances. Returns all {@link TimeUnit} instances that were removed.
    • format (then As Date) As String
      Format the given {@link Date} object. If the given {@link Date} is <code>null</code>, the current value of
      {@link System#currentTimeMillis()} will be used instead.
      then: the {@link Date} to be formatted
      Return type: @return:A formatted string representing {@code then}
    • formatCal (then As Calendar) As String
      Format the given {@link Calendar} object. If the given {@link Calendar} is <code>null</code>, the current value of
      {@link System#currentTimeMillis()} will be used instead.
      then: the {@link Calendar} whose date is to be formatted
      Return type: @return:A formatted string representing {@code then}
    • formatDur (duration As Duration) As String
      Format the given {@link Duration} object, using the {@link TimeFormat} specified by the {@link TimeUnit} contained
      within. If the given {@link Duration} is <code>null</code>, the current value of
      {@link System#currentTimeMillis()} will be used instead.
      duration: the {@link Duration} to be formatted
      Return type: @return:A formatted string representing {@code duration}
    • formatDur2 (durations As List) As String
      Format the given {@link Duration} objects, using the {@link TimeFormat} specified by the {@link TimeUnit}
      contained within. Rounding rules are ignored for all but the last {@link Duration} element. If the given
      {@link Duration} {@link List} is <code>null</code> or empty, the current value of
      {@link System#currentTimeMillis()} will be used instead.
      durations: the {@link Duration}s to be formatted
      Return type: @return:A list of formatted strings representing {@code durations}
    • formatDuration (then As Date) As String
      Format the given {@link Date} and return a non-relative (not decorated with past or future tense) {@link String}
      for the approximate duration of its difference between the reference {@link Date}. If the given {@link Date} is
      <code>null</code>, the current value of {@link System#currentTimeMillis()} will be used instead.
      <p>
      then: the date to be formatted
      Return type: @return:A formatted string of the given {@link Date}
    • formatDuration2 (durations As List) As String
      Format the given {@link Duration} {@link List} and return a non-relative (not decorated with past or future tense)
      {@link String} for the approximate duration of its difference between the reference {@link Date}. If the given
      {@link Duration} is <code>null</code>, the current value of {@link System#currentTimeMillis()} will be used
      instead.
      duration: the duration to be formatted
      Return type: @return:A formatted string of the given {@link Duration}
    • formatDurationCal (then As Calendar) As String
      Format the given {@link Calendar} and return a non-relative (not decorated with past or future tense)
      {@link String} for the approximate duration of its difference between the reference {@link Date}. If the given
      {@link Calendar} is <code>null</code>, the current value of {@link System#currentTimeMillis()} will be used
      instead.
      <p>
      then: the date to be formatted
      Return type: @return:A formatted string of the given {@link Date}
    • formatDurationUnrounded (then As Date) As String
      Format the given {@link Date} and return a non-relative (not decorated with past or future tense) {@link String}
      for the approximate duration of its difference between the reference {@link Date}. Rounding rules are ignored. If
      the given {@link Date} is <code>null</code>, the current value of {@link System#currentTimeMillis()} will be used
      instead.
      <p>
      then: the date to be formatted
      Return type: @return:A formatted string of the given {@link Date}
    • formatDurationUnrounded2 (durations As List) As String
      Format the given {@link Duration} {@link List} and return a non-relative (not decorated with past or future tense)
      {@link String} for the approximate duration of its difference between the reference {@link Date}. Rounding rules
      are ignored. If the given {@link Duration} is <code>null</code>, the current value of
      {@link System#currentTimeMillis()} will be used instead.
      duration: the duration to be formatted
      Return type: @return:A formatted string of the given {@link Duration}
    • formatDurationUnroundedCal (then As Calendar) As String
      Format the given {@link Calendar} and return a non-relative (not decorated with past or future tense)
      {@link String} for the approximate duration of its difference between the reference {@link Date}. Rounding rules
      are ignored. If the given {@link Calendar} is <code>null</code>, the current value of
      {@link System#currentTimeMillis()} will be used instead.
      <p>
      then: the date to be formatted
      Return type: @return:A formatted string of the given {@link Date}
    • formatUnrounded (then As Date) As String
      Format the given {@link Date} object. Rounding rules are ignored. If the given {@link Date} is <code>null</code>,
      the current value of {@link System#currentTimeMillis()} will be used instead.
      then: the {@link Date} to be formatted
      Return type: @return:A formatted string representing {@code then}
    • formatUnrounded2 (durations As List) As String
      Format the given {@link Duration} objects, using the {@link TimeFormat} specified by the {@link TimeUnit}
      contained within. Rounding rules are ignored. If the given {@link Duration} {@link List} is <code>null</code> or
      empty, the current value of {@link System#currentTimeMillis()} will be used instead.
      durations: the {@link Duration}s to be formatted
      Return type: @return:A list of formatted strings representing {@code durations}
    • formatUnroundedCal (then As Calendar) As String
      Format the given {@link Calendar} object. This method applies the {@link PrettyTime#approximateDuration(Date)}
      method to perform its calculation. Rounding rules are ignored. If the given {@link Calendar} is <code>null</code>,
      the current value of {@link System#currentTimeMillis()} will be used instead.
      then: the {@link Calendar} whose date is to be formatted
      Return type: @return:A formatted string representing {@code then}
    • formatUnroundedDur (duration As Duration) As String
      Format the given {@link Duration} object, using the {@link TimeFormat} specified by the {@link TimeUnit} contained
      within. Rounding rules are ignored. If the given {@link Duration} is <code>null</code>, the current value of
      {@link System#currentTimeMillis()} will be used instead.
      duration: the {@link Duration} to be formatted
      Return type: @return:A formatted string representing {@code duration}
    • getFormat (unit As TimeUnit) As TimeFormat
      Get the registered {@link TimeFormat} for the given {@link TimeUnit} or <code>null</code> if none exists.
    • getUnit (unitType As Class) As UNIT
      Get the registered {@link TimeUnit} for the given {@link TimeUnit} type or <code>null</code> if none exists.
    • registerUnit (unit As TimeUnit, format As TimeFormat) As PrettyTime
      Register the given {@link TimeUnit} and corresponding {@link TimeFormat} instance to be used in calculations. If
      an entry already exists for the given {@link TimeUnit}, its {@link TimeFormat} will be overwritten with the given
      {@link TimeFormat}. ({@link TimeUnit} and {@link TimeFormat} must not be <code>null</code>.)
    • removeUnit (unitType As Class) As TimeFormat
      Removes the mapping for the given {@link TimeUnit} type. This effectively de-registers the {@link TimeUnit} so it
      will not be used in formatting. Returns the {@link TimeFormat} that was removed, or <code>null</code> if no unit
      of the given type was registered.
    • setLocale (locale As Locale) As PrettyTime
      Set the the {@link Locale} for this {@link PrettyTime} object. This may be an expensive operation, since this
      operation calls {@link TimeUnit#setLocale(Locale)} for each {@link TimeUnit} in {@link #getUnits()}.
    • setReference (timestamp As Date) As PrettyTime
      Set the reference {@link Date}. If <code>null</code>, {@link PrettyTime} will always use the current value of
      {@link System#currentTimeMillis()} as the reference {@link Date}.
      <p>
      If the {@link Date} formatted is before the reference {@link Date}, the format command will produce a
      {@link String} that is in the past tense. If the {@link Date} formatted is after the reference {@link Date}, the
      format command will produce a {@link String} that is in the future tense.
    • toString As String
    Properties:
    • Locale As Locale [read only]
      Get the currently configured {@link Locale} for this {@link PrettyTime} object.
    • Reference As Long [read only]
      Get the current reference {@link Date}, or <code>null</code> if no reference {@link Date} is set.
      <p>
      See {@code PrettyTime.setReference(Date timestamp)}
    • Units As List [read only]
      Get the unmodifiable {@link List} of the current configured {@link TimeUnit} instances in calculations.

B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim pt As PrettyTime
End Sub
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    pt.Initialize("")   
    Log(pt.format(DateTime.Now+ 1000*60*10))
End Sub

Please note this is german as my devicelanguage is german. It should work with other languages too i guess
in 10 Minuten
 

Attachments

  • PrettyTimeV1.0.zip
    99.2 KB · Views: 402
Top