0

Given the requirement to parse input strings such as "a week from now" and "20/05/15" into Date objects in Java, Natty and JChronic seem to be my main options.

However both of them default to parsing "01/12/15" as 12th of January rather than 1st of December, i.e. middle-endian or MDY. Ruby's Chronic library has :endian_precedence => [:little, :median] option to reconfigure this behaviour however I cannot find anything similar in Natty or JChronic. Their constructor do take TimeZone and Calendar respectively but this does not seem to have an effect on the parser, neither does default locale of the machine.

How do I configure either of them to parse "01/12/15" in little-endian DMY format? Is there a third natural language date parser library in Java that can do that?

Ilya Kozhevnikov
  • 10,242
  • 4
  • 40
  • 70
  • Surely you could *parse* "01/12/15" using `SimpleDateFormat`, Joda Time or something similar. It sounds like you only need to use Natty or JChronic for the *formatting*. – Jon Skeet May 20 '15 at 12:53
  • @JonSkeet sure, but it's only one possible input format. I'd like get away without pre-Chronic regex pre-parser. – Ilya Kozhevnikov May 20 '15 at 12:57
  • So you've got multiple input formats? Don't you think that's something that should be explained in the question? It's pretty unclear to me what you're trying to achieve here. – Jon Skeet May 20 '15 at 13:02
  • @JonSkeet sorry. I want natural language date parser in Java, relative and absolute, where input string 01/12/15 would be parsed as 2015-12-01. Natty or JChronic seem to be my options except the part about configuring endian precedence. – Ilya Kozhevnikov May 20 '15 at 13:06
  • You keep talking about parsing as if the result was a string. It's not - you parse to convert a string to some other type (`int`, `Date`, whatever) and then you format to convert that type back to a string. It's important to keep those ideas separate. Now, you say there are multiple input formats - but you've still only given one. How are we meant to know about your actual requirements? Please read http://tinyurl.com/stack-hints and edit your question to be a lot clearer. – Jon Skeet May 20 '15 at 13:07
  • @JonSkeet With all due respect to you and your work on NodaTime I would have to disagree. I think "How to configure little endian in Natty or JChronic?" cannot possibly be any clearer and does not need to go into why I need natural language parser instead of JodaTIme in the first place. – Ilya Kozhevnikov May 20 '15 at 13:22
  • Well *I* don't understand what your requirements are, so I think it's fair to suggest that they're at least not as clear as they can be. Note that your question doesn't ask "How to configure little endian in Natty or JChronic?" - it only talks about endian-ness in respect to Ruby, and the relevance of that isn't clear. Given that the *only* input format you've given isn't natural language, it's very unclear why you *do* need a natural language parser. It's also still not clear whether you're performing String to String conversion (parse, reformat) or just parsing. – Jon Skeet May 20 '15 at 13:24
  • Of course you're welcome to ignore my suggestion to clarify your question - but if you actually want to get an answer, I'm not sure why you would assume that I'm too stupid to understand your question but everyone else will do so with no problems. – Jon Skeet May 20 '15 at 13:25
  • @JonSkeet please see the edit. In no way I was implying "too stupid" in my comments, I have too much respect for your work. – Ilya Kozhevnikov May 20 '15 at 13:49
  • That's definitely better, but it's still not clear what *other* formats you need to understand - or what you'd do with 01/13/2015 for example. Should *that* parse as January 13th, or should it fail as an error? (I'd be nervous of the former behaviour, personally...) (Do you have a complete list of formats you want to support? Natural language absolute as well as relative options?) – Jon Skeet May 20 '15 at 13:55
  • 1
    Looking at the source for both of them, I agree that it doesn't look easy to do, btw. You could fork [`DateParser.g`](https://github.com/joestelmach/natty/blob/master/src/main/antlr3/com/joestelmach/natty/generated/DateParser.g) in Natty pretty easily and rebuild as a last resort... – Jon Skeet May 20 '15 at 13:58

0 Answers0