File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to parse a user-entered Date into a Date or Calendar object

 
Pat Farrell
Rancher
Posts: 4660
5
Linux Mac OS X VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The standard DateFormat (and subclasses) handle parsing of date strings for well defined values using a mask.

Is there a more general solution?

Something that does not force the user to enter dates in one and only one format?

I'd like to let the user enter what they want, say

02/14/10
or Jan 14 2010
or 15-feb-10

It would be cool, but not required, if it could do reasonable guesses, so that

2/15/10 is guessed as Feb 15, since there is no 15th month.

I'm OK with 3/10/10 being either March 10 or October 3, perhaps guided by Locale.

Thanks
Pat
 
Henry Wong
author
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interesting. This is actually something that can be done "lazily" -- meaning you can add new format to check for, as you think of them.

I can start you off...



Of course, if there is already an implementation available, I would be interested too...

Henry
 
Pat Farrell
Rancher
Posts: 4660
5
Linux Mac OS X VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, I'll play a bit with it.

Parsing dates is such a fundamental concept, I would have expected it to be standard in either Java itself or JEE.

For my initial code, I don't need i18n, but doing it right even in US style is something that begs for code reuse.
 
Pat Farrell
Rancher
Posts: 4660
5
Linux Mac OS X VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
public Date string2Date(String datestr, String regex, String dateformat) {
datestr = datestr.trim();


What, exactly is the impact of the second line? Does it do the trim() on a local copy of the input string (call by value) or does it trim() the argument (call by name)?

Would be more natural for me to do a:

String working = datestr.trim();

even if the actual implementation is the same, my version looks safer.

Or am I all wet?
 
Henry Wong
author
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pat Farrell wrote:
What, exactly is the impact of the second line? Does it do the trim() on a local copy of the input string (call by value) or does it trim() the argument (call by name)?


Strings are not mutable. You can't change / impact a string object in any way.

Java passes a copy of the reference, for reference parameters. You can't change the reference variable used to call the method, by changing it in the method.

Henry
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pat Farrell wrote:Parsing dates is such a fundamental concept, I would have expected it to be standard in either Java itself or JEE.

And it is--within reason. Dealing with human-generated values is a non-trivial problem, and IMO it's nonsensical to expect a standard library to be able to handle all possible inputs, considering the sheer number of ways it could be entered. It's trivially easy to wrap up trying a bunch of different formats, and the Apache Commons DateUtils class has already it for us:

http://commons.apache.org/lang/api-2.4/org/apache/commons/lang/time/DateUtils.html#parseDate%28java.lang.String,%20java.lang.String[]%29
 
Rob Spoor
Sheriff
Pie
Posts: 20385
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out the parse method that takes a ParsePosition. It doesn't throw an exception but returns null instead. For example:
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic