Question about formatting time string

Splash Forums PrettyTime Users Question about formatting time string


This topic contains 1 reply, has 2 voices, and was last updated by  Lincoln Baxter III 10 years, 3 months ago.

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
  • #18059



    I want my time duration to have the following format:

    * hours and * minutes ago

    But i’m only getting <* hours ago> each time.

    What do i need to do to get the minutes in there?



    I see now that when the duration is less than an hour, it is formatted in minutes but when it’s > 1 hour, it’s formatted in hours without minutes. But i want also when the duration is > 1 hour, to see the minutes.


    Hi, currently in order to do what you ask, you’ll need to use:

    PrettyTime p = new PrettyTime();
    p.setUnits(new Hour(), new Minute());
    List<Duration> durations = p.calculatePreciseDuration(myDate);

    Then you’ll have to format each duration:

    List<String> formatted = new ArrayList();

    for(Duration d : durations)

    Then just join your elements:

    String result = Strings.join(formatted, "and")

    Using something like this:

    * @author <a href="">Lincoln Baxter, III</a>
    public abstract class Strings
    * Join a collection of strings using the given delimeter.
    public static String join(final Collection<?> collection, final String delimiter)
    StringBuffer buffer = new StringBuffer();
    Iterator<?> iter = collection.iterator();
    while (iter.hasNext())
    if (iter.hasNext())
    return buffer.toString();

Viewing 2 posts - 1 through 2 (of 2 total)

The forum ‘PrettyTime Users’ is closed to new topics and replies.

Comments are closed.