4

Is there simple way to convert time interval (age) to text? For example - age is 25.45y. I need conversion to "25 years, 3 months, 1 day". Question is not about numbers 25,3,1 but how to translate year/month/day using correct form (plural, declension) to different languages. English language seems to be easy to hardcode but others are not and I would prefer some generic solution.

number / English / Czech / ...
1 / day / den
2 / days / dny
5 / days / dnů
...

Victor Zakharov
  • 25,801
  • 18
  • 85
  • 151
LubošCZ
  • 91
  • 6

2 Answers2

4

Joda time will do that pretty easily.

For example:

public static void main(String[] args) {
  PeriodFormatter daysHoursMinutes = new PeriodFormatterBuilder()
    .appendDays()
    .appendSuffix(" day", " days")
    .appendSeparator(" and ")
    .appendMinutes()
    .appendSuffix(" minute", " minutes")
    .appendSeparator(" and ")
    .appendSeconds()
    .appendSuffix(" second", " seconds")
    .toFormatter();

  Period period = new Period(72, 24, 12, 0);

  System.out.println(daysHoursMinutes.print(period));
  System.out.println(daysHoursMinutes.print(period.normalizedStandard()));
}

Will print:

24 minutes and 12 seconds
3 days and 24 minutes and 12 seconds

See also: Period to string

Community
  • 1
  • 1
Vincent Mimoun-Prat
  • 28,208
  • 16
  • 81
  • 124
  • 2
    Thanks for the example, but it shows how to hardcode it for english language only. Prefer generic solution. Can't use method "appendSuffix(String singularText, String pluralText)" because declension of plural forms is not considered... – LubošCZ Feb 04 '13 at 19:54
3

JodaTime can do this with most of its formatters. Look at the javadoc of PeriodFormat as an example.

It says:

Controls the printing and parsing of a time period to and from a string.

This class is the main API for printing and parsing used by most applications. Instances of this class are created via one of three factory classes:

PeriodFormat - formats by pattern and style
ISOPeriodFormat - ISO8601 formats
PeriodFormatterBuilder - complex formats created via method calls
An instance of this class holds a reference internally to one printer and one parser. It is possible that one of these may be null, in which case the formatter cannot print/parse. This can be checked via the isPrinter() and isParser() methods.

The underlying printer/parser can be altered to behave exactly as required by using a decorator modifier:

withLocale(Locale) - returns a new formatter that uses the specified locale
This returns a new formatter (instances of this class are immutable).
The main methods of the class are the printXxx and parseXxx methods. These are used as follows:

 // print using the default locale
 String periodStr = formatter.print(period);
 // print using the French locale
 String periodStr = formatter.withLocale(Locale.FRENCH).print(period);

 // parse using the French locale
 Period date = formatter.withLocale(Locale.FRENCH).parsePeriod(str);
Daniel Kaplan
  • 62,768
  • 50
  • 234
  • 356