Duration Format

A small library for producing human-readable string representations of time durations.

License

License

Categories

Categories

ORM Data
GroupId

GroupId

hu.kazocsaba
ArtifactId

ArtifactId

duration-format
Last Version

Last Version

1.0.0
Release Date

Release Date

Type

Type

jar
Description

Description

Duration Format
A small library for producing human-readable string representations of time durations.
Project URL

Project URL

https://github.com/kazocsaba/duration-format
Source Code Management

Source Code Management

https://github.com/kazocsaba/duration-format.git

Download duration-format

How to add to project

<!-- https://jarcasting.com/artifacts/hu.kazocsaba/duration-format/ -->
<dependency>
    <groupId>hu.kazocsaba</groupId>
    <artifactId>duration-format</artifactId>
    <version>1.0.0</version>
</dependency>
// https://jarcasting.com/artifacts/hu.kazocsaba/duration-format/
implementation 'hu.kazocsaba:duration-format:1.0.0'
// https://jarcasting.com/artifacts/hu.kazocsaba/duration-format/
implementation ("hu.kazocsaba:duration-format:1.0.0")
'hu.kazocsaba:duration-format:jar:1.0.0'
<dependency org="hu.kazocsaba" name="duration-format" rev="1.0.0">
  <artifact name="duration-format" type="jar" />
</dependency>
@Grapes(
@Grab(group='hu.kazocsaba', module='duration-format', version='1.0.0')
)
libraryDependencies += "hu.kazocsaba" % "duration-format" % "1.0.0"
[hu.kazocsaba/duration-format "1.0.0"]

Dependencies

test (1)

Group / Artifact Type Version
junit : junit jar 4.4

Project Modules

There are no modules declared in this project.

Duration format

This library provides the class DurationFormat that can be used to create string representations of time durations. The primary feature of the library is that the time unit is selected automatically based on the input. The following example outputs are created using the same configuration:

13 s 499 μs
93 m 6 s 486 ms

Using

The library resides in the central Maven repository with group ID hu.kazocsaba and artifact ID duration-format. If you use a project management system which can fetch dependencies from there, you can just add the library as a dependency. E.g. in Maven:

<dependency>
    <groupId>hu.kazocsaba</groupId>
    <artifactId>duration-format</artifactId>
    <version>a.b.c</version>
</dependency>

Example

Quick and dirty benchmarking:

long start = System.nanoTime();
// run something
long end = System.nanoTime();
System.out.println("Finished in " + new DurationFormat().format(end - start));

Customizing the behaviour:

DurationFormat durationFormat = new DurationFormat()
    .setDropInnerZeroes(false)
    .timeUnit(TimeUnit.MILLISECONDS)        // never display a time unit higher than milliseconds
    .timeUnitLevel(3);                      // allow milliseconds, microseconds, and nanoseconds

durationFormat.format(360);                 // -> "360 ns"
durationFormat.format(150000000);           // -> "1500 ms 0 μs 0 ns"
durationFormat.format(60001003);            // -> "60 ms 1 μs 3 ns"

durationFormat = new DurationFormat()
    .timeUnit(TimeUnit.SECONDS);            // always show the duration in seconds
                                            // .timeUnitLevel(1) is the default

durationFormat.format(360);                 // -> "0 s"
durationFormat.format(158334286578L);       // -> "158 s"

durationFormat = new DurationFormat()       // choose the time unit automatically
    .lowestTimeUnit(TimeUnit.MILLISECONDS); // but we're not interested in anything below milliseconds

durationFormat.format(360);                 // -> "0 ms"
durationFormat.format(1500401823);          // -> "1500 ms"
durationFormat.format(158334286578L);       // -> "3 m"

Versions

Version
1.0.0