Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

197
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda