File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes How to parse a user-entered Date into a Date or Calendar object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to parse a user-entered Date into a Date or Calendar object" Watch "How to parse a user-entered Date into a Date or Calendar object" New topic
Author

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

Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4657
    
    5

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
Sheriff

Joined: Sep 28, 2004
Posts: 18836
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4657
    
    5

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

Joined: Aug 11, 2007
Posts: 4657
    
    5

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
Sheriff

Joined: Sep 28, 2004
Posts: 18836
    
  40

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

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Oct 27, 2005
Posts: 19693
    
  20

Check out the parse method that takes a ParsePosition. It doesn't throw an exception but returns null instead. For example:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
 
subject: How to parse a user-entered Date into a Date or Calendar object