Question about formatting time string

Splash Forums PrettyTime Users Question about formatting time string

Tagged: 

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
    Posts
  • #18059

    muijsenbergq
    Participant

    Hello,

    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?

    Thanks!

    update:

    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.

    #21585

    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)
    {
    formatted.add(p.format(d));
    }

    Then just join your elements:

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

    Using something like this:

    /**
    * @author <a href="mailto:lincolnbaxter@gmail.com">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())
    {
    buffer.append(iter.next());
    if (iter.hasNext())
    {
    buffer.append(delimiter);
    }
    }
    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.