Fortunately, there is a solution on the Maven Central.
<dependency>
<groupId>com.github.bogdanovmn.humanreadablevalues</groupId>
<artifactId>human-readable-values</artifactId>
<version>1.0.1</version>
</dependency>
You can just get values for amount of bytes or seconds. Also you can create you own fraction class.
Docs
https://github.com/bogdanovmn/java-human-readable-values
Seconds example
assertEquals(
"2h 46m 40s",
new SecondsValue(10000).fullString()
);
assertEquals(
"2.8h",
new SecondsValue(10000).shortString()
);
Bytes example
assertEquals(
"9K 784b",
new BytesValue(10000).fullString()
);
assertEquals(
"9.8K",
new BytesValue(10000).shortString()
);
Customization example
public class MyTypeValue extends FractionatedValue {
public MyTypeValue(long value) {
super(
value,
new FractionSpecification(
FractionDefinition.builder()
.name("the minimal fraction unit")
.shortNotation("foo")
.minimalUnitsAmount(1)
.build(),
FractionDefinition.builder()
.name("another fraction unit")
.shortNotation("baz")
.minimalUnitsAmount(60)
.build(),
FractionDefinition.builder()
.name("the last fraction unit")
.shortNotation("foo baz")
.minimalUnitsAmount(3600)
.build()
)
);
}
}
...
assertEquals(
"2foo 46baz 40foo baz",
new MyTypeValue(10000).fullString()
);