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

234
Visualizações
DateTimeFormatter.parse returns an invalid Japanese date

I'm trying to convert a Japanese date string to JapaneseDate.

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("Gyy年MM月dd日")
    .withChronology(JapaneseChronology.INSTANCE)
    .withResolverStyle(ResolverStyle.LENIENT);
JapaneseDate d = JapaneseDate.from(formatter.parse("平成01年01月01日"));
System.out.println(d);
System.out.println(d.format(formatter));

output:

Japanese Reiwa 71-01-01
令和71年01月01日

Since "令和元年" is "2019", "令和71年01月01日" is "2089-01-01". "平成01年01月01日" must be "1989-01-01".

How can I convert it correctly?

If I change the resolver style to ResolverStyle.STRICT, it throws

java.time.format.DateTimeParseException: Text '平成01年01月01日'
could not be parsed: year, month, and day not valid for Era

If I change the resolver style to ResolverStyle.SMART, it throws

java.time.format.DateTimeParseException: Text '平成01年01月01日'
could not be parsed: Invalid YearOfEra for Era: Heisei 101
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Try with this pattern:

DateTimeFormatter.ofPattern("Gy年MM月dd日")

y should be used single-handed otherwise it will be added a hundred years when it is up to 11 (cf. Javaバージョン別の改元(新元号)対応まとめ).

year 11:

JapaneseDate d = JapaneseDate.from(formatter.parse("平成11年11月01日"));

output:

Japanese Reiwa 81-11-01
令和81年11月01

year 12:

JapaneseDate d = JapaneseDate.from(formatter.parse("平成12年11月01日"));

output:

Japanese Heisei 12-11-01
平成12年11月01
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