Halley
Halley

Reputation: 531

Java date format conversions with localization

I want to convert my epoch time to the below format.

Tuesday, 29th of August, 12:30 PM

I am trying to use DateTimeFormatter

final DateTimeFormatter formatter = DateTimeFormatter
                .ofPattern("cccc, dd MMMM, hh:mm a")
                .withZone(ZoneId.of(timezone)).withLocale(locale);

Now, how can I get 'of' in my output? And that too, localized? For instance, in Spanish, I want it as

martes, 29 de agosto, 12:30 PM.

Upvotes: 1

Views: 103

Answers (1)

Basil Bourque
Basil Bourque

Reputation: 340118

If you want to automatically localize the date-time format language and styling, let DateTimeFormatter class determine the formatter by calling the ofLocalized… methods.

To localize, specify:

  • FormatStyle to determine how long or abbreviated should the string be.
  • Locale to determine (a) the human language for translation of name of day, name of month, and such, and (b) the cultural norms deciding issues of abbreviation, capitalization, punctuation, separators, and such.

Example code…

LocalDateTime ldt = LocalDateTime.of( 2017 , 8 , 29 , 12 , 30 );

Locale localeUS = Locale.US;
Locale localeCanadaFrench = Locale.CANADA_FRENCH;
Locale localeSpain = new Locale( "es" , "ES" );

DateTimeFormatter f = DateTimeFormatter.ofLocalizedDateTime( FormatStyle.MEDIUM );

String outputUS = ldt.format( f.withLocale( localeUS ) );
String outputCanadaFrench = ldt.format( f.withLocale( localeCanadaFrench ) );
String outputSpain = ldt.format( f.withLocale( localeSpain ) );

Dump to console.

System.out.println( "outputUS: " + outputUS );
System.out.println( "outputCanadaFrench: " + outputCanadaFrench );
System.out.println( "outputSpain: " + outputSpain );

outputUS: Aug 29, 2017, 12:30:00 PM

outputCanadaFrench: 29 août 2017 12:30:00

outputSpain: 29 ago. 2017 12:30:00

For a format style of LONG or FULL, you need to assign a time zone.

LocalDateTime ldt = LocalDateTime.of( 2017 , 8 , 29 , 12 , 30 );
ZoneId z = ZoneId.of( "Pacific/Auckland" );
ZonedDateTime zdt = ldt.atZone( z );

Locale localeUS = Locale.US;
Locale localeCanadaFrench = Locale.CANADA_FRENCH;
Locale localeSpain = new Locale( "es" , "ES" );

DateTimeFormatter f = DateTimeFormatter.ofLocalizedDateTime( FormatStyle.FULL );

String outputUS = zdt.format( f.withLocale( localeUS ) );
String outputCanadaFrench = zdt.format( f.withLocale( localeCanadaFrench ) );
String outputSpain = zdt.format( f.withLocale( localeSpain ) );

outputUS: Tuesday, August 29, 2017 at 12:30:00 PM New Zealand Standard Time

outputCanadaFrench: mardi 29 août 2017 à 12:30:00 heure normale de la Nouvelle-Zélande

outputSpain: martes, 29 de agosto de 2017, 12:30:00 (hora estándar de Nueva Zelanda)

Ordinal numbering

I have not noticed any ordinal numbers (such as th, nd, rd, etc.) appearing in any of the localizations that I’ve seen.


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Upvotes: 2

Related Questions