Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add localized formatting to Seconds, Minutes, Hours, Days, Weeks, Months, Years. #111

Closed
basil-bourque opened this issue Oct 11, 2018 · 3 comments

Comments

@basil-bourque
Copy link

basil-bourque commented Oct 11, 2018

In the java.time classes, the DateTimeFormatter class offers ofLocalized… methods where localized text is generated to represent the date-time object's value, guided by the specified FormatStyle and Locale.

Example:

String output = myLocalDate.format( 
    DateTimeFormatter
    .ofLocalizedDate( FormatStyle.LONG )
    .withLocale( Locale.CANADA_FRENCH ) 
) ;

11 octobre 2018

It would be nice if the Seconds, Minutes, Hours, Days, Weeks, Months, and Years classes in ThreeTen-Extra could also generate localized text.

Someone asked for such localized text on Stack Overflow: Are there standard ways to accomplish localized date components on Android such as 1s, 1m, 1h, 1d, 1w, 1week, etc.

The example given in that posting, for a date component in English and a Spanish localization:

English (en):

  • 3w
  • 3 wks
  • 3 weeks

Spanish (es):

  • 3sem
  • 3 sem
  • 3 semanas

Possible solution: CLDR

According to JEP 252, the Unicode Consortium's Common Locale Data Repository (CLDR) has been bundled with Java 8 and later, at least in OpenJDK.

It seems like these values needed for this feature might be found in the CLDR if I am correctly reading the <relativeTime> item in section 3, Calendar Fields, of Unicode Technical Standard #35 UNICODE LOCALE DATA MARKUP LANGUAGE (LDML) PART 4: DATES:

relativeTime Display names for an instance of the field that is a counted number of units in the past or the future relative to the current instance; this needs plural forms. In English, data is provided for year, quarter, month, week, day, specific days of the week, ,hour, minute, and second.

@jodastephen
Copy link
Member

This branch has work I started on this problem. The data is there (from Joda-Time) but I haven't got the time to fix the code. If anyone reading this wants to attempt a PR, please add a comment.

@monicagg
Copy link
Contributor

I forked that branch and looked through source code. These few weeks I have time to contribute and soon will send a PR.

@jodastephen
Copy link
Member

This has been merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

3 participants