Add the dependency to your pom.xml
:
<dependency>
<groupdId>to.lova.humanize</groupId>
<artifactId>humanize-time</artifactId>
<version>0.1.2</version>
</dependency>
Use it in your code as simple as:
Temporal minutesAgo = ZonedDateTime.now().minus(5, ChronoUnit.MINUTES);
String relative = HumanizeTime.fromNow(minutesAgo);
// produces: "5 minutes ago"
By default it uses the locale provided by Locale.getDefault()
, but you can request a different one or provide your own resolver:
Temporal minutesAgo = ZonedDateTime.now().minus(5, ChronoUnit.MINUTES);
Locale.setDefault(Locale.ITALIAN);
String italian = HumanizeTime.fromNow(temporal);
// produces: "5 minuti fa"
String french = HumanizeTime.fromNow(temporal, Locale.FRENCH);
// produces: "il y a 5 minutes"
String japanese = HumanizeTime.fromNow(temporal, () -> Locale.JAPANESE);
// produces: "5時間前"