aspose file tools*
The moose likes Java in General and the fly likes Parsing dates entered in different formats Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Parsing dates entered in different formats" Watch "Parsing dates entered in different formats" New topic
Author

Parsing dates entered in different formats

Serge Glaeser
Greenhorn

Joined: Jul 27, 2010
Posts: 7
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

Joined: Sep 16, 2008
Posts: 241
Take a look at setLenient. Combibig this and a set of formats might do the trick for you.


SCJP 6, OCMJD 6, OCPJWSD 6
I no good English.
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
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.


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19693
    
  20

A JSpinner with a SpinnerDateModel is better than a JComboBox or JTextField. It still allows free typing but performs validation on the entered text.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Serge Glaeser
Greenhorn

Joined: Jul 27, 2010
Posts: 7
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

Joined: Jul 27, 2010
Posts: 7
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

Joined: Oct 27, 2005
Posts: 19693
    
  20

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

Joined: Aug 19, 2009
Posts: 276

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?


OCPJP 6
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19693
    
  20

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

Joined: Aug 16, 2005
Posts: 14146
    
  18

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Vinoth Kumar Kannan
Ranch Hand

Joined: Aug 19, 2009
Posts: 276

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

Joined: Oct 27, 2005
Posts: 19693
    
  20

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
 
subject: Parsing dates entered in different formats