0

I have the following code:

val dateString = "12/Sep/2017:11:25:29 +0200"
println(LocalDateTime.parse(
    dateString, 
    DateTimeFormatter.ofPattern("dd/LLL/yyyy:HH:mm:ss X")
));

and the following error:

Exception in thread "main" java.time.format.DateTimeParseException:
Text '12/Sep/2017:11:25:29 +0200' could not be parsed at index 3

What is wrong in my format? I read https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html but do not find the correct one

EDIT1: "dd/MMM/yyyy:HH:mm:ss X" does not work either

Benjamin
  • 3,350
  • 4
  • 24
  • 49
  • 2
    [Never use SimpleDateFormat or DateTimeFormatter without a Locale](https://stackoverflow.com/a/65544056/10819573) – Arvind Kumar Avinash Jun 10 '21 at 20:49
  • Does this answer your question? [java DateTimeFormatterBuilder fails on testtime \[duplicate\]](https://stackoverflow.com/questions/50526234/java-datetimeformatterbuilder-fails-on-testtime) – Ole V.V. Jun 11 '21 at 07:11

3 Answers3

3

You have to use M instead of L (L is for the numeric format):

"dd/MMM/yyyy:HH:mm:ss X"

You probably have also to set the Locale that java will use to interpret the month (probably your locale is not english)

LocalDateTime.parse("12/Sep/2017:11:25:29 +0200", DateTimeFormatter.ofPattern("dd/MMM/yyyy:HH:mm:ss X", Locale.ENGLISH));

This works for me (with the locale declared, probably also L will work fine)

Alberto Sinigaglia
  • 12,097
  • 2
  • 20
  • 48
  • maybe OF, but it might help: I use http://www.sdfonlinetester.info/ to test the format of the dates, to check if it's correct or not (is correct if the output of the website is the input that you are providing) – Alberto Sinigaglia Jun 10 '21 at 20:18
  • The doc say the opposite: " M/L month-of-year number/text 7; 07; Jul; July; J" and use MMM return the same error – Benjamin Jun 10 '21 at 20:21
  • and I am not sure SimpleDateFormat is equivalent to DateTimeFormatter for text month – Benjamin Jun 10 '21 at 20:22
  • 1
    @Benjamin The Javadoc also explains that `L` is for the “standalone” month name. Some languages vary the spelling depending on whether the month is used by itself or in combination. English is not one of those languages. – Basil Bourque Jun 10 '21 at 20:32
  • @BasilBourque ho OK thanks for the explanation. As I said "dd/MMM/yyyy:HH:mm:ss X" does not work either – Benjamin Jun 10 '21 at 20:35
3

The Answer by Sinigaglia is correct but insufficient. You should specify a Locale for which Sep is recognizable as a month name. An English-speaking locale is needed such as Locale.US.

If unspecified the JVM’s current default time zone is automatically implicitly applied. Sep is not a month name in Russian, French, Japanese, etc.

Tip: As a general rule, always specify explicitly your intended locale and time zone; use the optional arguments. Relying implicitly on the JVM’s current defaults can be problematic.

    Locale locale = Locale.US ;
    DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd/MMM/yyyy:HH:mm:ss X" ).withLocale( locale ) ;
    String input = "12/Sep/2017:11:25:29 +0200" ;
    OffsetDateTime odt = OffsetDateTime.parse( input , f ) ;

    System.out.println( odt ) ;

See this code run live on IdeOne.com.

2017-09-12T11:25:29+02:00

As for your use of L, the Javadoc explains that L is for the “standalone” month name. Some languages vary the spelling depending on whether the month is used by itself or in combination. English is not one of those languages.

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

Removing / from the input String should work. (Didn't check)

val dateString = "12 Sep 2017:11:25:29 +0200"

dulvui
  • 122
  • 9
  • Why would i need to clean the date before parsing it? / is a well known character in dates formats as far as I know – Benjamin Jun 10 '21 at 20:32