How can I "pretty print" a Duration in Java?

how can it be youtube
marion aunor how can i mp3 download
how can i get
how can we
how can you
how can i song
how can i say
how can i do

Does anyone know of a Java library that can pretty print a number in milliseconds in the same way that C# does?

E.g., 123456 ms as a long would be printed as 4d1h3m5s.

Joda Time has a pretty good way to do this using a PeriodFormatterBuilder.

Quick Win: PeriodFormat.getDefault().print(duration.toPeriod());


//import org.joda.time.format.PeriodFormatter;
//import org.joda.time.format.PeriodFormatterBuilder;
//import org.joda.time.Duration;

Duration duration = new Duration(123456); // in milliseconds
PeriodFormatter formatter = new PeriodFormatterBuilder()
String formatted = formatter.print(duration.toPeriod());

Coronavirus: How can I help?, Coronavirus: How can I help? By Helier Cheung BBC News, Washington DC. 01 May 2020; World. Share this with Email Share this with Facebook Share this  How Can I - H 3 F (Official Music Video) #H3F #HowCanI #CheesyLyricsSloppyGroove. Now available on all digital download & streaming platform , follow the link below ! - -

I've built a simple solution, using Java 8's Duration.toString() and a bit of regex:

public static String humanReadableFormat(Duration duration) {
    return duration.toString()
            .replaceAll("(\\d[HMS])(?!$)", "$1 ")

The result will look like:

- 5h
- 7h 15m
- 6h 50m 15s
- 2h 5s
- 0.1s

If you don't want spaces between, just remove replaceAll.

How Can I Receive Christ?, The help Jesus offers isn't a commodity you can buy with money or morality. To have the Son, you must receive him by faith. Licensed to YouTube by. [Merlin] FUGA Aggregation (on behalf of Higher Forces Records), and 6 Music Rights Societies.

Apache commons-lang provides a useful class to get this done as well DurationFormatUtils

e.g. DurationFormatUtils.formatDurationHMS( 15362 * 1000 ) ) => 4:16:02.000 (H:m:s.millis) DurationFormatUtils.formatDurationISO( 15362 * 1000 ) ) => P0Y0M0DT4H16M2.000S, cf. ISO8601

What Is a Senior Developer and How Can I Become One?, Colby Fayock. A Front End Engineer and UX Designer that's passionate about tackling challenges that can make the world a better place. https://  Pumapag-Ibig - Marion feat. Rizza Cabrera and Seed Bunye (Music Video) - Duration: 4:14. ABS-CBN Star Music 992,055 views

JodaTime has a Period class that can represent such quantities, and can be rendered (via IsoPeriodFormat) in ISO8601 format, e.g. PT4D1H3M5S, e.g.

Period period = new Period(millis);
String formatted = ISOPeriodFormat.standard().print(period);

If that format isn't the one you want, then PeriodFormatterBuilder lets you assemble arbitrary layouts, including your C#-style 4d1h3m5s.

How can I enjoy the warm weather? What you can and can't do in , In Portland and the Willamette Valley area, Oregon State Parks and Recreation reopened Tyron Creek, Willamette Mission, Mongold Boat Ramp,  How I can. I'm answering that way. When asking a question, the pronouns should be followed by an (auxiliary) verb. So, the structure in general is - What/How/Which etc. + verb + pronoun. A simple example is - What can you do for me? If you reverse the order, the phrase remains a sentence and not a question. What you can do for me is unknown.

With Java 8 you can also use the toString() method of java.time.Duration to format it without external libraries using ISO 8601 seconds based representation such as PT8H6M12.345S.

Which one is correct in "How can I "? VS "How I can ", I did not get your question fully, but I think you are confused with the structure of a question - How can I Vs. How I can. I'm answering that way. When asking a  Family History Can Be Fun Recommended for you. 7:26. Joseph Smith first prayer + The Spirit of God - Duration: 4:01. George Leausa 94,124 views. 4:01.

How Can I Keep from Singing?, How can I worship on Mother's Day weekend? Here are hundreds of churches and temples with live-stream options. Updated 8:48 PM; Today 3:48 PM. Others have offered excellent answers. I would just add, for further clarification, that both are correct depending on what you want to say. If you are telling someone something, you would use "how I can teach." If you are asking someone somethin

How can I worship on Mother's Day weekend? Here are hundreds of , The Can I Share It look-up tool provides academic researchers with an easy way to check where a journal article can be shared in line with the paper's access and​  You can then grab the keys from that computer easily. You can also use a Linux live CD to pull the Windows directory off the other computer and onto a thumb drive, or just grab the registry files if you prefer. If you need help, we’ve got a guide to pulling data off a dead computer. RELATED: How to Recover Files From a Dead Computer

How Can I Share It, If it's been less than 30 days since you initiated the deletion, you can cancel your account deletion. After 30 days, your account and all your information will be permanently deleted, and you won't be able to retrieve your information. It may take up to 90 days from the beginning of the deletion process

  • FYI, the format you seem to be describing is a Duration defined in the sensible standard, ISO 8601: PnYnMnDTnHnMnS where P means "Period" and marks the beginning, T separates the date portion from time portion, and in between are optional occurrances of a number with a single-letter abbreviation. For example, PT4H30M = four and a half hours.
  • If all else fails it's a very simple matter to do it yourself. Just use successive applications of % and / to split the number into parts. Almost easier than some of the proposed answers.
  • @HotLicks Leave it to library methods for much cleaner and clearer code than using / and %.
  • Yes, in the intervening 8 years since I asked this question (!) I've moved over to joda time which suits my use case very well
  • @phatmanace The Joda-Time project is now in maintenance-mode, and advises migration to the java.time classes built into Java 8 and later.
  • It appears from an answer below that an instance of Period can be created directly, without first creating a Duration instance and then converting it to Period. E.g. Period period = new Period(millis); String formatted = formatter.print(period);
  • Beware of this "duration.toPeriod()" conversion. If the duration is quite big, the day portion onwards would remain as 0. The hours portion will keep growing. You will get 25h10m23s but never get the "d". The reason is there is no fully correct way to convert hours to days in Joda's strict ways. Most of the cases, if you are comparing two instants and wanting to print it, you can do new Period(t1, t2) instead of new Duration(t1, t2).toPeriod().
  • @Boon do you know how to convert duration to period with respect to days? I use duration from which I can substitute second in my on my timer. Event setting PeriodType.daysTime() or .standard() didn't help
  • @murt If you really want to, and can accept the standard definition of a day, then you can "formatter.print(duration.toPeriod().normalizedStandard())" in the code above. Have fun!
  • You should never rely on the output of toStrong() because this may change in further versions.
  • @Weltraumschaf it's not likely to change, since it's specified in the javadoc
  • @Weltraumschaf Unlikely to change as the output of Duration::toString is formatted according to the well-defined and well-worn ISO 8601 standard.
  • Just as a note, new Period(millis).toString() uses the ISOPeriodFormat.standard() by default.