Re: Question about formatting time string

Splash Forums PrettyTime Users Question about formatting time string Re: Question about formatting time string

#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();
}
}