1

I am trying to parse the following date: May 5 18:44:44 from a syslog entry. Note that after 'May' there are 2 spaces: one as separator and the other as omitted zero from the day of month.

The following code fails with java.lang.IllegalArgumentException: Invalid format: "May 5 18:44:44" is malformed at " 5 18:44:44"

String input = "May  5 18:44:44";
DateTimeFormatter formatter = DateTimeFormat.forPattern("MMM d HH:mm:ss")
    .withLocale(Locale.US);
DateTime dt = formatter.parseDateTime(input);

Apparently the parser does not grok the 2nd space. And it also works when I "manually" remove that 2nd space.

Does anyone have an idea on how to solve this either with joda time or standard java DateFormat?

Heiko Rupp
  • 30,426
  • 13
  • 82
  • 119
  • Yes, this is pretty much a duplicate. Searched quite a while to not find it ... – Heiko Rupp May 05 '14 at 18:18
  • Regular Expressions are a good method to burn CPU cycles, but if you are willing you can use grok. http://stackoverflow.com/questions/19565755/how-to-parse-using-grok-from-java-is-there-any-example-available – eckes May 05 '14 at 18:22

1 Answers1

1

Would you be willing to run 's/\s+/ ' first?

djechlin
  • 59,258
  • 35
  • 162
  • 290
  • While I was hoping that there is a magic letter in the format or setting for the formatter, I think this a very good solution here. Thanks. – Heiko Rupp May 05 '14 at 18:16