2

Trying to split the date and time which comes in the French style like the example below:

23 sept. 2021 à 13:30:57

And the final result should be the example below:

23-09-2021 and 13:30:57

The issue comes because of the << à >> when trying to get the time. I've tried with the different way such as DateTimeFormatter, DateFormat etc...

Could you please help? thanks in advance

Arsench
  • 179
  • 2
  • 4
  • 17

1 Answers1

5

Localization

The java.time classes can automatically localize. So let them parse your localized input. No need to specify a formatting pattern.

This approach works because your input uses the format defined by the Common Locale Data Repository (CLDR) used by default in modern Java.

LocalDateTime.parse( 
    "23 sept. 2021 à 13:30:57" , 
    DateTimeFormatter
    .ofLocalizedDateTime( FormatStyle.MEDIUM )
    .withLocale( Locale.FRANCE )
) 
.toString()

See this code run live at IdeOne.com.

2021-09-23T13:30:57

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154