Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

204
Views
How do I format am-pm-of-day to be "AM/PM" rather than "a.m./p.m."?

DateTimeFormatter's API reference seems to miss details for formatting am-pm-of-day properly, I could always use String.replace() but I feel like it makes more sense to have an option change a.m. to AM and/or p.m. to PM using "aaa...". Is there a way to do this?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Localized per CLDR

When formatting the textual representation of a date-time object, decisions about capitalization and abbreviation are matters left to the rules of localization. Those rules are defined as part of a Locale object. In the latest versions of Java built on the codebase of the OpenJDK project, the Locale class gets its localization data from the Common Locale Data Repository (CLDR) maintained by the Unicode Consortium.

So no, there is no way to specify use of “AM” versus “a.m.” other than to specify one Locale object versus another.

ZonedDateTime zdt = ZonedDateTime.now( ZoneId.of( "Asia/Tokyo" ) ) ;
DateTimeFormatter f = DateTimeFormatter.ofLocalizedTime( FormatStyle.MEDIUM ) ;
String outputCaFr = zdt.format( f.withLocale( Locale.CANADA_FRENCH ) ) ;
String outputUkEn = zdt.format( f.withLocale( Locale.UK) ) ;

If you insist on a particular format, you’ll have to do the string manipulation yourself. But I suggest letting Locale, DateTimeFormatter, and the CLDR do their work of localizing rather than hard-coding trivial formatting tweaks.

Note that the CLDR, and therefore the Locale & DateTimeFormatter classes, support sub-cultures. Explore the CLDR’s definitions if the higher-level culture does not meet your expectations. One of the sub-cultures may hold the choice of localization rules you want.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!