0

I have the following code:

String dateTimeInStr = "17-Jul-2020 12:12";
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MMM-YYYY HH:mm");
       // OffsetDateTime offsetDateTime = OffsetDateTime.parse(dateTimeInStr, formatter);
        LocalDateTime localDateTime = LocalDateTime.parse(dateTimeInStr, formatter);
        System.out.println(localDateTime);
     //   System.out.println(offsetDateTime);

As you can see I try convert string 17-Jul-2020 12:12 to the LocalDateTime or OffsetDateTime. But nothing works.

Text '17-Jul-2020 12:12' could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor: {WeekBasedYear[WeekFields[MONDAY,1]]=2020, MonthOfYear=7, DayOfMonth=17},ISO resolved to 12:12 of type java.time.format.Parsed

Does anyone know how to fix this issue? Thanks in advance.

Den B
  • 843
  • 1
  • 10
  • 26

1 Answers1

2

Case-sensitive

You are using uppercase 'YYYY' which is the week year. Try with lowercase 'yyyy':

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MMM-yyyy HH:mm");

Locale

And specify a Locale for the human language and cultural norms used in translating the name of month.

DateTimeFormatter formatter = 
        DateTimeFormatter
        .ofPattern("dd-MMM-yyyy HH:mm")
        .withLocale( Locale.US );

See this code run live at IdeOne.com.

2020-07-17T12:12

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Léo Schneider
  • 2,085
  • 3
  • 15
  • 28