You can try my library Time4A which is the Android version of Time4J. Demo:
Locale uk = Locale.UK;
TZID paris = EUROPE.PARIS;
ChronoFormatter<Moment> f =
ChronoFormatter.ofMomentPattern("d. MMMM uuuu h:mm B", PatternType.CLDR, uk, paris);
PrettyTime pt = PrettyTime.of(uk);
// => examples for relative formats
Moment moment = f.parse("4. January 2016 5:45 in the afternoon");
System.out.println(pt.printRelative(moment, paris));
// output: 1 week ago
Moment now = SystemClock.currentMoment();
System.out.println(pt.printRelative(now, paris));
// output: now
Moment shortTimeAgo = now.minus(15, SI.SECONDS);
System.out.println(pt.printRelative(shortTimeAgo, paris));
// output: 15 seconds ago
TimeUnit precision = TimeUnit.DAYS;
System.out.println(pt.printRelative(shortTimeAgo, Timezone.of(paris), precision));
// output: today
// => examples for list formats
Duration<?> duration = // this way for getting duration requires extra range-module
MomentInterval.between(moment, now).getNominalDuration(
Timezone.of(paris),
DAYS, HOURS, MINUTES);
System.out.println(pt.print(duration));
// output: 11 days, 1 hour and 26 minutes
System.out.println(pt.print(duration, TextWidth.ABBREVIATED));
// output: 11 days, 1 hr, 26 min
Actually there is support for 72 languages (v3.18-2016c) including localized plural rules (important for slavish languages or Arabic) and three different text widths:
af am ar az be bg bn bs ca cs da de el en es et fa fi
fil fr ga gu he hi hr hu hy id is it ja ka kk km kn ko
ky lo lt lv mk mn mr ms my nb ne nl nn pa pl pt ro ru
si sk sl sq sr sv sw ta te th tk tr uk ur uz vi zh zu
All units from years to seconds (or even millis, micros, nanos for list formats) are supported.
In order to convert java.util.Date
or long-timestamps (expressing millisecs since Unix epoch) from/to time4j-types you can use:
java.util.Date d = TemporalType.JAVA_UTIL_DATE.from(moment);
long epochMillis = TemporalType.MILLIS_SINCE_UNIX.from(moment);
Moment m1 = TemporalType.JAVA_UTIL_DATE.translate(d);
Moment m2 = TemporalType.MILLIS_SINCE_UNIX.translate(epochMillis);
Update from 2019-06-06:
The newer version Time4A-4.4-2019a with the gradle-dependency
dependencies {
implementation group: 'net.time4j', name: 'time4j-android', version: '4.4-2019a'
}
supports 94 languages and now also includes relative patterns like "next Sunday".