1

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.

Halley
  • 521
  • 10
  • 35
  • You will need to use a [ResourceBundle](https://docs.oracle.com/javase/9/docs/api/java/util/ResourceBundle.html), which allows you to store each locale’s format string in a separate property file or separate class. – VGR Nov 29 '17 at 23:12
  • 1
    You can add any text literal to the pattern in single quotes. The difficult part is the "th" suffix and translation. – shmosel Nov 29 '17 at 23:24

1 Answers1

2

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.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • Upvoted for the detailed answers! I was able to localize the string, but the ordinal numbers were missing. And that is what I was interested in! Cheers for your answer! – Halley Nov 30 '17 at 17:49