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

Parsing dates entered in different formats

 
Serge Glaeser
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello

I'm having a hard time parsing dates that are entered in different formats. Is there a general way to do that? I have a text field where the user can enter a date (so obviously he can enter any format) ex.

10102010
101010
10.10.2010
10.10.10
or the date can be invalid (505002 or anything else)

Is there a general approach on how to handle things like that? i found the classes simpledateformatter and dateformatter, but they can handle only one format.

any tips appreciated.

thanks
 
Martin Vanyavchich
Ranch Hand
Posts: 241
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at setLenient. Combibig this and a set of formats might do the trick for you.
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The best way would probably be to not let the user enter the date via a text field. That way you can more easily control the format of the date entered. If this is a swing app, you could possibly use a JComboBox to constrain the possible inputs.
 
Rob Spoor
Sheriff
Pie
Posts: 20380
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A JSpinner with a SpinnerDateModel is better than a JComboBox or JTextField. It still allows free typing but performs validation on the entered text.
 
Serge Glaeser
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for all the replies. Spinners and combos are not an option unfortunately (and we're using SWT not swing).

this setLenient sounds interesting. I'll have a look at that.

thanks
 
Serge Glaeser
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well lenient seems to be more forgiving if someone misspells a date (ex. 32.01.2010 would become 01.02.2010 instead of throwing an exception) but doesn't help with the formatting.

any other ideas? anyone done anything similar?
 
Rob Spoor
Sheriff
Pie
Posts: 20380
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use a number of allowed formats, then check each of them:
The order is quite important here; if MM/dd/yyyy comes before dd/MM/yyyy then 01/02/2010 will be January 2nd. If the order is the other way around then it's February 1st.
 
Vinoth Kumar Kannan
Ranch Hand
Posts: 276
Chrome Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:
Date date = format.parse(input, position); // returns null rather than throw a ParseException


Rob, one question...
The DateFormat class defines - public abstract Date parse(String source, ParsePosition pos)
then for format.parse(input, position) to work, format must be a SimpleDateFormat class object,right?
 
Rob Spoor
Sheriff
Pie
Posts: 20380
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It doesn't need to be. It can be any DateFormat subclass. SimpleDateFormat is mostly chosen but others are also possible.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is not a simple problem for which there is a simple solution, because there are numerous ways to enter a date in an ambiguous way.

For example, what if I enter "020310"? Does that mean 2 March 2010 (ddmmyy), 3 February 2010 (mmddyy), 10 March 2002 (yymmdd) or some other possible combination? You'll have to think about what inputs you're going to accept and how the different possibilities should be interpreted.

There's no standard method in the standard Java library to parse any "arbitrary format" string into a date.
 
Vinoth Kumar Kannan
Ranch Hand
Posts: 276
Chrome Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any difference between format.parse(String,ParsePosition) and parse(String), other than the first returning null instead of a ParseException by the second?
 
Rob Spoor
Sheriff
Pie
Posts: 20380
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. In fact, in the current implementation parse(String) calls parse(String, ParseException), then either returns the parsed Date or throws a ParseException:
By the way, these two fields of ParsePosition have package visibility; we must use the matching getters and setters.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic