How to convert javax.xml.datatype.Duration

I have a response from the server in javax.xml.datatype.Duration format .
This is what I get Travel duration = P2DT15H45M0S
and I want it in the format
required format = 2 days 15 hours 45 min
How to do it?
Is there a way to convert from javax.xml.datatype.Duration to String .

Thank.

+3


source to share


2 answers


Simple and straight forward solution without external library (Java has no built-in duration formatting):

Duration dur = DatatypeFactory.newDuration("P2DT15H45M0S");
int days = dur.getDays();
int hours = dur.getHours();
int minutes = dur.getMinutes();

String formatted = days + " Days " + hours + " Hrs " + minutes + " Min"

      



If you want special shapes like "Day", if one part of the duration is 1, it's up to you to add a little improvement to this code. You might also need to normalize your duration first (e.g. if your second part is outside 59), see the javadoc .

+7


source


Convert javax.xml.datatype.Duration instance to string

duration.toString()

      



then follow the link. which will help in correct formatting

0


source







All Articles