How to convert javax xml. datatype. Duration

I have javax from the server xml. datatype. Response in duration format

thank you.

Solution

A simple and direct solution without external libraries (there is no built-in duration formatter in Java):

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 a single form like "day" in case a duration part is only equal to 1, you need to add a small enhancement to this code Perhaps you also need to first standardize your duration (for example, if your second part exceeds 59), see Javadoc

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>