Period to string

Viewed 40166

I'm using the Joda-Time library with Java. I'm having some difficulty trying to turn a Period object to a string in the format of "x days, x hours, x minutes".

These Period objects are first created by adding an amount of seconds to them (they are serialized to XML as seconds and then recreated from them). If I simply use the getHours() etc. methods in them, all I get is zero and the total amount of seconds with getSeconds.

How can I make Joda calculate the seconds into the respective fields, like days, hours, etc...?

4 Answers

You can also use the Default formatter, which is good for most cases:

Period period = new Period(startDate, endDate);
System.out.println(PeriodFormat.getDefault().print(period))
Related